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