Line data Source code
1 : /*====================================================================================
2 : EVS Codec 3GPP TS26.452 Aug 12, 2021. Version 16.3.0
3 : ====================================================================================*/
4 : #include <stdint.h>
5 : #include "options.h" /* Compilation switches */
6 : #include "cnst.h" /* Common constants */
7 : #include "rom_com_fx.h"
8 : #include "rom_com.h"
9 : // #include "basop_mpy.h"
10 : #include <assert.h>
11 : #include "prot_fx.h" /* Function prototypes */
12 : #include "prot_fx_enc.h" /* Function prototypes */
13 :
14 : /*-------------------------------------------------------------------*
15 : * Local constants
16 : *-------------------------------------------------------------------*/
17 : #define ALPHA_ENER_FAST_FX 29491 /* Fast adaptation (noise down, speech up) */
18 : #define ALPHA_ENER_SLOW_FX 32440 /* Fast adaptation (noise down, speech up) */
19 :
20 : #define MIN_CNT 50 /* Minimum frame number before SID interval adaptation */
21 :
22 : #define SNR_H_FX 13056 /* Estimated SNR and corresponding SID interval */
23 : #define SNR_L_FX 9216 /* 51dB corresponds to 25dB SNR before noise supressor */
24 : #define INT_H 50
25 : #define INT_L 8
26 : #define RATIO ( INT_H - INT_L ) / ( ( SNR_H_FX - SNR_L_FX ) / 256 )
27 :
28 : #define LTE_VAR_FX -1024 /* Q8, -4.0f */
29 :
30 : #define CNG_TYPE_HO 20 /* hangover for switching between CNG types */
31 :
32 :
33 : #define MAX_BRATE_DTX_EVS ACELP_24k40 /* maximum bitrate to which the default DTX is applied in EVS; otherwise DTX is applied only in silence */
34 : #define MAX_BRATE_DTX_IVAS IVAS_80k /* maximum bitrate to which the default DTX is applied in IVAS; otherwise DTX is applied only in silence */
35 :
36 : /*-------------------------------------------------------------------*
37 : * Local function prototypes
38 : *-------------------------------------------------------------------*/
39 :
40 : static void update_SID_cnt_fx( DTX_ENC_HANDLE hDtxEnc, const Word32 core_brate, const Word16 Opt_AMR_WB );
41 : /*==================================================================================*/
42 : /* FUNCTION : dtx_ivas_fx() */
43 : /*----------------------------------------------------------------------------------*/
44 : /* PURPOSE : Discontinuous transmission operation */
45 : /*----------------------------------------------------------------------------------*/
46 : /* INPUT ARGUMENTS : */
47 : /* _ (Encoder_State_Fx) st_fx : encoder state structure */
48 : /* _ (Word16) vad : vad flag Q0 */
49 : /* _ (Word16[]) speech_fx : Pointer to the speech frame qSpeech */
50 : /* _ (Word16) qSpeech : speech buffer qformat value */
51 : /* _ (Word16*) qener : frame_ener/lt_ener_voiced/lt_ener_noise buf qformat */
52 : /*----------------------------------------------------------------------------------*/
53 : /* OUTPUT ARGUMENTS : */
54 : /* _ (Word16*) qener : frame_ener/lt_ener_voiced/lt_ener_noise buf qformat */
55 : /* _ (Encoder_State_Fx) st_fx : encoder state structure */
56 : /*----------------------------------------------------------------------------------*/
57 :
58 : /*----------------------------------------------------------------------------------*/
59 : /* RETURN ARGUMENTS : */
60 : /* _ None */
61 : /*==================================================================================*/
62 1128984 : void dtx_ivas_fx(
63 : Encoder_State *st_fx, /* i/o: encoder state structure */
64 : const Word32 last_ivas_total_brate, /* i : last IVAS total bitrate Q0*/
65 : const Word32 ivas_total_brate, /* i : IVAS total bitrate Q0*/
66 : const Word16 vad, /* i : vad flag for DTX Q0*/
67 : const Word16 speech[], /* i : Pointer to the speech frame Q_speech*/
68 : Word16 Q_speech /* i : Q factor for speech */
69 : )
70 : {
71 : Word16 alpha, i, j, Q_speech2;
72 : Word32 L_tmp;
73 1128984 : DTX_ENC_HANDLE hDtxEnc = st_fx->hDtxEnc;
74 1128984 : TD_CNG_ENC_HANDLE hTdCngEnc = st_fx->hTdCngEnc;
75 :
76 : Word16 last_br_cng_flag, last_br_flag, br_dtx_flag;
77 : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
78 1128984 : Flag Overflow = 0;
79 1128984 : move32();
80 : #endif
81 : Word32 total_brate_ref;
82 :
83 1128984 : total_brate_ref = st_fx->total_brate;
84 1128984 : move32();
85 :
86 1128984 : IF( st_fx->dtx_sce_sba != 0 )
87 : {
88 11370 : last_br_cng_flag = 1;
89 11370 : last_br_flag = 1;
90 11370 : move16();
91 11370 : move16();
92 : }
93 : ELSE
94 : {
95 1117614 : test();
96 1117614 : test();
97 1117614 : test();
98 1117614 : last_br_cng_flag = LE_32( st_fx->last_total_brate_cng, MAX_BRATE_DTX_EVS ) || LT_32( ( st_fx->lp_noise_32fx ), DTX_THR * 16777216 ) || ( EQ_16( st_fx->element_mode, IVAS_SCE ) && LE_32( st_fx->last_total_brate_cng, MAX_BRATE_DTX_IVAS ) );
99 :
100 1117614 : test();
101 1117614 : test();
102 1117614 : test();
103 :
104 1117614 : last_br_flag = ( st_fx->element_mode == EVS_MONO && LE_32( st_fx->last_total_brate, MAX_BRATE_DTX_EVS ) ) ||
105 1822077 : ( st_fx->element_mode != EVS_MONO && LE_32( last_ivas_total_brate, MAX_BRATE_DTX_IVAS ) ) ||
106 704463 : LT_32( ( st_fx->lp_noise_32fx ), DTX_THR * 16777216 );
107 : }
108 :
109 : /* Initialization */
110 1128984 : IF( st_fx->ini_frame == 0 )
111 : {
112 8212 : st_fx->active_cnt = CNG_TYPE_HO;
113 8212 : move16();
114 :
115 8212 : st_fx->cng_type = FD_CNG;
116 8212 : move16();
117 8212 : test();
118 8212 : test();
119 8212 : test();
120 8212 : if ( ( EQ_16( st_fx->codec_mode, MODE1 ) || st_fx->Opt_AMR_WB ) && NE_16( st_fx->element_mode, IVAS_SCE ) && NE_16( st_fx->element_mode, IVAS_CPE_MDCT ) )
121 : {
122 132 : st_fx->cng_type = LP_CNG;
123 132 : move16();
124 : }
125 : }
126 1128984 : test();
127 1128984 : test();
128 1128984 : test();
129 1128984 : test();
130 1128984 : test();
131 1128984 : IF( st_fx->Opt_DTX_ON && vad == 0 &&
132 : GT_16( st_fx->ini_frame, 2 ) && /* CNG coding starts after 2 frames */
133 : st_fx->fd_cng_reset_flag == 0 &&
134 : NE_16( st_fx->last_core, AMR_WB_CORE ) &&
135 : st_fx->Opt_AMR_WB == 0 )
136 : {
137 34253 : test();
138 34253 : test();
139 34253 : test();
140 34253 : IF( GT_32( st_fx->last_core_brate, SID_2k40 ) && NE_32( st_fx->last_total_brate_cng, -1 ) &&
141 : NE_32( st_fx->last_total_brate_cng, st_fx->total_brate ) && last_br_cng_flag )
142 : {
143 4087 : st_fx->total_brate = st_fx->last_total_brate_cng;
144 4087 : move32();
145 4087 : test();
146 4087 : if ( !( EQ_32( st_fx->total_brate, ACELP_7k20 ) && st_fx->Opt_SC_VBR ) )
147 : {
148 4087 : st_fx->Opt_SC_VBR = 0;
149 4087 : move16();
150 : }
151 4087 : st_fx->rf_mode = st_fx->last_rf_mode_cng;
152 4087 : move16();
153 4087 : st_fx->bwidth = st_fx->last_bwidth_cng;
154 4087 : move16();
155 4087 : st_fx->codec_mode = st_fx->last_codec_mode_cng;
156 4087 : move16();
157 : }
158 34253 : test();
159 34253 : test();
160 34253 : IF( LE_32( st_fx->last_core_brate, SID_2k40 ) && NE_32( st_fx->last_total_brate, st_fx->total_brate ) && last_br_flag )
161 :
162 : {
163 2933 : st_fx->total_brate = st_fx->last_total_brate;
164 2933 : move32();
165 2933 : test();
166 2933 : if ( !( EQ_32( st_fx->total_brate, ACELP_7k20 ) && st_fx->Opt_SC_VBR ) )
167 : {
168 2933 : st_fx->Opt_SC_VBR = 0;
169 2933 : move16();
170 : }
171 :
172 2933 : st_fx->Opt_RF_ON = 0;
173 2933 : move16();
174 2933 : test();
175 2933 : test();
176 2933 : test();
177 2933 : if ( st_fx->rf_mode && st_fx->rf_fec_offset > 0 && EQ_32( st_fx->total_brate, ACELP_13k20 ) && ( st_fx->bwidth != NB ) )
178 : {
179 0 : st_fx->Opt_RF_ON = 1;
180 0 : move16();
181 : }
182 2933 : st_fx->rf_mode = st_fx->Opt_RF_ON;
183 2933 : move16();
184 2933 : st_fx->bwidth = st_fx->last_bwidth;
185 2933 : move32();
186 2933 : IF( st_fx->element_mode > EVS_MONO )
187 : {
188 2933 : st_fx->codec_mode = MODE1;
189 2933 : move16();
190 : }
191 : ELSE
192 : {
193 0 : st_fx->codec_mode = get_codec_mode( st_fx->total_brate );
194 0 : move16();
195 : }
196 : }
197 : }
198 :
199 : /*------------------------------------------------------------------------*
200 : * Select SID or FRAME_NO_DATA frame if DTX is enabled
201 : *------------------------------------------------------------------------*/
202 :
203 1128984 : br_dtx_flag = 1;
204 1128984 : move16();
205 :
206 1128984 : IF( st_fx->dtx_sce_sba == 0 )
207 : {
208 1117614 : test();
209 1117614 : test();
210 1117614 : test();
211 1117614 : test();
212 1117614 : br_dtx_flag = ( ( st_fx->element_mode == EVS_MONO ) && LE_32( st_fx->total_brate, MAX_BRATE_DTX_EVS ) ) ||
213 1826576 : ( ( st_fx->element_mode != EVS_MONO ) && LE_32( ivas_total_brate, MAX_BRATE_DTX_IVAS ) ) ||
214 708962 : LT_16( extract_h( st_fx->lp_noise_32fx ), DTX_THR * 256 );
215 : }
216 1128984 : test();
217 1128984 : test();
218 1128984 : test();
219 1128984 : test();
220 1128984 : IF( st_fx->Opt_DTX_ON && vad == 0 &&
221 : GT_16( st_fx->ini_frame, 2 ) && /* CNG coding starts after 2 frames */
222 : br_dtx_flag &&
223 : st_fx->fd_cng_reset_flag == 0 )
224 : {
225 : /* reset counter */
226 30544 : st_fx->active_cnt = 0;
227 30544 : move16();
228 :
229 30544 : IF( st_fx->Opt_AMR_WB )
230 : {
231 0 : st_fx->last_total_brate_cng = -1;
232 0 : move16();
233 : }
234 : ELSE
235 : {
236 30544 : st_fx->last_total_brate_cng = st_fx->total_brate;
237 30544 : move16();
238 30544 : st_fx->last_bwidth_cng = st_fx->bwidth;
239 30544 : move16();
240 30544 : st_fx->last_codec_mode_cng = st_fx->codec_mode;
241 30544 : move16();
242 30544 : st_fx->last_rf_mode_cng = st_fx->rf_mode;
243 30544 : move16();
244 : }
245 :
246 30544 : IF( hDtxEnc->cnt_SID == 0 )
247 : {
248 : /* this will be a SID frame */
249 5409 : IF( st_fx->Opt_AMR_WB )
250 : {
251 0 : st_fx->core_brate = SID_1k75;
252 0 : move32();
253 : }
254 : ELSE
255 : {
256 5409 : st_fx->core_brate = SID_2k40;
257 5409 : move32();
258 : }
259 : }
260 : ELSE
261 : {
262 : /* this will be a no data frame */
263 25135 : st_fx->core_brate = FRAME_NO_DATA;
264 25135 : move32();
265 : }
266 :
267 30544 : test();
268 30544 : test();
269 30544 : IF( ( st_fx->core_brate == FRAME_NO_DATA ) && ( st_fx->last_core != ACELP_CORE ) && !st_fx->Opt_AMR_WB )
270 : {
271 : /* force SID frame when switching from HQ core or AMR-WB IO mode into inactive frame in ACELP core when DTX is on */
272 2367 : st_fx->core_brate = SID_2k40;
273 2367 : move32();
274 : }
275 : // PMT("dtx_sce_sba code is missing")
276 30544 : test();
277 30544 : test();
278 30544 : IF( ( ( st_fx->last_core != ACELP_CORE ) || EQ_16( st_fx->cng_type, FD_CNG ) ) && EQ_16( st_fx->dtx_sce_sba, 1 ) )
279 : {
280 759 : st_fx->cng_type = FD_CNG;
281 759 : move16();
282 759 : test();
283 759 : test();
284 759 : test();
285 759 : test();
286 759 : test();
287 759 : test();
288 759 : IF( ( st_fx->element_mode == EVS_MONO ) && ( EQ_32( st_fx->total_brate, ACELP_9k60 ) || EQ_32( st_fx->total_brate, ACELP_16k40 ) || EQ_32( st_fx->total_brate, ACELP_24k40 ) || EQ_32( st_fx->total_brate, ACELP_48k ) || EQ_32( st_fx->total_brate, HQ_96k ) || EQ_32( st_fx->total_brate, HQ_128k ) ) )
289 : {
290 0 : st_fx->codec_mode = MODE2;
291 0 : move16();
292 : }
293 : }
294 : ELSE
295 : {
296 29785 : test();
297 29785 : test();
298 29785 : test();
299 29785 : test();
300 29785 : IF( ( EQ_16( st_fx->cng_type, FD_CNG ) && ( LE_32( st_fx->total_brate, MAX_BRATE_DTX_EVS ) || ( ( st_fx->element_mode != EVS_MONO ) && LE_32( ivas_total_brate, MAX_BRATE_DTX_IVAS ) ) ) ) || ( EQ_16( st_fx->element_mode, IVAS_CPE_MDCT ) ) ) /* at highest bitrates, use exclusively LP_CNG */
301 : {
302 27135 : test();
303 27135 : test();
304 27135 : test();
305 27135 : IF( ( st_fx->element_mode == EVS_MONO ) && ( EQ_32( st_fx->total_brate, ACELP_9k60 ) || EQ_32( st_fx->total_brate, ACELP_16k40 ) || EQ_32( st_fx->total_brate, ACELP_24k40 ) ) )
306 : {
307 0 : st_fx->codec_mode = MODE2;
308 0 : move16();
309 : }
310 : }
311 : ELSE
312 : {
313 2650 : st_fx->cng_type = LP_CNG;
314 2650 : move16();
315 2650 : if ( EQ_16( st_fx->codec_mode, MODE2 ) )
316 : {
317 0 : hTdCngEnc->lp_cng_mode2 = 1;
318 0 : move16();
319 : }
320 2650 : st_fx->codec_mode = MODE1;
321 2650 : move16();
322 : }
323 : }
324 : /* reset the bitstream (IVAS format signalling was already written) */
325 30544 : test();
326 30544 : IF( NE_16( st_fx->element_mode, IVAS_CPE_MDCT ) && st_fx->hBstr != NULL )
327 : {
328 :
329 21381 : reset_indices_enc_fx( st_fx->hBstr, st_fx->hBstr->nb_ind_tot );
330 : }
331 : }
332 1098440 : ELSE IF( st_fx->element_mode != EVS_MONO )
333 : {
334 1098440 : st_fx->total_brate = total_brate_ref;
335 1098440 : move32();
336 : }
337 :
338 : /*------------------------------------------------------------------------*
339 : * Reset counters when in active frame (not in SID or FRAME_NO_DATA frame)
340 : *------------------------------------------------------------------------*/
341 : /* NB core bit rate can be "-1" at startup , so one can not use core_brate_fx <=2400 */
342 1128984 : test();
343 1128984 : test();
344 1128984 : IF( ( NE_32( st_fx->core_brate, SID_2k40 ) ) && ( NE_32( st_fx->core_brate, SID_1k75 ) ) && ( st_fx->core_brate != FRAME_NO_DATA ) )
345 : {
346 1098440 : IF( hDtxEnc != NULL )
347 : {
348 108408 : hDtxEnc->cnt_SID = 0;
349 108408 : move16();
350 :
351 : /* change SID update rate */
352 : /* first SID update is only 8 (3) frames after the active speech end */
353 108408 : IF( !st_fx->Opt_AMR_WB )
354 : {
355 108408 : hDtxEnc->max_SID = FIXED_SID_RATE;
356 108408 : move16();
357 : }
358 : ELSE
359 : {
360 0 : hDtxEnc->max_SID = 3;
361 0 : move16(); /* first SID update is only 3 frames after the active speech end */
362 : }
363 :
364 108408 : if ( LT_16( hDtxEnc->interval_SID, hDtxEnc->max_SID ) )
365 : {
366 0 : hDtxEnc->max_SID = hDtxEnc->interval_SID;
367 0 : move16(); /* change SID update rate */
368 : }
369 :
370 108408 : hDtxEnc->cng_cnt = 0;
371 108408 : move16(); /* reset the counter of CNG frames for averaging */
372 : }
373 1098440 : test();
374 1098440 : test();
375 1098440 : IF( GE_16( st_fx->active_cnt, CNG_TYPE_HO ) && st_fx->Opt_AMR_WB == 0 && NE_16( st_fx->element_mode, IVAS_CPE_MDCT ) )
376 : {
377 403702 : IF( EQ_16( st_fx->element_mode, IVAS_SCE ) )
378 : {
379 : Word32 lp_thresh, fd_thresh;
380 : // PMT( "lp_thresh scaling is to be found" )
381 350476 : test();
382 350476 : IF( st_fx->Opt_DTX_ON && EQ_16( st_fx->dtx_sce_sba, 1 ) )
383 : {
384 10163 : lp_thresh = 327680; // 5 in Q16
385 10163 : move32();
386 10163 : fd_thresh = 131072; // 2 in Q16
387 10163 : move32();
388 : }
389 : ELSE
390 : {
391 340313 : lp_thresh = 655360; // 10 in Q16
392 340313 : move32();
393 340313 : fd_thresh = 327680; // 5 in Q16
394 340313 : move32();
395 : }
396 :
397 : /*More conservative selection of LP-CNG for SCE*/
398 350476 : test();
399 350476 : test();
400 350476 : test();
401 350476 : IF( st_fx->cng_type == LP_CNG && ( GT_32( st_fx->bckr_tilt_lt, lp_thresh ) ) )
402 : {
403 0 : st_fx->cng_type = FD_CNG;
404 0 : move16();
405 : }
406 350476 : ELSE IF( EQ_16( st_fx->cng_type, FD_CNG ) && ( LT_32( st_fx->bckr_tilt_lt, fd_thresh ) ) && ( GT_32( st_fx->lp_noise_32fx, 67108864 /* 2 in Q24 */ ) ) )
407 : {
408 64 : st_fx->cng_type = LP_CNG;
409 64 : move16();
410 : }
411 : }
412 : ELSE
413 : {
414 53226 : test();
415 53226 : test();
416 53226 : test();
417 53226 : test();
418 53226 : test();
419 53226 : test();
420 53226 : test();
421 53226 : test();
422 53226 : IF( ( st_fx->cng_type == LP_CNG ) && ( ( ( st_fx->input_bwidth == NB ) && GT_32( st_fx->bckr_tilt_lt, 589824 /*9.f Q16*/ ) ) || ( ( st_fx->input_bwidth > NB ) && GT_32( st_fx->bckr_tilt_lt, 2949120l /*45.f Q16*/ ) ) ) )
423 : {
424 66 : st_fx->cng_type = FD_CNG;
425 66 : move16();
426 : }
427 53160 : ELSE IF( EQ_16( st_fx->cng_type, FD_CNG ) && ( ( ( st_fx->input_bwidth == NB ) && LT_32( st_fx->bckr_tilt_lt, 131072l /*2.f Q16*/ ) ) || ( ( st_fx->input_bwidth > NB ) && LT_32( st_fx->bckr_tilt_lt, 655360l /*10.f Q16*/ ) ) ) )
428 : {
429 39 : st_fx->cng_type = LP_CNG;
430 39 : move16();
431 : }
432 : }
433 403702 : st_fx->last_total_brate_cng = -1;
434 403702 : move16();
435 : }
436 694738 : ELSE IF( st_fx->Opt_AMR_WB )
437 : {
438 0 : st_fx->cng_type = LP_CNG;
439 0 : move16();
440 : }
441 1098440 : st_fx->active_cnt = add( st_fx->active_cnt, 1 );
442 1098440 : move16();
443 1098440 : st_fx->active_cnt = s_min( st_fx->active_cnt, 200 );
444 1098440 : move16();
445 : }
446 :
447 : /*------------------------------------------------------------------------*
448 : * Update speech and background noise long-term energy
449 : *------------------------------------------------------------------------*/
450 1128984 : IF( hDtxEnc != NULL )
451 : {
452 138952 : hDtxEnc->frame_ener_fx = L_deposit_l( 0 );
453 138952 : move32();
454 :
455 138952 : IF( st_fx->Opt_DTX_ON )
456 : {
457 138952 : Q_speech2 = add( shl( Q_speech, 1 ), 7 );
458 2362184 : FOR( j = 0; j < 16; j++ )
459 : {
460 2223232 : L_tmp = L_mult0( *speech, *speech ); /*2*Q_speech*/
461 2223232 : speech++;
462 35571712 : FOR( i = 1; i < L_FRAME / 16; i++ )
463 : {
464 33348480 : L_tmp = L_mac0_o( L_tmp, *speech, *speech, &Overflow ); /*2*Q_speech*/
465 33348480 : speech++;
466 : }
467 2223232 : hDtxEnc->frame_ener_fx = L_add( hDtxEnc->frame_ener_fx, L_shr( L_tmp, Q_speech2 ) ); /* Q(-7) */
468 2223232 : move32();
469 : }
470 :
471 : /* Active speech (voiced) */
472 :
473 138952 : IF( EQ_16( st_fx->clas, VOICED_CLAS ) )
474 : {
475 20740 : alpha = ALPHA_ENER_SLOW_FX; /*Q15 */
476 20740 : move16();
477 20740 : if ( GT_32( hDtxEnc->frame_ener_fx, hDtxEnc->lt_ener_voiced_fx ) )
478 : {
479 6511 : alpha = ALPHA_ENER_FAST_FX; /*Q15 */
480 6511 : move16();
481 : }
482 :
483 : /*st_fx->lt_ener_voiced_fx = alpha * st_fx->lt_ener_voiced_fx + (1.0f-alpha) * st_fx->frame_ener_fx;*/
484 20740 : L_tmp = L_sub( hDtxEnc->lt_ener_voiced_fx, hDtxEnc->frame_ener_fx ); /*Q(-7)*/
485 20740 : L_tmp = Mult_32_16( L_tmp, alpha ); /*Q(-7)*/
486 20740 : hDtxEnc->lt_ener_voiced_fx = L_add( L_tmp, hDtxEnc->frame_ener_fx ); /* Q(-7) */
487 20740 : move32();
488 :
489 20740 : hDtxEnc->VarDTX_cnt_voiced = add( hDtxEnc->VarDTX_cnt_voiced, 1 );
490 20740 : move16();
491 :
492 20740 : hDtxEnc->VarDTX_cnt_voiced = s_min( hDtxEnc->VarDTX_cnt_voiced, MIN_CNT );
493 20740 : move16();
494 : }
495 : /* Background noise */
496 118212 : ELSE IF( !st_fx->Opt_AMR_WB )
497 : {
498 118212 : alpha = ALPHA_ENER_SLOW_FX; /*Q15*/
499 118212 : move16();
500 118212 : if ( LT_32( hDtxEnc->frame_ener_fx, hDtxEnc->lt_ener_noise_fx ) )
501 : {
502 30897 : alpha = ALPHA_ENER_FAST_FX; /*Q15*/
503 30897 : move16();
504 : }
505 :
506 : /*st_fx->lt_ener_noise_fx = alpha * st_fx->lt_ener_noise_fx + (1.0f-alpha) * st_fx->frame_ener_fx;*/
507 118212 : L_tmp = L_sub( hDtxEnc->lt_ener_noise_fx, hDtxEnc->frame_ener_fx ); /* Q(-7) */
508 118212 : L_tmp = Mult_32_16( L_tmp, alpha ); /* Q(-7) */
509 118212 : hDtxEnc->lt_ener_noise_fx = L_add( L_tmp, hDtxEnc->frame_ener_fx ); /* Q(-7) */
510 118212 : move32();
511 :
512 118212 : hDtxEnc->VarDTX_cnt_noise = add( hDtxEnc->VarDTX_cnt_noise, 1 );
513 118212 : move16();
514 :
515 118212 : hDtxEnc->VarDTX_cnt_noise = s_min( hDtxEnc->VarDTX_cnt_noise, MIN_CNT );
516 118212 : move16();
517 : }
518 : }
519 : }
520 :
521 : /* Update of the SID counter */
522 1128984 : update_SID_cnt_fx( hDtxEnc, st_fx->core_brate, st_fx->Opt_AMR_WB );
523 :
524 : /* Update encoded bandwidth */
525 1128984 : test();
526 1128984 : test();
527 1128984 : IF( st_fx->Opt_DTX_ON && ( EQ_32( st_fx->core_brate, SID_2k40 ) || ( st_fx->core_brate == FRAME_NO_DATA ) ) )
528 : {
529 :
530 30544 : st_fx->bwidth = st_fx->last_bwidth;
531 30544 : move16();
532 30544 : test();
533 30544 : IF( GT_32( st_fx->last_core_brate, SID_2k40 ) && NE_32( st_fx->last_total_brate_cng, -1 ) )
534 : {
535 7605 : st_fx->bwidth = st_fx->last_bwidth_cng;
536 7605 : move16();
537 : }
538 :
539 30544 : test();
540 30544 : test();
541 30544 : IF( st_fx->Opt_RF_ON && ( EQ_32( st_fx->total_brate, ACELP_13k20 ) ) && ( ( st_fx->bwidth == NB ) ) )
542 : {
543 0 : st_fx->codec_mode = MODE1;
544 0 : move16();
545 0 : reset_rf_indices_fx( st_fx );
546 0 : st_fx->Opt_RF_ON = 0;
547 0 : move16();
548 0 : st_fx->rf_mode = 0;
549 0 : move16();
550 : }
551 :
552 30544 : test();
553 30544 : IF( st_fx->Opt_RF_ON && NE_32( st_fx->total_brate, ACELP_13k20 ) )
554 : {
555 0 : reset_rf_indices_fx( st_fx );
556 0 : move16();
557 0 : st_fx->Opt_RF_ON = 0;
558 0 : move16();
559 0 : st_fx->rf_mode = 0;
560 : }
561 :
562 : /* Set and limit the encoded bandwidth */
563 30544 : IF( EQ_16( st_fx->codec_mode, MODE2 ) )
564 : {
565 : Word16 n, bits_frame_nominal;
566 :
567 : UWord16 lsb;
568 : Word16 tmpbandwidthMin;
569 :
570 0 : Mpy_32_16_ss( st_fx->total_brate, 5243, &L_tmp, &lsb ); /* 5243 is 1/50 in Q18. (0+18-15=3) */
571 0 : bits_frame_nominal = extract_l( L_shr( L_tmp, 3 ) ); /* Q0 */
572 :
573 0 : FOR( n = 0; n < FRAME_SIZE_NB; n++ )
574 : {
575 0 : IF( EQ_16( FrameSizeConfig[n].frame_bits, bits_frame_nominal ) )
576 : {
577 0 : BREAK;
578 : }
579 : }
580 0 : IF( EQ_16( n, FRAME_SIZE_NB ) )
581 : {
582 0 : assert( !"Bitrate not supported: not part of EVS" );
583 : }
584 0 : tmpbandwidthMin = FrameSizeConfig[n].bandwidth_min;
585 0 : move16();
586 0 : if ( EQ_16( st_fx->rf_mode, 1 ) )
587 : {
588 0 : tmpbandwidthMin = WB;
589 0 : move16();
590 : }
591 0 : st_fx->bwidth = s_max( s_min( st_fx->bwidth, FrameSizeConfig[n].bandwidth_max ), tmpbandwidthMin );
592 0 : move16();
593 : }
594 : }
595 :
596 1128984 : return;
597 : }
598 :
599 : /*==================================================================================*/
600 : /* FUNCTION : dtx_fx() */
601 : /*----------------------------------------------------------------------------------*/
602 : /* PURPOSE : Discontinuous transmission operation */
603 : /*----------------------------------------------------------------------------------*/
604 : /* INPUT ARGUMENTS : */
605 : /* _ (Encoder_State_Fx) st_fx : encoder state structure */
606 : /* _ (Word16) vad : vad flag Q0 */
607 : /* _ (Word16[]) speech_fx : Pointer to the speech frame qSpeech */
608 : /* _ (Word16) qSpeech : speech buffer qformat value */
609 : /* _ (Word16*) qener : frame_ener/lt_ener_voiced/lt_ener_noise buf qformat */
610 : /*----------------------------------------------------------------------------------*/
611 : /* OUTPUT ARGUMENTS : */
612 : /* _ (Word16*) qener : frame_ener/lt_ener_voiced/lt_ener_noise buf qformat */
613 : /* _ (Encoder_State_Fx) st_fx : encoder state structure */
614 : /*----------------------------------------------------------------------------------*/
615 :
616 : /*----------------------------------------------------------------------------------*/
617 : /* RETURN ARGUMENTS : */
618 : /* _ None */
619 : /*==================================================================================*/
620 3100 : void dtx_fx(
621 : Encoder_State *st_fx, /* i/o: encoder state structure */
622 : const Word16 vad, /* i : vad flag for DTX Q0*/
623 : const Word16 speech[], /* i : Pointer to the speech frame Q_speech*/
624 : Word16 Q_speech /* i : Q factor for speech */
625 :
626 : )
627 : {
628 : Word16 alpha, j, i, Q_speech2;
629 : Word32 L_tmp;
630 3100 : DTX_ENC_HANDLE hDtxEnc = st_fx->hDtxEnc;
631 3100 : TD_CNG_ENC_HANDLE hTdCngEnc = st_fx->hTdCngEnc;
632 :
633 : Word16 last_br_cng_flag, last_br_flag, br_dtx_flag;
634 : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
635 3100 : Flag Overflow = 0;
636 3100 : move32();
637 : #endif
638 :
639 3100 : IF( st_fx->dtx_sce_sba != 0 )
640 : {
641 0 : last_br_cng_flag = 1;
642 0 : last_br_flag = 1;
643 0 : br_dtx_flag = 1;
644 0 : move16();
645 0 : move16();
646 0 : move16();
647 : }
648 : ELSE
649 : {
650 3100 : last_br_cng_flag = LE_32( st_fx->last_total_brate_cng, ACELP_24k40 ) || LT_16( st_fx->lp_noise_fx, DTX_THR * 256 ) || ( EQ_16( st_fx->element_mode, IVAS_SCE ) && LE_32( st_fx->last_total_brate_cng, ACELP_32k ) );
651 :
652 3100 : last_br_flag = LE_32( st_fx->last_total_brate, ACELP_24k40 ) || LT_16( st_fx->lp_noise_fx, DTX_THR * 256 ) || ( EQ_16( st_fx->element_mode, IVAS_SCE ) && LE_32( st_fx->last_total_brate, ACELP_32k ) );
653 3100 : br_dtx_flag = 0;
654 3100 : move16();
655 : }
656 : /* Initialization */
657 3100 : IF( st_fx->ini_frame == 0 )
658 : {
659 3 : st_fx->active_fr_cnt_fx = CNG_TYPE_HO;
660 3 : move16();
661 :
662 3 : st_fx->cng_type = FD_CNG;
663 3 : move16();
664 3 : test();
665 3 : IF( ( EQ_16( st_fx->codec_mode, MODE1 ) || st_fx->Opt_AMR_WB ) && EQ_16( st_fx->element_mode, IVAS_SCE ) && EQ_16( st_fx->element_mode, IVAS_CPE_MDCT ) )
666 : {
667 0 : st_fx->cng_type = LP_CNG;
668 0 : move16();
669 : }
670 : }
671 3100 : test();
672 3100 : test();
673 3100 : IF( st_fx->Opt_DTX_ON && vad == 0 &&
674 : GT_16( st_fx->ini_frame, 2 ) && /* CNG coding starts after 2 frames */
675 : st_fx->fd_cng_reset_flag == 0 &&
676 : NE_16( st_fx->last_core, AMR_WB_CORE ) &&
677 : st_fx->Opt_AMR_WB == 0 )
678 : {
679 0 : test();
680 0 : test();
681 0 : test();
682 0 : IF( GT_32( st_fx->last_core_brate, SID_2k40 ) && NE_32( st_fx->last_total_brate_cng, -1 ) &&
683 : NE_32( st_fx->last_total_brate_cng, st_fx->total_brate ) && last_br_cng_flag )
684 : {
685 0 : st_fx->total_brate = st_fx->last_total_brate_cng;
686 0 : move32();
687 0 : test();
688 0 : IF( !( EQ_32( st_fx->total_brate, ACELP_7k20 ) && st_fx->Opt_SC_VBR ) )
689 : {
690 0 : st_fx->Opt_SC_VBR = 0;
691 0 : move16();
692 : }
693 0 : st_fx->rf_mode = st_fx->last_rf_mode_cng;
694 0 : move16();
695 0 : st_fx->bwidth = st_fx->last_bwidth_cng;
696 0 : move16();
697 0 : st_fx->codec_mode = st_fx->last_codec_mode_cng;
698 0 : move16();
699 : }
700 0 : test();
701 0 : test();
702 0 : IF( LE_32( st_fx->last_core_brate, SID_2k40 ) && NE_32( st_fx->last_total_brate, st_fx->total_brate ) && last_br_flag )
703 :
704 : {
705 0 : st_fx->total_brate = st_fx->last_total_brate;
706 0 : move32();
707 0 : test();
708 0 : IF( !( EQ_32( st_fx->total_brate, ACELP_7k20 ) && st_fx->Opt_SC_VBR ) )
709 : {
710 0 : st_fx->Opt_SC_VBR = 0;
711 0 : move16();
712 : }
713 :
714 0 : st_fx->Opt_RF_ON = 0;
715 0 : move16();
716 0 : test();
717 0 : test();
718 0 : test();
719 0 : IF( st_fx->rf_mode && st_fx->rf_fec_offset > 0 && EQ_32( st_fx->total_brate, ACELP_13k20 ) && NE_16( st_fx->bwidth, NB ) )
720 : {
721 0 : st_fx->Opt_RF_ON = 1;
722 0 : move16();
723 : }
724 0 : st_fx->rf_mode = st_fx->Opt_RF_ON;
725 0 : move16();
726 0 : st_fx->bwidth = st_fx->last_bwidth;
727 0 : move32();
728 0 : IF( ( st_fx->element_mode > EVS_MONO ) )
729 : {
730 0 : st_fx->codec_mode = MODE1;
731 0 : move16();
732 : }
733 : ELSE
734 : {
735 0 : st_fx->codec_mode = get_codec_mode( st_fx->total_brate );
736 : }
737 : }
738 : }
739 :
740 : /*------------------------------------------------------------------------*
741 : * Select SID or FRAME_NO_DATA frame if DTX is enabled
742 : *------------------------------------------------------------------------*/
743 :
744 3100 : if ( st_fx->dtx_sce_sba == 0 )
745 : {
746 7200 : br_dtx_flag = LE_32( st_fx->total_brate, ACELP_24k40 ) || LT_16( st_fx->lp_noise_fx, DTX_THR * 256 ) || ( EQ_16( st_fx->element_mode, IVAS_SCE ) && LE_32( st_fx->total_brate, ACELP_32k ) ) ||
747 4100 : EQ_16( st_fx->element_mode, IVAS_CPE_DFT ) || ( EQ_16( st_fx->element_mode, IVAS_CPE_MDCT ) && ( LE_32( st_fx->element_brate, IVAS_64k ) || LT_16( st_fx->lp_noise_fx, DTX_THR * 256 ) ) );
748 : }
749 3100 : test();
750 3100 : test();
751 3100 : test();
752 3100 : test();
753 3100 : IF( st_fx->Opt_DTX_ON && vad == 0 &&
754 : GT_16( st_fx->ini_frame, 2 ) && /* CNG coding starts after 2 frames */
755 : br_dtx_flag &&
756 : st_fx->fd_cng_reset_flag == 0 )
757 : {
758 : /* reset counter */
759 0 : st_fx->active_fr_cnt_fx = 0;
760 0 : move16();
761 :
762 0 : IF( st_fx->Opt_AMR_WB )
763 : {
764 0 : st_fx->last_total_brate_cng = -1;
765 0 : st_fx->last_rf_mode_cng = st_fx->rf_mode;
766 0 : move32();
767 0 : move16();
768 : }
769 : ELSE
770 : {
771 0 : st_fx->last_total_brate_cng = st_fx->total_brate;
772 0 : st_fx->last_bwidth_cng = st_fx->bwidth;
773 0 : st_fx->last_codec_mode_cng = st_fx->codec_mode;
774 0 : move32();
775 0 : move16();
776 0 : move16();
777 : }
778 :
779 0 : IF( hDtxEnc->cnt_SID == 0 )
780 : {
781 : /* this will be a SID frame */
782 0 : IF( st_fx->Opt_AMR_WB )
783 : {
784 0 : st_fx->core_brate = SID_1k75;
785 0 : move32();
786 : }
787 : ELSE
788 : {
789 0 : st_fx->core_brate = SID_2k40;
790 0 : move32();
791 : }
792 : }
793 : ELSE
794 : {
795 : /* this will be a no data frame */
796 0 : st_fx->core_brate = FRAME_NO_DATA;
797 0 : move32();
798 : }
799 :
800 0 : test();
801 0 : test();
802 0 : IF( ( st_fx->core_brate == FRAME_NO_DATA ) && ( st_fx->last_core != ACELP_CORE ) && !st_fx->Opt_AMR_WB )
803 : {
804 : /* force SID frame when switching from HQ core or AMR-WB IO mode into inactive frame in ACELP core when DTX is on */
805 0 : st_fx->core_brate = SID_2k40;
806 0 : move32();
807 : }
808 :
809 : {
810 0 : test();
811 0 : test();
812 0 : test();
813 0 : test();
814 0 : IF( ( EQ_16( st_fx->cng_type, FD_CNG ) && ( LE_32( st_fx->total_brate, ACELP_24k40 ) || ( ( st_fx->element_mode != EVS_MONO ) && LE_32( st_fx->total_brate, ACELP_32k ) ) ) ) || ( EQ_16( st_fx->element_mode, IVAS_CPE_MDCT ) ) ) /* at highest bitrates, use exclusively LP_CNG */
815 : {
816 0 : test();
817 0 : test();
818 0 : IF( EQ_16( st_fx->element_mode, EVS_MONO ) && ( EQ_32( st_fx->total_brate, ACELP_9k60 ) || EQ_32( st_fx->total_brate, ACELP_16k40 ) || EQ_32( st_fx->total_brate, ACELP_24k40 ) ) )
819 : {
820 0 : st_fx->codec_mode = MODE2;
821 0 : move16();
822 : }
823 : }
824 : ELSE
825 : {
826 0 : st_fx->cng_type = LP_CNG;
827 0 : move16();
828 0 : IF( st_fx->codec_mode == MODE2 )
829 : {
830 0 : hTdCngEnc->lp_cng_mode2 = 1;
831 0 : move16();
832 : }
833 0 : st_fx->codec_mode = MODE1;
834 0 : move16();
835 : }
836 : }
837 :
838 : /* reset the bitstream (IVAS format signalling was already written) */
839 0 : IF( NE_16( st_fx->element_mode, IVAS_CPE_MDCT ) && st_fx->hBstr != NULL )
840 : {
841 0 : reset_indices_enc_fx( st_fx->hBstr, st_fx->hBstr->nb_ind_tot );
842 : }
843 : }
844 :
845 : /*------------------------------------------------------------------------*
846 : * Reset counters when in active frame (not in SID or FRAME_NO_DATA frame)
847 : *------------------------------------------------------------------------*/
848 : /* NB core bit rate can be "-1" at startup , so one can not use core_brate_fx <=2400 */
849 3100 : test();
850 3100 : test();
851 3100 : IF( ( NE_32( st_fx->core_brate, SID_2k40 ) ) && ( NE_32( st_fx->core_brate, SID_1k75 ) ) && ( st_fx->core_brate != FRAME_NO_DATA ) )
852 : {
853 3100 : IF( hDtxEnc != NULL )
854 : {
855 3100 : hDtxEnc->cnt_SID = 0;
856 3100 : move16();
857 :
858 : /* change SID update rate */
859 : /* first SID update is only 8 (3) frames after the active speech end */
860 3100 : IF( !st_fx->Opt_AMR_WB )
861 : {
862 3100 : hDtxEnc->max_SID = FIXED_SID_RATE;
863 3100 : move16();
864 : }
865 : ELSE
866 : {
867 0 : hDtxEnc->max_SID = 3;
868 0 : move16(); /* first SID update is only 3 frames after the active speech end */
869 : }
870 :
871 3100 : IF( LT_16( hDtxEnc->interval_SID, hDtxEnc->max_SID ) )
872 : {
873 0 : hDtxEnc->max_SID = hDtxEnc->interval_SID;
874 0 : move16(); /* change SID update rate */
875 : }
876 :
877 3100 : hDtxEnc->cng_cnt = 0;
878 3100 : move16(); /* reset the counter of CNG frames for averaging */
879 : }
880 3100 : test();
881 3100 : IF( GE_16( st_fx->active_fr_cnt_fx, CNG_TYPE_HO ) && st_fx->Opt_AMR_WB == 0 && NE_16( st_fx->element_mode, IVAS_CPE_MDCT ) )
882 : {
883 3100 : IF( EQ_16( st_fx->element_mode, IVAS_SCE ) )
884 : {
885 : }
886 : ELSE
887 : {
888 3100 : test();
889 3100 : test();
890 3100 : test();
891 3100 : test();
892 3100 : test();
893 3100 : test();
894 3100 : test();
895 3100 : test();
896 3100 : IF( EQ_16( st_fx->cng_type, LP_CNG ) && ( ( EQ_16( st_fx->input_bwidth, NB ) && GT_32( st_fx->bckr_tilt_lt, 589824l /*9.f Q16*/ ) ) || ( GT_16( st_fx->input_bwidth, NB ) && GT_32( st_fx->bckr_tilt_lt, 2949120l /*45.f Q16*/ ) ) ) )
897 : {
898 0 : st_fx->cng_type = FD_CNG;
899 0 : move16();
900 : }
901 3100 : ELSE IF( EQ_16( st_fx->cng_type, FD_CNG ) && ( ( EQ_16( st_fx->input_bwidth, NB ) && LT_32( st_fx->bckr_tilt_lt, 131072l /*2.f Q16*/ ) ) || ( GT_16( st_fx->input_bwidth, NB ) && LT_32( st_fx->bckr_tilt_lt, 655360l /*10.f Q16*/ ) ) ) )
902 : {
903 3 : st_fx->cng_type = LP_CNG;
904 3 : move16();
905 : }
906 : }
907 3100 : st_fx->last_total_brate_cng = -1;
908 : }
909 0 : ELSE IF( st_fx->Opt_AMR_WB )
910 : {
911 0 : st_fx->cng_type = LP_CNG;
912 0 : move16();
913 : }
914 3100 : st_fx->active_fr_cnt_fx = add( st_fx->active_fr_cnt_fx, 1 );
915 3100 : st_fx->active_fr_cnt_fx = s_min( st_fx->active_fr_cnt_fx, 200 );
916 : }
917 :
918 : /*------------------------------------------------------------------------*
919 : * Update speech and background noise long-term energy
920 : *------------------------------------------------------------------------*/
921 3100 : IF( hDtxEnc != NULL )
922 : {
923 3100 : hDtxEnc->frame_ener_fx = L_deposit_l( 0 );
924 :
925 3100 : IF( st_fx->Opt_DTX_ON )
926 : {
927 0 : Q_speech2 = add( shl( Q_speech, 1 ), 7 );
928 0 : FOR( j = 0; j < 16; j++ )
929 : {
930 0 : L_tmp = L_mult0( *speech, *speech ); /*2*Q_speech*/
931 0 : speech++;
932 0 : FOR( i = 1; i < L_FRAME / 16; i++ )
933 : {
934 0 : L_tmp = L_mac0_o( L_tmp, *speech, *speech, &Overflow ); /*2*Q_speech*/
935 0 : speech++;
936 : }
937 0 : hDtxEnc->frame_ener_fx = L_add( hDtxEnc->frame_ener_fx, L_shr( L_tmp, Q_speech2 ) ); /*Q(-7) */
938 : }
939 :
940 : /* Active speech (voiced) */
941 :
942 0 : IF( EQ_16( st_fx->clas, VOICED_CLAS ) )
943 : {
944 0 : alpha = ALPHA_ENER_SLOW_FX;
945 0 : move16();
946 0 : if ( GT_32( hDtxEnc->frame_ener_fx, hDtxEnc->lt_ener_voiced_fx ) )
947 : {
948 0 : alpha = ALPHA_ENER_FAST_FX; /*Q15 */
949 0 : move16();
950 : }
951 :
952 : /*st_fx->lt_ener_voiced_fx = alpha * st_fx->lt_ener_voiced_fx + (1.0f-alpha) * st_fx->frame_ener_fx;*/
953 0 : L_tmp = L_sub( hDtxEnc->lt_ener_voiced_fx, hDtxEnc->frame_ener_fx ); /*Q(-7)*/
954 0 : L_tmp = Mult_32_16( L_tmp, alpha ); /*Q(-7)*/
955 0 : hDtxEnc->lt_ener_voiced_fx = L_add( L_tmp, hDtxEnc->frame_ener_fx ); /*Q(-7)*/
956 :
957 0 : hDtxEnc->VarDTX_cnt_voiced = add( hDtxEnc->VarDTX_cnt_voiced, 1 );
958 :
959 0 : hDtxEnc->VarDTX_cnt_voiced = s_min( hDtxEnc->VarDTX_cnt_voiced, MIN_CNT );
960 : }
961 : /* Background noise */
962 0 : ELSE IF( !st_fx->Opt_AMR_WB )
963 : {
964 0 : alpha = ALPHA_ENER_SLOW_FX; /*Q15 */
965 0 : move16();
966 0 : if ( LT_32( hDtxEnc->frame_ener_fx, hDtxEnc->lt_ener_noise_fx ) )
967 : {
968 0 : alpha = ALPHA_ENER_FAST_FX; /*Q15 */
969 0 : move16();
970 : }
971 :
972 : /*st_fx->lt_ener_noise_fx = alpha * st_fx->lt_ener_noise_fx + (1.0f-alpha) * st_fx->frame_ener_fx;*/
973 0 : L_tmp = L_sub( hDtxEnc->lt_ener_noise_fx, hDtxEnc->frame_ener_fx ); /*Q(-7)*/
974 0 : L_tmp = Mult_32_16( L_tmp, alpha ); /*Q(-7)*/
975 0 : hDtxEnc->lt_ener_noise_fx = L_add( L_tmp, hDtxEnc->frame_ener_fx ); /*Q(-7)*/
976 0 : move32();
977 :
978 0 : hDtxEnc->VarDTX_cnt_noise = add( hDtxEnc->VarDTX_cnt_noise, 1 );
979 :
980 0 : hDtxEnc->VarDTX_cnt_noise = s_min( hDtxEnc->VarDTX_cnt_noise, MIN_CNT );
981 : }
982 : }
983 : }
984 :
985 : /* Update of the SID counter */
986 3100 : update_SID_cnt_fx( hDtxEnc, st_fx->core_brate, st_fx->Opt_AMR_WB );
987 :
988 : /* Update encoded bandwidth */
989 3100 : test();
990 3100 : test();
991 3100 : IF( st_fx->Opt_DTX_ON && ( st_fx->core_brate == SID_2k40 || st_fx->core_brate == FRAME_NO_DATA ) )
992 : {
993 :
994 0 : st_fx->bwidth = st_fx->last_bwidth;
995 0 : move16();
996 0 : test();
997 0 : IF( GT_32( st_fx->last_core_brate, SID_2k40 ) && NE_32( st_fx->last_total_brate_cng, -1 ) )
998 : {
999 0 : st_fx->bwidth = st_fx->last_bwidth_cng;
1000 0 : move16();
1001 : }
1002 :
1003 0 : test();
1004 0 : test();
1005 0 : IF( st_fx->Opt_RF_ON && ( EQ_32( st_fx->total_brate, ACELP_13k20 ) ) && ( EQ_16( st_fx->bwidth, NB ) ) )
1006 : {
1007 0 : st_fx->codec_mode = MODE1;
1008 0 : move16();
1009 0 : reset_rf_indices_fx( st_fx );
1010 0 : st_fx->Opt_RF_ON = 0;
1011 0 : move16();
1012 0 : st_fx->rf_mode = 0;
1013 0 : move16();
1014 : }
1015 :
1016 0 : test();
1017 0 : test();
1018 0 : IF( st_fx->Opt_RF_ON && NE_32( st_fx->total_brate, ACELP_13k20 ) )
1019 : {
1020 0 : reset_rf_indices_fx( st_fx );
1021 0 : move16();
1022 0 : st_fx->Opt_RF_ON = 0;
1023 0 : move16();
1024 0 : st_fx->rf_mode = 0;
1025 0 : move16();
1026 : }
1027 :
1028 : /* Set and limit the encoded bandwidth */
1029 0 : IF( EQ_16( st_fx->codec_mode, MODE2 ) )
1030 : {
1031 : Word16 n, bits_frame_nominal;
1032 :
1033 : UWord16 lsb;
1034 : Word16 tmpbandwidthMin;
1035 :
1036 0 : Mpy_32_16_ss( st_fx->total_brate, 5243, &L_tmp, &lsb ); /* 5243 is 1/50 in Q18. (0+18-15=3) */
1037 0 : bits_frame_nominal = extract_l( L_shr( L_tmp, 3 ) ); /* Q0 */
1038 :
1039 0 : FOR( n = 0; n < FRAME_SIZE_NB; n++ )
1040 : {
1041 0 : IF( EQ_16( FrameSizeConfig[n].frame_bits, bits_frame_nominal ) )
1042 : {
1043 0 : BREAK;
1044 : }
1045 : }
1046 0 : if ( n == FRAME_SIZE_NB )
1047 : {
1048 0 : assert( !"Bitrate not supported: not part of EVS" );
1049 : }
1050 0 : tmpbandwidthMin = FrameSizeConfig[n].bandwidth_min;
1051 0 : if ( EQ_16( st_fx->rf_mode, 1 ) )
1052 : {
1053 0 : tmpbandwidthMin = WB;
1054 : }
1055 0 : st_fx->bwidth = s_max( s_min( st_fx->bwidth, FrameSizeConfig[n].bandwidth_max ), tmpbandwidthMin );
1056 : }
1057 : }
1058 :
1059 3100 : return;
1060 : }
1061 :
1062 : /*---------------------------------------------------------------------*
1063 : * update_SID_cnt()
1064 : *
1065 : * Update of the SID counter
1066 : *---------------------------------------------------------------------*/
1067 :
1068 1132084 : static void update_SID_cnt_fx(
1069 : DTX_ENC_HANDLE hDtxEnc, /* i/o: common DTX handle */
1070 : const Word32 core_brate, /* i : core coder core bitrate Q0*/
1071 : const Word16 Opt_AMR_WB /* i : AMR BW IO mode? Q0*/
1072 : )
1073 : {
1074 : Word16 EstimatedSNR, delta, frac, exp;
1075 : Word32 L_tmp1, L_tmp2;
1076 :
1077 1132084 : test();
1078 1132084 : test();
1079 1132084 : IF( EQ_32( core_brate, SID_2k40 ) || EQ_32( core_brate, SID_1k75 ) || core_brate == FRAME_NO_DATA )
1080 : {
1081 : /* Adapt the SID interval */
1082 30544 : test();
1083 30544 : test();
1084 30544 : IF( hDtxEnc->var_SID_rate_flag != 0 && EQ_16( hDtxEnc->VarDTX_cnt_voiced, MIN_CNT ) && EQ_16( hDtxEnc->VarDTX_cnt_noise, MIN_CNT ) )
1085 : {
1086 : /* EstimatedSNR = 10.0f * (float)log10( (0.01f + st_fx->lt_ener_voiced) / (0.01f + st_fx->lt_ener_noise) ); */
1087 :
1088 0 : L_tmp1 = L_max( hDtxEnc->lt_ener_voiced_fx, 1 );
1089 0 : exp = norm_l( L_tmp1 );
1090 0 : frac = Log2_norm_lc( L_shl( L_tmp1, exp ) );
1091 0 : exp = sub( 30, exp );
1092 0 : L_tmp1 = L_Comp( exp, frac ); /*Q16*/
1093 0 : L_tmp2 = L_max( hDtxEnc->lt_ener_noise_fx, 1 );
1094 0 : exp = norm_l( L_tmp2 );
1095 0 : frac = Log2_norm_lc( L_shl( L_tmp2, exp ) );
1096 0 : exp = sub( 30, exp );
1097 0 : L_tmp1 = L_sub( L_tmp1, L_Comp( exp, frac ) ); /*Q16*/
1098 : /* 10 x Log10(a/b) = 10 x Log10(2) x [Log2(a) - Log2(b)] */
1099 : /* 10 x Log10(2) = ~3.0103 */
1100 0 : L_tmp1 = Mpy_32_16_1( L_tmp1, 24660 ); /* mult by 3.0103 / 4 in Q15 */
1101 0 : L_tmp1 = L_shl( L_tmp1, 2 + 8 ); /* mult by 4 and shift left 8 to go in Q24 */
1102 0 : EstimatedSNR = round_fx( L_tmp1 ); /* now in Q8 */
1103 0 : IF( GT_16( EstimatedSNR, SNR_H_FX ) )
1104 : {
1105 0 : hDtxEnc->interval_SID = INT_H;
1106 0 : move16();
1107 : }
1108 0 : ELSE IF( LT_16( EstimatedSNR, SNR_L_FX ) )
1109 : {
1110 0 : hDtxEnc->interval_SID = INT_L;
1111 0 : move16();
1112 : }
1113 : ELSE
1114 : {
1115 0 : hDtxEnc->interval_SID = extract_h( L_mac( INT_L * 65536L - SNR_L_FX / 256 * 65536L * RATIO, ( 32768 / 256 ) * RATIO, EstimatedSNR ) );
1116 : }
1117 0 : hDtxEnc->interval_SID = s_min( s_max( hDtxEnc->interval_SID, INT_L ), INT_H );
1118 :
1119 0 : test();
1120 :
1121 0 : IF( Opt_AMR_WB == 0 || NE_16( hDtxEnc->max_SID, 3 ) )
1122 : {
1123 0 : hDtxEnc->max_SID = hDtxEnc->interval_SID;
1124 0 : move16(); /* change SID update rate */
1125 : }
1126 : }
1127 30544 : test();
1128 30544 : IF( hDtxEnc->cnt_SID != 0 )
1129 : {
1130 25135 : L_tmp1 = L_max( hDtxEnc->lt_ener_noise_fx, 1 );
1131 25135 : exp = norm_l( L_tmp1 );
1132 25135 : frac = Log2_norm_lc( L_shl( L_tmp1, exp ) );
1133 25135 : exp = sub( 30, exp );
1134 25135 : L_tmp1 = L_Comp( exp, frac ); /*Q16*/
1135 25135 : L_tmp2 = L_max( hDtxEnc->lt_ener_last_SID_fx, 1 );
1136 25135 : exp = norm_l( L_tmp2 );
1137 25135 : frac = Log2_norm_lc( L_shl( L_tmp2, exp ) );
1138 25135 : exp = sub( 30, exp );
1139 25135 : L_tmp1 = L_sub( L_tmp1, L_Comp( exp, frac ) ); /*Q16*/
1140 : /* 10 x Log10(a/b) = 10 x Log10(2) x [Log2(a) - Log2(b)] */
1141 : /* 10 x Log10(2) = ~3.0103 */
1142 25135 : L_tmp1 = Mpy_32_16_1( L_tmp1, 24660 ); /* mult by 3.0103 / 4 in Q15 */
1143 25135 : L_tmp1 = L_shl( L_tmp1, 2 + 8 ); /* mult by 4 and shift left 8 to go in Q24 */
1144 25135 : delta = round_fx( L_tmp1 ); /* now in Q8 */
1145 25135 : test();
1146 25135 : test();
1147 25135 : IF( LT_16( delta, LTE_VAR_FX ) && EQ_16( hDtxEnc->VarDTX_cnt_voiced, MIN_CNT ) && EQ_16( hDtxEnc->VarDTX_cnt_noise, MIN_CNT ) )
1148 : {
1149 : /* Send SID frame, and reset lt_ener_noise */
1150 133 : hDtxEnc->lt_ener_noise_fx = hDtxEnc->frame_ener_fx; /* Q(-7) */
1151 133 : move32();
1152 : }
1153 : }
1154 : ELSE
1155 : {
1156 : /* If SID frame was sent, update long-term energy */
1157 5409 : hDtxEnc->lt_ener_last_SID_fx = hDtxEnc->lt_ener_noise_fx; /* Q(-7) */
1158 5409 : move32();
1159 : }
1160 30544 : hDtxEnc->cnt_SID = add( hDtxEnc->cnt_SID, 1 );
1161 :
1162 30544 : IF( hDtxEnc->var_SID_rate_flag )
1163 : {
1164 0 : test();
1165 0 : test();
1166 0 : test();
1167 :
1168 0 : IF( Opt_AMR_WB != 0 && EQ_16( hDtxEnc->max_SID, 3 ) && EQ_16( hDtxEnc->cnt_SID, 3 ) )
1169 : {
1170 : /* set the size of CNG history buffer for averaging to DTX_HIST_SIZE frames */
1171 : /* be sure that DTX_HIST_SIZE >= INT_L */
1172 0 : hDtxEnc->cng_hist_size = 3;
1173 0 : move16();
1174 : }
1175 0 : test();
1176 : /*else if ( st_fx->max_SID != 3 && st_fx->cnt_SID == DTX_HIST_SIZE )//compile error */
1177 0 : if ( NE_16( hDtxEnc->max_SID, 3 ) && EQ_16( hDtxEnc->cnt_SID, DTX_HIST_SIZE ) )
1178 : {
1179 : /* set the size of CNG history buffer for averaging to 3 frames */
1180 0 : hDtxEnc->cng_hist_size = DTX_HIST_SIZE;
1181 0 : move16();
1182 : }
1183 : }
1184 30544 : test();
1185 30544 : IF( hDtxEnc->var_SID_rate_flag == 0 && GT_16( hDtxEnc->interval_SID, 1 ) )
1186 : {
1187 : /* set the size of CNG history buffer for averaging to interval_SID frames */
1188 30544 : hDtxEnc->cng_hist_size = hDtxEnc->interval_SID;
1189 30544 : move16();
1190 30544 : if ( GT_16( hDtxEnc->cng_hist_size, DTX_HIST_SIZE ) )
1191 : {
1192 0 : hDtxEnc->cng_hist_size = DTX_HIST_SIZE;
1193 0 : move16();
1194 : }
1195 : }
1196 30544 : IF( GE_16( hDtxEnc->cnt_SID, hDtxEnc->max_SID ) )
1197 : {
1198 : /* adaptive SID update interval */
1199 3231 : hDtxEnc->max_SID = hDtxEnc->interval_SID;
1200 3231 : move16();
1201 3231 : hDtxEnc->cnt_SID = 0;
1202 3231 : move16();
1203 : }
1204 : }
1205 :
1206 1132084 : return;
1207 : }
1208 :
1209 :
1210 1510 : void dtx_hangover_control_fx(
1211 : Encoder_State *st_fx, /* i/o: encoder state structure */
1212 : const Word16 lsp_new_fx[M] /* i : current frame LSPs Q15*/
1213 : )
1214 : {
1215 : Word16 ptr;
1216 : Word16 i, j, m;
1217 : Word16 tmp_lsp[/*max(DTX_HIST_SIZE,*/ HO_HIST_SIZE /*)*/ * M];
1218 : Word32 tmp_enr[/*max(DTX_HIST_SIZE,*/ HO_HIST_SIZE /*)*/];
1219 : Word16 tmp[/*max(DTX_HIST_SIZE,*/ HO_HIST_SIZE /*)*/ * M];
1220 : Word16 enr_new;
1221 : Word16 weights;
1222 : Word32 enr_est;
1223 : Word16 enr_est_log;
1224 : Word16 lsp_est[M];
1225 : Word16 Dlsp, Denr;
1226 : Word16 lsf_tmp[M];
1227 : Word32 C[M];
1228 : Word32 max_val[2];
1229 : Word16 max_idx[2];
1230 : Word16 ftmp_fx;
1231 : Word16 Dlsp_n2e, Denr_n2e;
1232 : Word16 exp, fra, exp2, fra2;
1233 : Word16 S_max;
1234 : Word16 S_tmp;
1235 : Word32 L_tmp;
1236 1510 : VAD_HANDLE hVAD = st_fx->hVAD;
1237 1510 : DTX_ENC_HANDLE hDtxEnc = st_fx->hDtxEnc;
1238 1510 : TD_CNG_ENC_HANDLE hTdCngEnc = st_fx->hTdCngEnc;
1239 : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
1240 1510 : Flag Overflow = 0;
1241 : #endif
1242 :
1243 : /* get current frame exc energy in log2 */
1244 1510 : exp = norm_l( hTdCngEnc->ho_ener_circ_fx[hTdCngEnc->ho_circ_ptr] );
1245 1510 : fra = Log2_norm_lc( L_shl( hTdCngEnc->ho_ener_circ_fx[hTdCngEnc->ho_circ_ptr], exp ) );
1246 1510 : exp = sub( sub( 30, exp ), 6 );
1247 1510 : L_tmp = L_Comp( exp, fra ); /*Q16*/
1248 1510 : enr_new = round_fx( L_shl( L_tmp, 8 ) ); /*Q8 */
1249 :
1250 1510 : if ( enr_new < 0 )
1251 : {
1252 247 : enr_new = 0;
1253 247 : move16(); /*Q8 */
1254 : }
1255 :
1256 : /* get energies and lsps of hangover frames */
1257 1510 : ptr = sub( hTdCngEnc->ho_circ_ptr, sub( hTdCngEnc->burst_ho_cnt, 1 ) ); /*Q0*/
1258 1510 : IF( ptr < 0 )
1259 : {
1260 451 : ptr = add( hTdCngEnc->ho_circ_size, ptr ); /*Q0*/
1261 : }
1262 :
1263 5979 : FOR( i = 0; i < hTdCngEnc->burst_ho_cnt - 1; i++ )
1264 : {
1265 4469 : Copy( &( hTdCngEnc->ho_lsp_circ_fx[ptr * M] ), &( tmp_lsp[i * M] ), M ); /*Q15*/
1266 4469 : tmp_enr[i] = hTdCngEnc->ho_ener_circ_fx[ptr]; /*Q6 */
1267 4469 : move32();
1268 :
1269 4469 : ptr = add( ptr, 1 );
1270 4469 : if ( EQ_16( ptr, hTdCngEnc->ho_circ_size ) )
1271 : {
1272 451 : ptr = 0;
1273 451 : move16();
1274 : }
1275 : }
1276 :
1277 : /* get estimated CNG energy and lsps assuming terminate hangover at current frame */
1278 1510 : ptr = sub( hTdCngEnc->burst_ho_cnt, 2 );
1279 1510 : enr_est = Mpy_32_16_1( tmp_enr[ptr], W_DTX_HO_FX[0] ); /*Q6 */
1280 1510 : weights = W_DTX_HO_FX[0]; /*Q15 */
1281 1510 : move16();
1282 1510 : Copy( &( tmp_lsp[ptr * M] ), tmp, M );
1283 1510 : m = 1;
1284 1510 : move16();
1285 :
1286 3394 : FOR( i = 1; i < hTdCngEnc->burst_ho_cnt - 2; i++ )
1287 : {
1288 1884 : test();
1289 1884 : IF( LT_32( Mpy_32_16_1( tmp_enr[ptr - i], ONE_OVER_BUF_H_NRG_FX ), tmp_enr[ptr] ) &&
1290 : GT_32( tmp_enr[ptr - i], Mpy_32_16_1( tmp_enr[ptr], BUF_L_NRG_FX ) ) )
1291 : {
1292 701 : enr_est = L_add( enr_est, Mpy_32_16_1( tmp_enr[ptr - i], W_DTX_HO_FX[i] ) ); /*Q6 */
1293 701 : weights = add( weights, W_DTX_HO_FX[i] ); /*Q15 */
1294 701 : Copy( &tmp_lsp[( ptr - i ) * M], &tmp[m * M], M ); /*Q15*/
1295 701 : m = add( m, 1 );
1296 : }
1297 : }
1298 :
1299 1510 : exp = norm_l( enr_est );
1300 1510 : fra = round_fx_sat( L_shl_sat( enr_est, exp ) ); /*Q6+exp-16*/
1301 1510 : exp2 = norm_s( weights );
1302 1510 : fra2 = shl( weights, exp2 ); /*Q15+exp2*/
1303 1510 : exp = sub( sub( exp, 16 ), exp2 );
1304 1510 : IF( GT_16( fra, fra2 ) )
1305 : {
1306 499 : fra = shr( fra, 1 );
1307 499 : exp = sub( exp, 1 );
1308 : }
1309 1510 : L_tmp = L_deposit_l( div_s( fra, fra2 ) ); /*Q6+exp*/
1310 1510 : enr_est = L_shr( L_tmp, exp ); /*Q6 */
1311 :
1312 1510 : if ( LT_32( enr_est, 64 ) )
1313 : {
1314 259 : enr_est = 64; /*Q6 */
1315 259 : move16();
1316 : }
1317 :
1318 1510 : exp = norm_l( enr_est );
1319 1510 : fra = Log2_norm_lc( L_shl( enr_est, exp ) );
1320 1510 : exp = sub( sub( 30, exp ), 6 );
1321 1510 : L_tmp = L_Comp( exp, fra ); /*Q16*/
1322 1510 : enr_est_log = round_fx( L_shl( L_tmp, 8 ) ); /*Q8 */
1323 1510 : Denr_n2e = abs_s( sub( enr_new, enr_est_log ) ); /*Q8 */
1324 :
1325 1510 : IF( LT_16( m, 3 ) )
1326 : {
1327 1327 : enr_est = L_add( Mpy_32_16_1( enr_est, 26214 ), Mpy_32_16_1( hTdCngEnc->ho_ener_circ_fx[hTdCngEnc->ho_circ_ptr], 6554 ) ); /*Q6 */
1328 : }
1329 : ELSE
1330 : {
1331 183 : enr_est = L_add( Mpy_32_16_1( enr_est, 31130 ), Mpy_32_16_1( hTdCngEnc->ho_ener_circ_fx[hTdCngEnc->ho_circ_ptr], 1638 ) ); /*Q6 */
1332 : }
1333 :
1334 1510 : exp = norm_l( enr_est );
1335 1510 : fra = Log2_norm_lc( L_shl( enr_est, exp ) );
1336 1510 : exp = sub( sub( 30, exp ), 6 );
1337 1510 : L_tmp = L_Comp( exp, fra ); /*Q16*/
1338 1510 : enr_est_log = round_fx( L_shl( L_tmp, 8 ) ); /*Q8 */
1339 :
1340 1510 : if ( enr_est_log < 0 )
1341 : {
1342 262 : enr_est_log = 0;
1343 262 : move16();
1344 : }
1345 :
1346 1510 : set32_fx( max_val, 0, 2 );
1347 1510 : set16_fx( max_idx, 0, 2 );
1348 :
1349 3721 : FOR( i = 0; i < m; i++ )
1350 : {
1351 2211 : IF( EQ_16( st_fx->L_frame, L_FRAME ) )
1352 : {
1353 1093 : lsp2lsf_fx( &tmp[i * M], lsf_tmp, M, INT_FS_FX );
1354 1093 : ftmp_fx = 964; /*6400.0f / ( M + 1 ) * 2.56*/
1355 1093 : move16(); /*QX2.56 */
1356 1093 : S_tmp = sub( 16384, add( lsf_tmp[M - 1], ftmp_fx ) ); /*QX2.56 */
1357 1093 : C[i] = L_mult0( S_tmp, S_tmp ); /*QX6.5536 */
1358 : }
1359 : ELSE
1360 : {
1361 1118 : lsp2lsf_fx( &tmp[i * M], lsf_tmp, M, INT_FS_16k );
1362 1118 : ftmp_fx = 1205; /*8000.0f/ ( M + 1 ) * 2.56*/
1363 1118 : move16(); /*QX2.56 */
1364 1118 : S_tmp = sub( 20480, add( lsf_tmp[M - 1], ftmp_fx ) ); /*QX2.56 */
1365 1118 : C[i] = L_mult0( S_tmp, S_tmp ); /*QX6.5536 */
1366 : }
1367 :
1368 2211 : S_tmp = sub( lsf_tmp[0], ftmp_fx ); /*QX2.56 */
1369 2211 : C[i] = L_mac0( C[i], S_tmp, S_tmp ); /*QX6.5536 */
1370 35376 : FOR( j = 0; j < M - 1; j++ )
1371 : {
1372 33165 : S_tmp = sub( sub( lsf_tmp[j + 1], lsf_tmp[j] ), ftmp_fx ); /*QX2.56 */
1373 33165 : C[i] = L_mac0( C[i], S_tmp, S_tmp ); /*QX6.5536 */
1374 : }
1375 :
1376 2211 : C[i] = Mpy_32_16_1( C[i], 1928 /*Q15*/ ); /*QX6.5536 */
1377 :
1378 2211 : IF( GT_32( C[i], max_val[0] ) )
1379 : {
1380 1803 : max_val[1] = max_val[0]; /*x6.5536*/
1381 1803 : move16();
1382 1803 : max_idx[1] = max_idx[0]; /*Q0*/
1383 1803 : move16();
1384 1803 : max_val[0] = C[i]; /*x6.5536*/
1385 1803 : move16();
1386 1803 : max_idx[0] = i;
1387 1803 : move16();
1388 : }
1389 408 : ELSE IF( GT_32( C[i], max_val[1] ) )
1390 : {
1391 264 : max_val[1] = C[i]; /*x6.5536*/
1392 264 : move16();
1393 264 : max_idx[1] = i;
1394 264 : move16();
1395 : }
1396 : }
1397 :
1398 1510 : IF( EQ_16( m, 1 ) )
1399 : {
1400 1109 : Copy( tmp, lsp_est, M ); /*Q15*/
1401 : }
1402 401 : ELSE IF( LT_16( m, 4 ) )
1403 : {
1404 5559 : FOR( i = 0; i < M; i++ )
1405 : {
1406 5232 : lsp_est[i] = 0; /*Q15 */
1407 5232 : move16();
1408 17440 : FOR( j = 0; j < m; j++ )
1409 : {
1410 12208 : lsp_est[i] = add_o( lsp_est[i], tmp[j * M + i], &Overflow ); /*Q15 */
1411 : }
1412 :
1413 5232 : lsp_est[i] = sub( lsp_est[i], tmp[max_idx[0] * M + i] ); /*Q15 */
1414 5232 : S_tmp = div_s( 1, sub( m, 1 ) ); /*Q15 */
1415 5232 : lsp_est[i] = mult_r( lsp_est[i], S_tmp ); /*Q15 */
1416 : }
1417 : }
1418 : ELSE
1419 : {
1420 1258 : FOR( i = 0; i < M; i++ )
1421 : {
1422 1184 : lsp_est[i] = 0; /*Q15 */
1423 1184 : move16();
1424 6608 : FOR( j = 0; j < m; j++ )
1425 : {
1426 5424 : lsp_est[i] = add_o( lsp_est[i], tmp[j * M + i], &Overflow ); /*Q15 */
1427 : }
1428 :
1429 1184 : lsp_est[i] = sub_o( lsp_est[i], add_o( tmp[max_idx[0] * M + i], tmp[max_idx[1] * M + i], &Overflow ), &Overflow ); /*Q15 */
1430 1184 : S_tmp = div_s( 1, sub( m, 2 ) ); /*Q15 */
1431 1184 : lsp_est[i] = mult_r( lsp_est[i], S_tmp ); /*Q15 */
1432 : }
1433 : }
1434 :
1435 1510 : Dlsp_n2e = 0; /*Q15 */
1436 1510 : move16();
1437 25670 : FOR( i = 0; i < M; i++ )
1438 : {
1439 24160 : Dlsp_n2e = add_o( Dlsp_n2e, abs_s( sub( lsp_new_fx[i], lsp_est[i] ) ), &Overflow ); /*Q15 */
1440 24160 : lsp_est[i] = add( mult_r( 26214, lsp_est[i] ), mult_r( 6554, lsp_new_fx[i] ) ); /*Q15 */
1441 : }
1442 :
1443 : /* get deviation of CNG parameters between newly estimated and current state memory */
1444 1510 : Dlsp = 0;
1445 1510 : move16();
1446 1510 : S_max = 0;
1447 1510 : move16();
1448 :
1449 25670 : FOR( i = 0; i < M; i++ )
1450 : {
1451 24160 : S_tmp = abs_s( sub( hDtxEnc->lspCNG_fx[i], lsp_est[i] ) ); /*Q15 */
1452 24160 : Dlsp = add_o( Dlsp, S_tmp, &Overflow ); /*Q15 */
1453 24160 : IF( GT_16( S_tmp, S_max ) )
1454 : {
1455 6918 : S_max = S_tmp; /*Q15 */
1456 : }
1457 : }
1458 :
1459 1510 : exp = norm_l( hTdCngEnc->lp_ener_fx );
1460 1510 : fra = Log2_norm_lc( L_shl( hTdCngEnc->lp_ener_fx, exp ) );
1461 1510 : exp = sub( sub( 30, exp ), 6 );
1462 1510 : L_tmp = L_Comp( exp, fra ); /*Q16*/
1463 1510 : S_tmp = round_fx( L_shl( L_tmp, 8 ) ); /*Q8 */
1464 1510 : Denr = abs_s( sub( S_tmp, enr_est_log ) ); /*Q8 */
1465 :
1466 : /* make decision if DTX hangover can be terminated */
1467 1510 : hVAD->hangover_terminate_flag = 0; /*Q0 */
1468 1510 : move16();
1469 :
1470 1510 : test();
1471 1510 : test();
1472 1510 : test();
1473 1510 : test();
1474 1510 : test();
1475 1510 : test();
1476 1510 : test();
1477 1510 : test();
1478 1510 : test();
1479 1510 : test();
1480 1510 : test();
1481 1510 : IF( ( LT_16( Dlsp, 13107 ) && LT_16( Denr, 359 ) && LT_16( S_max, 3277 ) && LT_16( Dlsp_n2e, 13107 ) && LT_16( Denr_n2e, 308 ) && st_fx->Opt_SC_VBR == 1 ) ||
1482 : ( LT_16( Dlsp, 13107 ) && LT_16( Denr, 205 ) && LT_16( S_max, 3277 ) && LT_16( Dlsp_n2e, 13107 ) && LT_16( Denr_n2e, 205 ) && st_fx->Opt_SC_VBR == 0 ) )
1483 :
1484 : {
1485 67 : hVAD->hangover_terminate_flag = 1; /*Q0 */
1486 67 : move16();
1487 : }
1488 :
1489 1510 : return;
1490 : }
1491 :
1492 : /*-------------------------------------------------------------------*
1493 : * td_cng_enc_init_fx()
1494 : *
1495 : *
1496 : *-------------------------------------------------------------------*/
1497 :
1498 3 : void td_cng_enc_init_fx(
1499 : TD_CNG_ENC_HANDLE hTdCngEnc, /* i/o: DTX/TD CNG data handle */
1500 : const Word16 Opt_DTX_ON, /* i : flag indicating DTX operation Q0*/
1501 : const Word16 max_bwidth /* i : maximum encoded bandwidth Q0*/
1502 : )
1503 : {
1504 :
1505 3 : hTdCngEnc->cng_seed = RANDOM_INITSEED;
1506 3 : hTdCngEnc->cng_ener_seed = RANDOM_INITSEED;
1507 3 : hTdCngEnc->cng_ener_seed1 = RANDOM_INITSEED;
1508 3 : hTdCngEnc->lp_ener_fx = 0;
1509 3 : hTdCngEnc->old_enr_index = -1;
1510 3 : hTdCngEnc->Enew_fx = 0;
1511 :
1512 3 : hTdCngEnc->lp_sp_enr_fx = 0;
1513 3 : hTdCngEnc->last_allow_cn_step = 0;
1514 3 : move16();
1515 3 : move32();
1516 3 : move16();
1517 3 : move16();
1518 3 : move32();
1519 3 : move16();
1520 3 : move16();
1521 :
1522 3 : IF( Opt_DTX_ON )
1523 : {
1524 0 : hTdCngEnc->cng_hist_ptr = -1;
1525 0 : move16();
1526 0 : set16_fx( hTdCngEnc->cng_lsp_hist_fx, 0, DTX_HIST_SIZE * M );
1527 0 : set16_fx( hTdCngEnc->cng_ener_hist_fx, 0, DTX_HIST_SIZE );
1528 :
1529 0 : hTdCngEnc->ho_hist_ptr = -1;
1530 0 : move16();
1531 0 : hTdCngEnc->ho_sid_bw = L_deposit_l( 0 );
1532 0 : set16_fx( hTdCngEnc->ho_lsp_hist_fx, 0, HO_HIST_SIZE * M );
1533 0 : set32_fx( hTdCngEnc->ho_ener_hist_fx, 0, HO_HIST_SIZE );
1534 0 : set32_fx( hTdCngEnc->ho_env_hist_fx, 0, HO_HIST_SIZE * NUM_ENV_CNG );
1535 0 : hTdCngEnc->ho_hist_size = 0;
1536 0 : move16();
1537 0 : hTdCngEnc->act_cnt = 0;
1538 0 : move16();
1539 : }
1540 :
1541 3 : set16_fx( hTdCngEnc->ho_16k_lsp, 0, HO_HIST_SIZE );
1542 3 : hTdCngEnc->act_cnt2 = 0;
1543 3 : hTdCngEnc->num_ho = 0;
1544 3 : move16();
1545 3 : move16();
1546 :
1547 3 : hTdCngEnc->ho_circ_ptr = -1;
1548 3 : move16();
1549 3 : set16_fx( hTdCngEnc->ho_lsp_circ_fx, 0, HO_HIST_SIZE * M );
1550 3 : set32_fx( hTdCngEnc->ho_ener_circ_fx, 0, HO_HIST_SIZE );
1551 3 : set32_fx( hTdCngEnc->ho_env_circ_fx, 0, HO_HIST_SIZE * NUM_ENV_CNG );
1552 3 : hTdCngEnc->ho_circ_size = 0;
1553 3 : hTdCngEnc->burst_ho_cnt = 0;
1554 3 : move16();
1555 3 : move16();
1556 :
1557 3 : hTdCngEnc->cng_buf_cnt = 0;
1558 :
1559 :
1560 3 : set32_fx( hTdCngEnc->lp_env_fx, 0, 20 );
1561 3 : set32_fx( hTdCngEnc->cng_res_env_fx, 0, 20 * 8 );
1562 3 : set16_fx( hTdCngEnc->exc_mem_fx, 0, 24 );
1563 3 : set16_fx( hTdCngEnc->exc_mem1_fx, 0, 30 );
1564 3 : set16_fx( hTdCngEnc->exc_mem2_fx, 0, 30 );
1565 3 : set32_fx( hTdCngEnc->old_env_fx, 0, NUM_ENV_CNG );
1566 :
1567 : /* SWB CNG/DTX */
1568 3 : hTdCngEnc->last_wb_cng_ener_fx = -1541; /* Q8 */
1569 3 : move16();
1570 3 : hTdCngEnc->last_shb_cng_ener_fx = -1541; /* Q8 */
1571 3 : move16();
1572 3 : hTdCngEnc->mov_wb_cng_ener_fx = -1541; /* Q8 */
1573 3 : move16();
1574 3 : hTdCngEnc->mov_shb_cng_ener_fx = -1541; /* Q8 */
1575 3 : move16();
1576 3 : hTdCngEnc->shb_cng_ini_cnt = 1;
1577 3 : move16();
1578 3 : hTdCngEnc->shb_NO_DATA_cnt = 0;
1579 3 : move16();
1580 3 : hTdCngEnc->last_SID_bwidth = s_min( max_bwidth, SWB );
1581 3 : hTdCngEnc->last_vad = 0;
1582 3 : move16();
1583 :
1584 3 : return;
1585 : }
1586 :
1587 148 : void td_cng_enc_init_ivas_fx(
1588 : TD_CNG_ENC_HANDLE hTdCngEnc, /* i/o: DTX/TD CNG data handle */
1589 : const Word16 Opt_DTX_ON, /* i : flag indicating DTX operation Q0*/
1590 : const Word16 max_bwidth /* i : maximum encoded bandwidth Q0*/
1591 : )
1592 : {
1593 :
1594 148 : hTdCngEnc->cng_seed = RANDOM_INITSEED;
1595 148 : move16();
1596 148 : hTdCngEnc->cng_ener_seed = RANDOM_INITSEED;
1597 148 : move16();
1598 148 : hTdCngEnc->cng_ener_seed1 = RANDOM_INITSEED;
1599 148 : move16();
1600 148 : hTdCngEnc->lp_ener_fx = 0;
1601 148 : move16();
1602 148 : hTdCngEnc->old_enr_index = -1;
1603 148 : move16();
1604 148 : hTdCngEnc->Enew_fx = 0;
1605 148 : move16();
1606 :
1607 148 : hTdCngEnc->lp_sp_enr_fx = 0;
1608 148 : move16();
1609 148 : hTdCngEnc->last_allow_cn_step = 0;
1610 148 : move16();
1611 :
1612 148 : hTdCngEnc->CNG_att_fx = 0;
1613 148 : move16();
1614 :
1615 148 : IF( Opt_DTX_ON )
1616 : {
1617 148 : hTdCngEnc->cng_hist_ptr = -1;
1618 148 : move16();
1619 148 : set16_fx( hTdCngEnc->cng_lsp_hist_fx, 0, DTX_HIST_SIZE * M );
1620 148 : set16_fx( hTdCngEnc->cng_ener_hist_fx, 0, DTX_HIST_SIZE );
1621 148 : hTdCngEnc->ho_hist_ptr = -1;
1622 148 : move16();
1623 148 : hTdCngEnc->ho_sid_bw = 0;
1624 148 : move32();
1625 148 : set16_fx( hTdCngEnc->ho_lsp_hist_fx, 0, HO_HIST_SIZE * M );
1626 148 : set32_fx( hTdCngEnc->ho_ener_hist_fx, 0, HO_HIST_SIZE );
1627 148 : set32_fx( hTdCngEnc->ho_env_hist_fx, 0, HO_HIST_SIZE * NUM_ENV_CNG );
1628 148 : hTdCngEnc->ho_hist_size = 0;
1629 148 : move16();
1630 148 : hTdCngEnc->act_cnt = 0;
1631 148 : move16();
1632 : }
1633 :
1634 148 : set16_fx( hTdCngEnc->ho_16k_lsp, 0, HO_HIST_SIZE );
1635 148 : hTdCngEnc->act_cnt2 = 0;
1636 148 : move16();
1637 148 : hTdCngEnc->num_ho = 0;
1638 148 : move16();
1639 :
1640 148 : hTdCngEnc->ho_circ_ptr = -1;
1641 148 : move16();
1642 148 : set16_fx( hTdCngEnc->ho_lsp_circ_fx, 0, HO_HIST_SIZE * M );
1643 148 : set32_fx( hTdCngEnc->ho_ener_circ_fx, 0, HO_HIST_SIZE );
1644 148 : set32_fx( hTdCngEnc->ho_env_circ_fx, 0, HO_HIST_SIZE * NUM_ENV_CNG );
1645 148 : hTdCngEnc->ho_circ_size = 0;
1646 148 : move16();
1647 148 : hTdCngEnc->burst_ho_cnt = 0;
1648 148 : move16();
1649 148 : hTdCngEnc->cng_buf_cnt = 0;
1650 148 : move16();
1651 :
1652 148 : set32_fx( hTdCngEnc->lp_env_fx, 0, 20 );
1653 148 : set32_fx( hTdCngEnc->cng_res_env_fx, 0, 20 * 8 );
1654 148 : set16_fx( hTdCngEnc->exc_mem_fx, 0, 24 );
1655 148 : set16_fx( hTdCngEnc->exc_mem1_fx, 0, 30 );
1656 148 : set16_fx( hTdCngEnc->exc_mem2_fx, 0, 30 );
1657 148 : set32_fx( hTdCngEnc->old_env_fx, 0, NUM_ENV_CNG );
1658 148 : set16_fx( hTdCngEnc->cng_exc2_buf, 0, HO_HIST_SIZE * L_FFT );
1659 148 : set16_fx( hTdCngEnc->cng_Qexc_buf, Q15, HO_HIST_SIZE );
1660 :
1661 : /* SWB CNG/DTX */
1662 148 : hTdCngEnc->last_wb_cng_ener_fx = -1541; /* Q8 */
1663 148 : move16();
1664 148 : hTdCngEnc->last_shb_cng_ener_fx = -1541; /* Q8 */
1665 148 : move16();
1666 148 : hTdCngEnc->mov_wb_cng_ener_fx = -1541; /* Q8 */
1667 148 : move16();
1668 148 : hTdCngEnc->mov_shb_cng_ener_fx = -1541; /* Q8 */
1669 148 : move16();
1670 148 : hTdCngEnc->shb_cng_ini_cnt = 1;
1671 148 : move16();
1672 148 : hTdCngEnc->shb_NO_DATA_cnt = 0;
1673 148 : move16();
1674 148 : hTdCngEnc->last_SID_bwidth = s_min( max_bwidth, SWB );
1675 148 : move16();
1676 148 : hTdCngEnc->last_vad = 0;
1677 148 : move16();
1678 148 : hTdCngEnc->last_idx_ener = 0;
1679 148 : move16();
1680 :
1681 148 : return;
1682 : }
1683 :
1684 : /*-------------------------------------------------------------------*
1685 : * dtx_enc_init_fx()
1686 : *
1687 : * Initialize DTX parameters
1688 : *-------------------------------------------------------------------*/
1689 :
1690 614 : void dtx_enc_init_fx(
1691 : Encoder_State *st, /* i : Encoder state handle */
1692 : const Word16 var_SID_rate_flag, /* i : flag for variable SID update rate Q0*/
1693 : const Word16 interval_SID /* i : interval for SID update Q0*/
1694 : )
1695 : {
1696 : DTX_ENC_HANDLE hDtxEnc;
1697 614 : hDtxEnc = st->hDtxEnc;
1698 :
1699 614 : hDtxEnc->first_CNG = 0;
1700 614 : move16();
1701 614 : hDtxEnc->cnt_SID = 0;
1702 614 : move16();
1703 614 : hDtxEnc->max_SID = 2;
1704 614 : move16();
1705 614 : hDtxEnc->CNG_mode = -1;
1706 614 : move16();
1707 614 : Copy( st->lsp_old1_fx, hDtxEnc->lspCNG_fx, M );
1708 614 : hDtxEnc->VarDTX_cnt_voiced = 0;
1709 614 : move16();
1710 614 : hDtxEnc->VarDTX_cnt_noise = 0;
1711 614 : move16();
1712 614 : hDtxEnc->lt_ener_voiced_fx = 0;
1713 614 : move32();
1714 614 : hDtxEnc->lt_ener_noise_fx = 0;
1715 614 : move32();
1716 614 : hDtxEnc->frame_ener_fx = 0;
1717 614 : move32();
1718 614 : hDtxEnc->lt_ener_last_SID_fx = 0;
1719 614 : move32();
1720 614 : hDtxEnc->last_CNG_L_frame = L_FRAME;
1721 614 : move16();
1722 614 : hDtxEnc->var_SID_rate_flag = var_SID_rate_flag; /*Q0*/
1723 614 : move16();
1724 614 : hDtxEnc->last_active_brate = ACELP_7k20;
1725 614 : move32();
1726 614 : hDtxEnc->cng_cnt = 0;
1727 614 : move16();
1728 :
1729 614 : IF( hDtxEnc->var_SID_rate_flag )
1730 : {
1731 3 : hDtxEnc->interval_SID = 12;
1732 3 : move16();
1733 3 : hDtxEnc->cng_hist_size = DTX_HIST_SIZE;
1734 3 : move16();
1735 : }
1736 : ELSE
1737 : {
1738 611 : hDtxEnc->interval_SID = interval_SID;
1739 611 : move16();
1740 611 : if ( GE_16( hDtxEnc->interval_SID, DTX_HIST_SIZE ) )
1741 : {
1742 611 : hDtxEnc->cng_hist_size = hDtxEnc->interval_SID;
1743 611 : move16();
1744 : }
1745 : }
1746 614 : return;
1747 : }
|