Line data Source code
1 : /*====================================================================================
2 : EVS Codec 3GPP TS26.452 Aug 12, 2021. Version 16.3.0
3 : ====================================================================================*/
4 :
5 : #include <stdint.h>
6 : #include "options.h" /* Compilation switches */
7 : #include "cnst.h" /* Common constants */
8 : #include "rom_com.h"
9 : #include "prot_fx.h" /* Function prototypes */
10 : #include "prot_fx_enc.h" /* Function prototypes */
11 :
12 : /*---------------------------------------------------------------------*
13 : * Local constants
14 : *---------------------------------------------------------------------*/
15 :
16 : #define K_COR_FX 23405 /* Q13 2.857 */
17 : #define C_COR_FX -10535 /* Q13 -1.286 */
18 :
19 : #define K_EE_FX 1365 /* Q15 0.04167 */
20 : #define C_EE_FX 0
21 :
22 : #define K_ZC_FX -1311 /* Q15 -0.04 */
23 : #define C_ZC_FX 19661 /* Q13 2.4 */
24 :
25 : #define K_RELE_FX 1638 /* Q15 0.05 */
26 : #define C_RELE_FX 14746 /* Q15 0.45 */
27 :
28 : #define K_PC_FX -2341 /* Q15 -0.07143*/
29 : #define C_PC_FX 30425 /* Q1 1.857 */
30 :
31 : #define K_SNR_FX 3541 /* Q15 .1111 */
32 : #define C_SNR_FX -10921 /* Q15 -0.3333f */
33 :
34 :
35 : #define THRES_EEN 514206 /* 251.077 => (10^(1/(K_EE*10))) Q11*/
36 :
37 : /*-------------------------------------------------------------------*
38 : * signal_clas_fx()
39 : *
40 : * classification state machine for FEC
41 : * TC frames selection
42 : *-------------------------------------------------------------------*/
43 :
44 3100 : Word16 signal_clas_fx( /* o : classification for current frames */
45 : Encoder_State *st, /* i/o: encoder state structure */
46 : const Word16 *speech, /* i : pointer to speech signal for E computation in Qx */
47 : const Word32 *ee, /* i : lf/hf E ration for 2 half-frames in Q6 */
48 : const Word16 relE, /* i : frame relative E to the long term average in Q8 */
49 : const Word16 L_look, /* i : look-ahead */
50 : Word16 *clas_mod /* o : class flag for NOOP detection */
51 : )
52 : {
53 : Word32 Ltmp;
54 : Word16 mean_voi2, een, corn, zcn, relEn, pcn, fmerit1;
55 : Word16 i, clas, pc, zc, lo, lo2, hi, hi2, exp_ee, frac_ee;
56 : Word16 tmp16, tmpS;
57 : const Word16 *pt1;
58 : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
59 3100 : Flag Overflow = 0;
60 3100 : move32();
61 : #endif
62 :
63 : /*----------------------------------------------------------------*
64 : * Calculate average voicing
65 : * Calculate average spectral tilt
66 : * Calculate zero-crossing rate
67 : * Calculate pitch stability
68 : *----------------------------------------------------------------*/
69 :
70 : /* average voicing on second half-frame and look-ahead */
71 3100 : Ltmp = L_mult( st->voicing_fx[1], 16384 ); /* Q15*Q14->Q30 */
72 3100 : mean_voi2 = mac_r( Ltmp, st->voicing_fx[2], 16384 );
73 :
74 : /* average spectral tilt in dB */
75 3100 : lo = L_Extract_lc( ee[0], &hi );
76 3100 : lo2 = L_Extract_lc( ee[1], &hi2 );
77 3100 : Ltmp = L_mult( lo, lo2 ); /* Q5*Q5->Q11 */
78 :
79 3100 : test();
80 3100 : test();
81 3100 : IF( LT_32( Ltmp, 2048 ) )
82 : {
83 464 : een = 0;
84 464 : move16();
85 : }
86 2636 : ELSE IF( GT_32( Ltmp, THRES_EEN ) || hi > 0 || hi2 > 0 )
87 : {
88 2150 : een = 512;
89 2150 : move16();
90 : }
91 : ELSE
92 : {
93 : /* mean_ee2 = 0.5f * 20.0f * (float)log10( tmp ); */
94 : /* een = K_EE_ENC * mean_ee2 + C_EE_ENC; */
95 486 : exp_ee = norm_l( Ltmp );
96 486 : frac_ee = Log2_norm_lc( L_shl( Ltmp, exp_ee ) );
97 486 : exp_ee = sub( 30 - 11, exp_ee );
98 486 : Ltmp = Mpy_32_16( exp_ee, frac_ee, LG10 ); /* Ltmp Q14 */
99 486 : een = round_fx( L_shl( Ltmp, 16 - 5 ) ); /* Q14 -> Q9 */
100 486 : een = mac_r( C_EE_FX, een, K_EE_FX );
101 : }
102 : /* compute zero crossing rate */
103 3100 : pt1 = speech + sub( L_look, 1 );
104 3100 : tmpS = shr( *pt1, 15 ); /* sets 'tmpS to -1 if *pt1 < 0 */
105 3100 : Ltmp = L_deposit_l( 0 );
106 796700 : FOR( i = 0; i < L_FRAME; i++ )
107 : {
108 793600 : tmp16 = add( 1, tmpS );
109 793600 : pt1++;
110 793600 : tmpS = shr( *pt1, 15 ); /* pt1 >=0 ---> 0 OTHERWISE -1 */
111 793600 : Ltmp = L_msu0( Ltmp, tmpS, tmp16 );
112 : }
113 3100 : zc = extract_l( Ltmp );
114 :
115 : /* compute pitch stability */
116 3100 : pc = add( abs_s( sub( st->pitch[1], st->pitch[0] ) ), abs_s( sub( st->pitch[2], st->pitch[1] ) ) );
117 3100 : st->tdm_pc = pc;
118 3100 : move16();
119 : /*-----------------------------------------------------------------*
120 : * Transform parameters to the range <0:1>
121 : * Compute the merit function
122 : *-----------------------------------------------------------------*/
123 :
124 : /* corn = K_COR * mean_voi2 + C_COR */
125 3100 : Ltmp = L_mult( C_COR_FX, 32767 );
126 3100 : corn = round_fx( L_shl( L_mac( Ltmp, mean_voi2, K_COR_FX ), -4 ) ); /*Q13+Q13*Q15 =>Q13->Q9*/
127 : /* Limit [0, 1] */
128 3100 : corn = s_max( corn, 0 );
129 3100 : corn = s_min( corn, 512 );
130 :
131 3100 : Ltmp = L_mult( C_ZC_FX, 4 ); /*Q13*Q2 -> Q16*/
132 3100 : zcn = round_fx( L_shl( L_mac( Ltmp, zc, K_ZC_FX ), 16 - 7 ) ); /*Q0*Q15 + Q16*/
133 : /* Limit [0, 1] */
134 3100 : zcn = s_max( zcn, 0 );
135 3100 : zcn = s_min( zcn, 512 );
136 :
137 3100 : Ltmp = L_mult( C_RELE_FX, 256 ); /*Q15*Q8 ->Q24*/
138 3100 : relEn = round_fx( L_shl( L_mac( Ltmp, relE, K_RELE_FX ), 1 ) ); /*relE in Q8 but relEn in Q9*/
139 : /* Limit [0.5, 1] */
140 3100 : relEn = s_max( relEn, 256 );
141 3100 : relEn = s_min( relEn, 512 );
142 :
143 3100 : Ltmp = L_mult( C_PC_FX, 2 ); /*Q14*Q1 -> Q16*/
144 3100 : pcn = round_fx( L_shl( L_mac( Ltmp, pc, K_PC_FX ), 16 - 7 ) ); /*Q16 + Q0*Q15*/
145 : /* Limit [0, 1] */
146 3100 : pcn = s_max( pcn, 0 );
147 3100 : pcn = s_min( pcn, 512 );
148 :
149 3100 : Ltmp = L_mult( een, 10923 );
150 3100 : Ltmp = L_mac( Ltmp, corn, 21845 );
151 3100 : Ltmp = L_mac( Ltmp, zcn, 10923 );
152 3100 : Ltmp = L_mac( Ltmp, relEn, 10923 );
153 3100 : Ltmp = L_mac( Ltmp, pcn, 10923 );
154 :
155 3100 : fmerit1 = round_fx_o( L_shl_o( Ltmp, 16 - 10 - 1, &Overflow ), &Overflow ); /* fmerit1 ->Q15 */
156 :
157 : /*-----------------------------------------------------------------*
158 : * FEC classification
159 : *-----------------------------------------------------------------*/
160 :
161 3100 : st->fmerit_dt = sub( st->prev_fmerit, fmerit1 ); /*Q15*/
162 3100 : move16();
163 3100 : st->prev_fmerit = fmerit1;
164 3100 : move16();
165 :
166 : /* FEC classification */
167 3100 : test();
168 3100 : test();
169 3100 : IF( st->localVAD == 0 || EQ_16( st->coder_type, UNVOICED ) || LT_16( relE, -1536 ) )
170 : {
171 1401 : clas = UNVOICED_CLAS;
172 1401 : *clas_mod = clas;
173 1401 : move16();
174 1401 : move16();
175 : }
176 : ELSE
177 : {
178 1699 : SWITCH( st->last_clas )
179 : {
180 1430 : case VOICED_CLAS:
181 : case ONSET:
182 : case VOICED_TRANSITION:
183 :
184 1430 : IF( LT_16( fmerit1, 16056 ) ) /*0.49f*/
185 : {
186 25 : clas = UNVOICED_CLAS;
187 25 : move16();
188 : }
189 1405 : ELSE IF( LT_16( fmerit1, 21626 ) ) /*0.66*/
190 : {
191 127 : clas = VOICED_TRANSITION;
192 127 : move16();
193 : }
194 : ELSE
195 : {
196 1278 : clas = VOICED_CLAS;
197 1278 : move16();
198 : }
199 1430 : IF( LT_16( fmerit1, 14745 /* 0.45f*/ ) )
200 : {
201 18 : *clas_mod = UNVOICED_CLAS;
202 18 : move16();
203 : }
204 1412 : ELSE IF( LT_16( fmerit1, 21626 /* 0.66f*/ ) )
205 : {
206 134 : *clas_mod = VOICED_TRANSITION;
207 134 : move16();
208 : }
209 : ELSE
210 : {
211 1278 : *clas_mod = VOICED_CLAS;
212 1278 : move16();
213 : }
214 1430 : BREAK;
215 :
216 269 : case UNVOICED_CLAS:
217 : case UNVOICED_TRANSITION:
218 269 : IF( GT_16( fmerit1, 20643 ) ) /*0.63*/
219 : {
220 135 : clas = ONSET;
221 135 : move16();
222 : }
223 134 : ELSE IF( GT_16( fmerit1, 19169 ) ) /*0.585*/
224 : {
225 40 : clas = UNVOICED_TRANSITION;
226 40 : move16();
227 : }
228 : ELSE
229 : {
230 94 : clas = UNVOICED_CLAS;
231 94 : move16();
232 : }
233 269 : *clas_mod = clas;
234 269 : move16();
235 :
236 269 : BREAK;
237 :
238 0 : default:
239 0 : clas = UNVOICED_CLAS;
240 0 : *clas_mod = clas;
241 0 : move16();
242 0 : move16();
243 0 : BREAK;
244 : }
245 : }
246 : /* Onset classification */
247 :
248 : /* tc_cnt == -1: frame after TC frame in continuous block of GC/VC frames */
249 : /* tc_cnt == 0: UC frame */
250 : /* tc_cnt == 1: onset/transition frame, coded by GC coder type */
251 : /* tc_cnt == 2: frame after onset/transition frame, coded by TC coder type */
252 :
253 3100 : if ( clas == 0 )
254 : {
255 1520 : st->tc_cnt = 0;
256 1520 : move16();
257 : }
258 :
259 3100 : test();
260 3100 : IF( GE_16( clas, VOICED_TRANSITION ) && st->tc_cnt >= 0 )
261 : {
262 384 : st->tc_cnt = add( st->tc_cnt, 1 );
263 384 : move16();
264 : }
265 :
266 3100 : if ( GT_16( st->tc_cnt, 2 ) )
267 : {
268 124 : st->tc_cnt = -1;
269 124 : move16();
270 : }
271 3100 : return clas;
272 : }
273 :
274 1136964 : Word16 signal_clas_ivas_fx( /* o : classification for current frames */
275 : Encoder_State *st, /* i/o: encoder state structure */
276 : const Word16 *speech, /* i : pointer to speech signal for E computation in Qx */
277 : const Word32 *ee, /* i : lf/hf E ration for 2 half-frames in Q6 */
278 : const Word16 relE, /* i : frame relative E to the long term average in Q8 */
279 : const Word16 L_look, /* i : look-ahead */
280 : Word16 *clas_mod /* o : class flag for NOOP detection */
281 : )
282 : {
283 : Word32 Ltmp;
284 : Word16 mean_voi2, een, corn, zcn, relEn, pcn, fmerit1;
285 : Word16 i, clas, pc, zc, exp_ee;
286 : Word16 tmp16, tmpS;
287 : const Word16 *pt1;
288 : Word64 tmp64;
289 : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
290 1136964 : Flag Overflow = 0;
291 1136964 : move32();
292 : #endif
293 :
294 : /*----------------------------------------------------------------*
295 : * Calculate average voicing
296 : * Calculate average spectral tilt
297 : * Calculate zero-crossing rate
298 : * Calculate pitch stability
299 : *----------------------------------------------------------------*/
300 :
301 : /* average voicing on second half-frame and look-ahead */
302 1136964 : Ltmp = L_mult( st->voicing_fx[1], 16384 ); /* Q15*Q14->Q30 */
303 1136964 : mean_voi2 = mac_r( Ltmp, st->voicing_fx[2], 16384 );
304 :
305 : /* average spectral tilt in dB */
306 1136964 : tmp64 = W_mult0_32_32( ee[0], ee[1] );
307 1136964 : exp_ee = W_norm( tmp64 );
308 1136964 : Ltmp = W_extract_h( W_shl( tmp64, exp_ee ) ); // Q = Q6+Q6 + exp_ee - 32
309 1136964 : exp_ee = sub( 31, sub( add( Q12, exp_ee ), 32 ) );
310 1136964 : IF( EQ_16( BASOP_Util_Cmp_Mant32Exp( Ltmp, exp_ee, ONE_IN_Q31, 0 ), -1 ) )
311 : {
312 110406 : een = 0;
313 110406 : move16();
314 : }
315 : ELSE
316 : {
317 : /* mean_ee2 = 0.5f * 20.0f * (float)log10( tmp ); */
318 : /* een = K_EE_ENC * mean_ee2 + C_EE_ENC; */
319 1026558 : Ltmp = BASOP_Util_Log10( Ltmp, exp_ee ); // Q25
320 1026558 : Ltmp = Mpy_32_32( Ltmp, 671088640 /*20.f in Q25*/ ); // Q25 + Q25 -Q31 = Q19 * 0.5 = Q20
321 1026558 : een = extract_l( L_shl( Mpy_32_16_1( Ltmp, K_EE_FX ), Q9 - Q20 ) ); // Q9
322 :
323 1026558 : IF( GT_16( een, 512 ) )
324 : {
325 734592 : een = 512;
326 734592 : move16();
327 : }
328 291966 : ELSE IF( een < 0 )
329 : {
330 0 : een = 0;
331 0 : move16();
332 : }
333 : }
334 : /* compute zero crossing rate */
335 1136964 : pt1 = speech + sub( L_look, 1 );
336 1136964 : tmpS = shr( *pt1, 15 ); /* sets 'tmpS to -1 if *pt1 < 0 */
337 1136964 : Ltmp = L_deposit_l( 0 );
338 292199748 : FOR( i = 0; i < L_FRAME; i++ )
339 : {
340 291062784 : tmp16 = add( 1, tmpS );
341 291062784 : pt1++;
342 291062784 : tmpS = shr( *pt1, 15 ); /* pt1 >=0 ---> 0 OTHERWISE -1 */
343 291062784 : Ltmp = L_msu0( Ltmp, tmpS, tmp16 );
344 : }
345 1136964 : zc = extract_l( Ltmp );
346 :
347 : /* compute pitch stability */
348 1136964 : pc = add( abs_s( sub( st->pitch[1], st->pitch[0] ) ), abs_s( sub( st->pitch[2], st->pitch[1] ) ) );
349 1136964 : st->tdm_pc = pc;
350 1136964 : move16();
351 : /*-----------------------------------------------------------------*
352 : * Transform parameters to the range <0:1>
353 : * Compute the merit function
354 : *-----------------------------------------------------------------*/
355 :
356 : /* corn = K_COR * mean_voi2 + C_COR */
357 1136964 : Ltmp = L_mult( C_COR_FX, 32767 );
358 1136964 : corn = round_fx( L_shl( L_mac( Ltmp, mean_voi2, K_COR_FX ), -4 ) ); /*Q13+Q13*Q15 =>Q13->Q9*/
359 : /* Limit [0, 1] */
360 1136964 : corn = s_max( corn, 0 );
361 1136964 : corn = s_min( corn, 512 );
362 :
363 1136964 : Ltmp = L_mult( C_ZC_FX, 4 ); /*Q13*Q2 -> Q16*/
364 1136964 : zcn = round_fx( L_shl( L_mac( Ltmp, zc, K_ZC_FX ), 16 - 7 ) ); /*Q0*Q15 + Q16*/
365 : /* Limit [0, 1] */
366 1136964 : zcn = s_max( zcn, 0 );
367 1136964 : zcn = s_min( zcn, 512 );
368 :
369 1136964 : Ltmp = L_mult( C_RELE_FX, 256 ); /*Q15*Q8 ->Q24*/
370 1136964 : relEn = round_fx( L_shl( L_mac( Ltmp, relE, K_RELE_FX ), 1 ) ); /*relE in Q8 but relEn in Q9*/
371 : /* Limit [0.5, 1] */
372 1136964 : relEn = s_max( relEn, 256 );
373 1136964 : relEn = s_min( relEn, 512 );
374 :
375 1136964 : Ltmp = L_mult( C_PC_FX, 2 ); /*Q14*Q1 -> Q16*/
376 1136964 : pcn = round_fx( L_shl( L_mac( Ltmp, pc, K_PC_FX ), 16 - 7 ) ); /*Q16 + Q0*Q15*/
377 : /* Limit [0, 1] */
378 1136964 : pcn = s_max( pcn, 0 );
379 1136964 : pcn = s_min( pcn, 512 );
380 :
381 1136964 : Ltmp = L_mult( een, 10923 );
382 1136964 : Ltmp = L_mac( Ltmp, corn, 21845 );
383 1136964 : Ltmp = L_mac( Ltmp, zcn, 10923 );
384 1136964 : Ltmp = L_mac( Ltmp, relEn, 10923 );
385 1136964 : Ltmp = L_mac( Ltmp, pcn, 10923 );
386 :
387 1136964 : fmerit1 = round_fx_o( L_shl_o( Ltmp, 16 - 10 - 1, &Overflow ), &Overflow ); /* fmerit1 ->Q15 */
388 :
389 : /*-----------------------------------------------------------------*
390 : * FEC classification
391 : *-----------------------------------------------------------------*/
392 :
393 1136964 : st->fmerit_dt = sub( st->prev_fmerit, fmerit1 ); /*Q15*/
394 1136964 : move16();
395 1136964 : st->prev_fmerit = fmerit1;
396 1136964 : move16();
397 :
398 : /* FEC classification */
399 1136964 : test();
400 1136964 : test();
401 1136964 : IF( st->localVAD == 0 || EQ_16( st->coder_type, UNVOICED ) || LT_16( relE, -1536 ) )
402 : {
403 543510 : clas = UNVOICED_CLAS;
404 543510 : *clas_mod = clas;
405 543510 : move16();
406 543510 : move16();
407 : }
408 : ELSE
409 : {
410 593454 : SWITCH( st->last_clas )
411 : {
412 451585 : case VOICED_CLAS:
413 : case ONSET:
414 : case VOICED_TRANSITION:
415 :
416 451585 : IF( LT_16( fmerit1, 16056 ) ) /*0.49f*/
417 : {
418 20446 : clas = UNVOICED_CLAS;
419 20446 : move16();
420 : }
421 431139 : ELSE IF( LT_16( fmerit1, 21626 ) ) /*0.66*/
422 : {
423 54876 : clas = VOICED_TRANSITION;
424 54876 : move16();
425 : }
426 : ELSE
427 : {
428 376263 : clas = VOICED_CLAS;
429 376263 : move16();
430 : }
431 451585 : IF( LT_16( fmerit1, 14745 /* 0.45f*/ ) )
432 : {
433 13028 : *clas_mod = UNVOICED_CLAS;
434 13028 : move16();
435 : }
436 438557 : ELSE IF( LT_16( fmerit1, 21626 /* 0.66f*/ ) )
437 : {
438 62294 : *clas_mod = VOICED_TRANSITION;
439 62294 : move16();
440 : }
441 : ELSE
442 : {
443 376263 : *clas_mod = VOICED_CLAS;
444 376263 : move16();
445 : }
446 451585 : BREAK;
447 :
448 141869 : case UNVOICED_CLAS:
449 : case UNVOICED_TRANSITION:
450 141869 : IF( GT_16( fmerit1, 20643 ) ) /*0.63*/
451 : {
452 57510 : clas = ONSET;
453 57510 : move16();
454 : }
455 84359 : ELSE IF( GT_16( fmerit1, 19169 ) ) /*0.585*/
456 : {
457 12012 : clas = UNVOICED_TRANSITION;
458 12012 : move16();
459 : }
460 : ELSE
461 : {
462 72347 : clas = UNVOICED_CLAS;
463 72347 : move16();
464 : }
465 141869 : *clas_mod = clas;
466 141869 : move16();
467 :
468 141869 : BREAK;
469 :
470 0 : default:
471 0 : clas = UNVOICED_CLAS;
472 0 : *clas_mod = clas;
473 0 : move16();
474 0 : move16();
475 0 : BREAK;
476 : }
477 : }
478 : /* Onset classification */
479 :
480 : /* tc_cnt == -1: frame after TC frame in continuous block of GC/VC frames */
481 : /* tc_cnt == 0: UC frame */
482 : /* tc_cnt == 1: onset/transition frame, coded by GC coder type */
483 : /* tc_cnt == 2: frame after onset/transition frame, coded by TC coder type */
484 :
485 1136964 : if ( clas == 0 )
486 : {
487 636303 : st->tc_cnt = 0;
488 636303 : move16();
489 : }
490 :
491 1136964 : test();
492 1136964 : IF( GE_16( clas, VOICED_TRANSITION ) && st->tc_cnt >= 0 )
493 : {
494 143256 : st->tc_cnt = add( st->tc_cnt, 1 );
495 143256 : move16();
496 : }
497 :
498 1136964 : if ( GT_16( st->tc_cnt, 2 ) )
499 : {
500 39412 : st->tc_cnt = -1;
501 39412 : move16();
502 : }
503 1136964 : return clas;
504 : }
505 :
506 : /*-------------------------------------------------------------------*
507 : * select_TC_fx()
508 : *
509 : * Select TC coder type for appropriate frames which is in general VOICED_TRANSITION,
510 : * VOICED_CLAS or ONSET frames following UNVOICED_CLAS frames
511 : *-------------------------------------------------------------------*/
512 :
513 1140064 : void select_TC_fx(
514 : const Word16 codec_mode, /* i : codec mode */
515 : const Word16 tc_cnt, /* i : TC frame counter */
516 : Word16 *coder_type, /* i/o: coder type */
517 : const Word16 localVAD /* i : VAD without hangover */
518 : )
519 : {
520 1140064 : IF( EQ_16( codec_mode, MODE1 ) )
521 : {
522 : /*---------------------------------------------------------------------*
523 : * Select TC coder type for appropriate frames which is in general VOICED_TRANSITION,
524 : * VOICED_CLAS or ONSET frames following UNVOICED_CLAS frames
525 : *---------------------------------------------------------------------*/
526 1139014 : test();
527 1139014 : IF( localVAD != 0 && GE_16( tc_cnt, 1 ) )
528 : {
529 104016 : IF( EQ_16( tc_cnt, 1 ) )
530 : {
531 : /* onset/transition frame is always coded using GC coder type */
532 57602 : *coder_type = GENERIC;
533 57602 : move16();
534 : }
535 : ELSE
536 : {
537 : /* frame after onset/transition frame is coded by TC coder type */
538 46414 : *coder_type = TRANSITION;
539 46414 : move16();
540 : }
541 : }
542 : }
543 :
544 1140064 : return;
545 : }
546 :
547 : /*-------------------------------------------------------------------*
548 : * coder_type_modif_fx()
549 : *
550 : * Coder type modification
551 : *-------------------------------------------------------------------*/
552 :
553 3100 : void coder_type_modif_fx(
554 : Encoder_State *st, /* i/o: encoder state structure */
555 : const Word16 relE /* i : frame relative E to the long term average Q8*/
556 : )
557 : {
558 : Word16 unmod_coder_type, vbr_generic_ho;
559 :
560 3100 : SC_VBR_ENC_HANDLE hSC_VBR = st->hSC_VBR;
561 :
562 :
563 3100 : IF( st->Opt_SC_VBR )
564 : {
565 0 : vbr_generic_ho = hSC_VBR->vbr_generic_ho;
566 0 : move16();
567 : }
568 : ELSE
569 : {
570 3100 : vbr_generic_ho = -1;
571 3100 : move16();
572 : }
573 :
574 3100 : IF( EQ_16( st->codec_mode, MODE1 ) )
575 : {
576 : /*---------------------------------------------------------------------*
577 : * Coder type modification
578 : *
579 : * Prevent UC coder type in certain conditions
580 : * Prevent VC coder type in certain conditions
581 : * Select TC coder type in appropriate frames
582 : *---------------------------------------------------------------------*/
583 :
584 : /* At higher rates, use GC coding instead of UC coding to improve quality */
585 2050 : test();
586 2050 : test();
587 2050 : test();
588 2050 : test();
589 2050 : test();
590 2050 : if ( ( st->element_mode == 0 && GT_32( st->total_brate, ACELP_9k60 ) && EQ_16( st->coder_type, UNVOICED ) ) ||
591 1957 : ( st->element_mode > 0 && GT_32( st->total_brate, MAX_UNVOICED_BRATE ) && EQ_16( st->coder_type, UNVOICED ) ) )
592 : {
593 93 : st->coder_type = GENERIC;
594 93 : move16();
595 : }
596 :
597 : /* Prevent UC coding on mixed content at 9.6 kb/s */
598 2050 : test();
599 2050 : test();
600 2050 : if ( GE_32( st->total_brate, ACELP_9k60 ) && EQ_16( st->coder_type, UNVOICED ) && st->audio_frame_cnt != 0 )
601 : {
602 0 : st->coder_type = GENERIC;
603 0 : move16();
604 : }
605 :
606 2050 : unmod_coder_type = st->coder_type;
607 2050 : move16();
608 :
609 : /* Enforce GC coder type on inactive signal (this can be later overwritten to INACTIVE) */
610 2050 : test();
611 2050 : test();
612 2050 : test();
613 2050 : test();
614 2050 : test();
615 2050 : test();
616 2050 : test();
617 2109 : if ( st->localVAD == 0 && ( (
618 118 : EQ_16( st->coder_type, UNVOICED ) && ( ( st->Opt_SC_VBR == 0 ) || ( ( EQ_16( st->Opt_SC_VBR, 1 ) ) && vbr_generic_ho == 0 && GT_16( st->last_coder_type, UNVOICED ) ) ) ) ||
619 118 : EQ_16( st->coder_type, TRANSITION ) || EQ_16( st->coder_type, VOICED ) )
620 :
621 : )
622 : {
623 0 : st->coder_type = GENERIC;
624 0 : move16();
625 : }
626 :
627 2050 : test();
628 2050 : test();
629 2050 : IF( EQ_16( st->Opt_SC_VBR, 1 ) )
630 : {
631 0 : test();
632 0 : if ( EQ_16( st->coder_type, GENERIC ) && EQ_16( unmod_coder_type, UNVOICED ) )
633 : {
634 0 : hSC_VBR->vbr_generic_ho = 1;
635 0 : move16();
636 : }
637 :
638 0 : if ( GT_16( st->coder_type, UNVOICED ) )
639 : {
640 0 : hSC_VBR->vbr_generic_ho = 0;
641 0 : move16();
642 : }
643 : } //_DIFF_FLOAT_FIX_ see below
644 : // PMT("Verify if EVS or IVAS is right about last_7k2_coder_type update")
645 2050 : hSC_VBR->last_7k2_coder_type = st->coder_type;
646 2050 : move16();
647 2050 : test();
648 2050 : if ( st->localVAD == 0 && EQ_16( st->coder_type, UNVOICED ) )
649 : {
650 0 : hSC_VBR->last_7k2_coder_type = GENERIC;
651 0 : move16();
652 : }
653 : //} closing bracket here in IVAS float, but not in EVS float. currently affects BE for switching bitrate on Linux 20220929 _DIFF_FLOAT_FIX_ !!
654 :
655 2050 : IF( st->element_mode == 0 )
656 : {
657 : /* At higher rates and with 16kHz core, allow only GC and TC coder type */
658 2050 : test();
659 2050 : test();
660 : // test();
661 2050 : if ( GT_32( st->total_brate, ACELP_16k40 ) && NE_16( st->coder_type, GENERIC ) && NE_16( st->coder_type, TRANSITION ) )
662 : {
663 : /* onset/transition frame is always coded using GC mode */
664 556 : st->coder_type = GENERIC;
665 556 : move16();
666 : }
667 : }
668 : ELSE /*IVAS*/
669 : {
670 : /* At higher bitrates, disable UC and VC coder type; note that IC coder type is classified later */
671 0 : test();
672 0 : test();
673 0 : test();
674 0 : if ( ( GT_32( st->total_brate, MAX_VOICED_BRATE ) && EQ_16( st->coder_type, VOICED ) ) ||
675 0 : ( GT_32( st->total_brate, MAX_UNVOICED_BRATE ) && EQ_16( st->coder_type, UNVOICED ) ) )
676 : {
677 0 : st->coder_type = GENERIC;
678 0 : move16();
679 : }
680 : }
681 :
682 : /* Patch for certain low-level signals for which the gain quantizer sometimes goes out of its dynamic range */
683 2050 : test();
684 2050 : test();
685 2050 : test();
686 2050 : if ( EQ_16( st->coder_type, VOICED ) && st->input_bwidth == 0 && LT_16( relE, -2560 ) && LE_32( st->total_brate, ACELP_8k00 ) )
687 : {
688 0 : st->coder_type = GENERIC;
689 0 : move16();
690 : }
691 : }
692 :
693 3100 : return;
694 : }
695 1133153 : void coder_type_modif_ivas_fx(
696 : Encoder_State *st, /* i/o: encoder state structure */
697 : const Word16 relE /* i : frame relative E to the long term average */
698 : )
699 : {
700 : Word16 unmod_coder_type, vbr_generic_ho;
701 :
702 1133153 : SC_VBR_ENC_HANDLE hSC_VBR = st->hSC_VBR;
703 :
704 :
705 1133153 : IF( st->Opt_SC_VBR )
706 : {
707 0 : vbr_generic_ho = hSC_VBR->vbr_generic_ho;
708 0 : move16();
709 : }
710 : ELSE
711 : {
712 1133153 : vbr_generic_ho = -1;
713 1133153 : move16();
714 : }
715 :
716 1133153 : IF( EQ_16( st->codec_mode, MODE1 ) )
717 : {
718 : /*---------------------------------------------------------------------*
719 : * Coder type modification
720 : *
721 : * Prevent UC coder type in certain conditions
722 : * Prevent VC coder type in certain conditions
723 : * Select TC coder type in appropriate frames
724 : *---------------------------------------------------------------------*/
725 :
726 : /* At higher rates, use GC coding instead of UC coding to improve quality */
727 1133153 : test();
728 1133153 : test();
729 1133153 : test();
730 1133153 : test();
731 1133153 : test();
732 1133153 : if ( ( st->element_mode == 0 && GT_32( st->total_brate, ACELP_9k60 ) && EQ_16( st->coder_type, UNVOICED ) ) ||
733 1133153 : ( st->element_mode > 0 && GT_32( st->total_brate, MAX_UNVOICED_BRATE ) && EQ_16( st->coder_type, UNVOICED ) ) )
734 : {
735 70169 : st->coder_type = GENERIC;
736 70169 : move16();
737 : }
738 :
739 : /* Prevent UC coding on mixed content at 9.6 kb/s */
740 1133153 : test();
741 1133153 : test();
742 1133153 : if ( GE_32( st->total_brate, ACELP_9k60 ) && EQ_16( st->coder_type, UNVOICED ) && st->audio_frame_cnt != 0 )
743 : {
744 2287 : st->coder_type = GENERIC;
745 2287 : move16();
746 : }
747 :
748 1133153 : unmod_coder_type = st->coder_type;
749 1133153 : move16();
750 :
751 : /* Enforce GC coder type on inactive signal (this can be later overwritten to INACTIVE) */
752 1133153 : test();
753 1133153 : test();
754 1133153 : test();
755 1133153 : test();
756 1133153 : test();
757 1133153 : test();
758 1133153 : test();
759 1294826 : if ( st->localVAD == 0 && ( (
760 323346 : EQ_16( st->coder_type, UNVOICED ) && ( ( st->Opt_SC_VBR == 0 ) || ( ( EQ_16( st->Opt_SC_VBR, 1 ) ) && vbr_generic_ho == 0 && GT_16( st->last_coder_type, UNVOICED ) ) ) ) ||
761 322292 : EQ_16( st->coder_type, TRANSITION ) || EQ_16( st->coder_type, VOICED ) )
762 :
763 : )
764 : {
765 527 : st->coder_type = GENERIC;
766 527 : move16();
767 : }
768 :
769 1133153 : if ( EQ_16( st->Opt_SC_VBR, 1 ) )
770 : {
771 0 : test();
772 0 : if ( EQ_16( st->coder_type, GENERIC ) && EQ_16( unmod_coder_type, UNVOICED ) )
773 : {
774 0 : hSC_VBR->vbr_generic_ho = 1;
775 0 : move16();
776 : }
777 :
778 0 : if ( GT_16( st->coder_type, UNVOICED ) )
779 : {
780 0 : hSC_VBR->vbr_generic_ho = 0;
781 0 : move16();
782 : }
783 :
784 0 : hSC_VBR->last_7k2_coder_type = st->coder_type;
785 0 : move16();
786 0 : test();
787 0 : if ( st->localVAD == 0 && EQ_16( st->coder_type, UNVOICED ) )
788 : {
789 0 : hSC_VBR->last_7k2_coder_type = GENERIC;
790 0 : move16();
791 : }
792 : }
793 :
794 1133153 : IF( st->element_mode == 0 )
795 : {
796 : /* At higher rates and with 16kHz core, allow only GC and TC coder type */
797 0 : test();
798 0 : test();
799 0 : if ( GT_32( st->total_brate, ACELP_16k40 ) && NE_16( st->coder_type, GENERIC ) && NE_16( st->coder_type, TRANSITION ) )
800 : {
801 : /* onset/transition frame is always coded using GC mode */
802 0 : st->coder_type = GENERIC;
803 0 : move16();
804 : }
805 : }
806 : ELSE /*IVAS*/
807 : {
808 : /* At higher bitrates, disable UC and VC coder type; note that IC coder type is classified later */
809 1133153 : test();
810 1133153 : test();
811 1133153 : test();
812 1940298 : if ( ( GT_32( st->total_brate, MAX_VOICED_BRATE ) && EQ_16( st->coder_type, VOICED ) ) ||
813 1517864 : ( GT_32( st->total_brate, MAX_UNVOICED_BRATE ) && EQ_16( st->coder_type, UNVOICED ) ) )
814 : {
815 326008 : st->coder_type = GENERIC;
816 326008 : move16();
817 : }
818 : }
819 :
820 : /* Patch for certain low-level signals for which the gain quantizer sometimes goes out of its dynamic range */
821 1133153 : test();
822 1133153 : test();
823 1133153 : test();
824 1133153 : if ( EQ_16( st->coder_type, VOICED ) && EQ_16( st->input_bwidth, NB ) && LT_16( relE, -2560 ) && LE_32( st->total_brate, ACELP_8k00 ) )
825 : {
826 0 : st->coder_type = GENERIC;
827 0 : move16();
828 : }
829 : }
830 :
831 1133153 : return;
832 : }
|