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