LCOV - code coverage report
Current view: top level - lib_enc - sig_clas_fx.c (source / functions) Hit Total Coverage
Test: Coverage on main @ 3a3a2ce7b84dee5b65e96301c38f19283bb2603d Lines: 321 381 84.3 %
Date: 2025-11-12 02:46:38 Functions: 5 5 100.0 %

          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             : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
      59        5150 :     Flag Overflow = 0;
      60        5150 :     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        5150 :     Ltmp = L_mult( st->voicing_fx[1], 16384 ); /* Q15*Q14->Q30 */
      72        5150 :     mean_voi2 = mac_r( Ltmp, st->voicing_fx[2], 16384 );
      73             : 
      74             :     /* average spectral tilt in dB */
      75        5150 :     lo = L_Extract_lc( ee[0], &hi );
      76        5150 :     lo2 = L_Extract_lc( ee[1], &hi2 );
      77        5150 :     Ltmp = L_mult( lo, lo2 ); /* Q5*Q5->Q11 */
      78             : 
      79        5150 :     test();
      80        5150 :     test();
      81        5150 :     IF( LT_32( Ltmp, 2048 ) )
      82             :     {
      83         749 :         een = 0;
      84         749 :         move16();
      85             :     }
      86        4401 :     ELSE IF( GT_32( Ltmp, THRES_EEN ) || hi > 0 || hi2 > 0 )
      87             :     {
      88        3585 :         een = 512;
      89        3585 :         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         816 :         exp_ee = norm_l( Ltmp );
      96         816 :         frac_ee = Log2_norm_lc( L_shl( Ltmp, exp_ee ) );
      97         816 :         exp_ee = sub( 30 - 11, exp_ee );
      98         816 :         Ltmp = Mpy_32_16( exp_ee, frac_ee, LG10 ); /* Ltmp Q14 */
      99         816 :         een = round_fx( L_shl( Ltmp, 16 - 5 ) );   /* Q14 -> Q9 */
     100         816 :         een = mac_r( C_EE_FX, een, K_EE_FX );
     101             :     }
     102             :     /* compute zero crossing rate */
     103        5150 :     pt1 = speech + sub( L_look, 1 );
     104        5150 :     tmpS = shr( *pt1, 15 ); /* sets 'tmpS to -1 if *pt1 < 0 */
     105        5150 :     Ltmp = L_deposit_l( 0 );
     106     1323550 :     FOR( i = 0; i < L_FRAME; i++ )
     107             :     {
     108     1318400 :         tmp16 = add( 1, tmpS );
     109     1318400 :         pt1++;
     110     1318400 :         tmpS = shr( *pt1, 15 ); /* pt1 >=0 ---> 0 OTHERWISE -1   */
     111     1318400 :         Ltmp = L_msu0( Ltmp, tmpS, tmp16 );
     112             :     }
     113        5150 :     zc = extract_l( Ltmp );
     114             : 
     115             :     /* compute pitch stability */
     116        5150 :     pc = add( abs_s( sub( st->pitch[1], st->pitch[0] ) ), abs_s( sub( st->pitch[2], st->pitch[1] ) ) );
     117        5150 :     st->tdm_pc = pc;
     118        5150 :     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        5150 :     Ltmp = L_mult( C_COR_FX, 32767 );
     126        5150 :     corn = round_fx( L_shl( L_mac( Ltmp, mean_voi2, K_COR_FX ), -4 ) ); /*Q13+Q13*Q15 =>Q13->Q9*/
     127             :     /* Limit [0, 1] */
     128        5150 :     corn = s_max( corn, 0 );
     129        5150 :     corn = s_min( corn, 512 );
     130             : 
     131        5150 :     Ltmp = L_mult( C_ZC_FX, 4 );                                   /*Q13*Q2 -> Q16*/
     132        5150 :     zcn = round_fx( L_shl( L_mac( Ltmp, zc, K_ZC_FX ), 16 - 7 ) ); /*Q0*Q15 + Q16*/
     133             :     /* Limit [0, 1] */
     134        5150 :     zcn = s_max( zcn, 0 );
     135        5150 :     zcn = s_min( zcn, 512 );
     136             : 
     137        5150 :     Ltmp = L_mult( C_RELE_FX, 256 );                                /*Q15*Q8 ->Q24*/
     138        5150 :     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        5150 :     relEn = s_max( relEn, 256 );
     141        5150 :     relEn = s_min( relEn, 512 );
     142             : 
     143        5150 :     Ltmp = L_mult( C_PC_FX, 2 );                                   /*Q14*Q1 -> Q16*/
     144        5150 :     pcn = round_fx( L_shl( L_mac( Ltmp, pc, K_PC_FX ), 16 - 7 ) ); /*Q16 + Q0*Q15*/
     145             :     /* Limit [0, 1] */
     146        5150 :     pcn = s_max( pcn, 0 );
     147        5150 :     pcn = s_min( pcn, 512 );
     148             : 
     149        5150 :     Ltmp = L_mult( een, 10923 );
     150        5150 :     Ltmp = L_mac( Ltmp, corn, 21845 );
     151        5150 :     Ltmp = L_mac( Ltmp, zcn, 10923 );
     152        5150 :     Ltmp = L_mac( Ltmp, relEn, 10923 );
     153        5150 :     Ltmp = L_mac( Ltmp, pcn, 10923 );
     154             : 
     155        5150 :     fmerit1 = round_fx_o( L_shl_o( Ltmp, 16 - 10 - 1, &Overflow ), &Overflow ); /* fmerit1 ->Q15 */
     156             : 
     157             :     /*-----------------------------------------------------------------*
     158             :      * FEC classification
     159             :      *-----------------------------------------------------------------*/
     160             : 
     161        5150 :     st->fmerit_dt = sub( st->prev_fmerit, fmerit1 ); /*Q15*/
     162        5150 :     move16();
     163        5150 :     st->prev_fmerit = fmerit1;
     164        5150 :     move16();
     165             : 
     166             :     /* FEC classification */
     167        5150 :     test();
     168        5150 :     test();
     169        5150 :     IF( st->localVAD == 0 || EQ_16( st->coder_type, UNVOICED ) || LT_16( relE, -1536 ) )
     170             :     {
     171        2299 :         clas = UNVOICED_CLAS;
     172        2299 :         *clas_mod = clas;
     173        2299 :         move16();
     174        2299 :         move16();
     175             :     }
     176             :     ELSE
     177             :     {
     178        2851 :         SWITCH( st->last_clas )
     179             :         {
     180        2398 :             case VOICED_CLAS:
     181             :             case ONSET:
     182             :             case VOICED_TRANSITION:
     183             : 
     184        2398 :                 IF( LT_16( fmerit1, 16056 ) ) /*0.49f*/
     185             :                 {
     186          42 :                     clas = UNVOICED_CLAS;
     187          42 :                     move16();
     188             :                 }
     189        2356 :                 ELSE IF( LT_16( fmerit1, 21626 ) ) /*0.66*/
     190             :                 {
     191         214 :                     clas = VOICED_TRANSITION;
     192         214 :                     move16();
     193             :                 }
     194             :                 ELSE
     195             :                 {
     196        2142 :                     clas = VOICED_CLAS;
     197        2142 :                     move16();
     198             :                 }
     199        2398 :                 IF( LT_16( fmerit1, 14745 /* 0.45f*/ ) )
     200             :                 {
     201          30 :                     *clas_mod = UNVOICED_CLAS;
     202          30 :                     move16();
     203             :                 }
     204        2368 :                 ELSE IF( LT_16( fmerit1, 21626 /* 0.66f*/ ) )
     205             :                 {
     206         226 :                     *clas_mod = VOICED_TRANSITION;
     207         226 :                     move16();
     208             :                 }
     209             :                 ELSE
     210             :                 {
     211        2142 :                     *clas_mod = VOICED_CLAS;
     212        2142 :                     move16();
     213             :                 }
     214        2398 :                 BREAK;
     215             : 
     216         453 :             case UNVOICED_CLAS:
     217             :             case UNVOICED_TRANSITION:
     218         453 :                 IF( GT_16( fmerit1, 20643 ) ) /*0.63*/
     219             :                 {
     220         225 :                     clas = ONSET;
     221         225 :                     move16();
     222             :                 }
     223         228 :                 ELSE IF( GT_16( fmerit1, 19169 ) ) /*0.585*/
     224             :                 {
     225          69 :                     clas = UNVOICED_TRANSITION;
     226          69 :                     move16();
     227             :                 }
     228             :                 ELSE
     229             :                 {
     230         159 :                     clas = UNVOICED_CLAS;
     231         159 :                     move16();
     232             :                 }
     233         453 :                 *clas_mod = clas;
     234         453 :                 move16();
     235             : 
     236         453 :                 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        5150 :     if ( clas == 0 )
     254             :     {
     255        2500 :         st->tc_cnt = 0;
     256        2500 :         move16();
     257             :     }
     258             : 
     259        5150 :     test();
     260        5150 :     IF( GE_16( clas, VOICED_TRANSITION ) && st->tc_cnt >= 0 )
     261             :     {
     262         639 :         st->tc_cnt = add( st->tc_cnt, 1 );
     263         639 :         move16();
     264             :     }
     265             : 
     266        5150 :     if ( GT_16( st->tc_cnt, 2 ) )
     267             :     {
     268         206 :         st->tc_cnt = -1;
     269         206 :         move16();
     270             :     }
     271        5150 :     return clas;
     272             : }
     273             : 
     274     1290382 : 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     1290382 :     Flag Overflow = 0;
     291     1290382 :     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     1290382 :     Ltmp = L_mult( st->voicing_fx[1], 16384 ); /* Q15*Q14->Q30 */
     303     1290382 :     mean_voi2 = mac_r( Ltmp, st->voicing_fx[2], 16384 );
     304             : 
     305             :     /* average spectral tilt in dB */
     306     1290382 :     tmp64 = W_mult0_32_32( ee[0], ee[1] );
     307     1290382 :     exp_ee = W_norm( tmp64 );
     308     1290382 :     Ltmp = W_extract_h( W_shl( tmp64, exp_ee ) ); // Q = Q6+Q6 + exp_ee - 32
     309     1290382 :     exp_ee = sub( 31, sub( add( Q12, exp_ee ), 32 ) );
     310     1290382 :     IF( EQ_16( BASOP_Util_Cmp_Mant32Exp( Ltmp, exp_ee, ONE_IN_Q31, 0 ), -1 ) )
     311             :     {
     312      132525 :         een = 0;
     313      132525 :         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     1157857 :         Ltmp = BASOP_Util_Log10( Ltmp, exp_ee );                            // Q25
     320     1157857 :         Ltmp = Mpy_32_32( Ltmp, 671088640 /*20.f in Q25*/ );                // Q25 + Q25 -Q31 = Q19 * 0.5 = Q20
     321     1157857 :         een = extract_l( L_shl( Mpy_32_16_1( Ltmp, K_EE_FX ), Q9 - Q20 ) ); // Q9
     322             : 
     323     1157857 :         IF( GT_16( een, 512 ) )
     324             :         {
     325      824232 :             een = 512;
     326      824232 :             move16();
     327             :         }
     328      333625 :         ELSE IF( een < 0 )
     329             :         {
     330           0 :             een = 0;
     331           0 :             move16();
     332             :         }
     333             :     }
     334             :     /* compute zero crossing rate */
     335     1290382 :     pt1 = speech + sub( L_look, 1 );
     336     1290382 :     tmpS = shr( *pt1, 15 ); /* sets 'tmpS to -1 if *pt1 < 0 */
     337     1290382 :     Ltmp = L_deposit_l( 0 );
     338   331628174 :     FOR( i = 0; i < L_FRAME; i++ )
     339             :     {
     340   330337792 :         tmp16 = add( 1, tmpS );
     341   330337792 :         pt1++;
     342   330337792 :         tmpS = shr( *pt1, 15 ); /* pt1 >=0 ---> 0 OTHERWISE -1   */
     343   330337792 :         Ltmp = L_msu0( Ltmp, tmpS, tmp16 );
     344             :     }
     345     1290382 :     zc = extract_l( Ltmp );
     346             : 
     347             :     /* compute pitch stability */
     348     1290382 :     pc = add( abs_s( sub( st->pitch[1], st->pitch[0] ) ), abs_s( sub( st->pitch[2], st->pitch[1] ) ) );
     349     1290382 :     st->tdm_pc = pc;
     350     1290382 :     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     1290382 :     Ltmp = L_mult( C_COR_FX, 32767 );
     358     1290382 :     corn = round_fx( L_shl( L_mac( Ltmp, mean_voi2, K_COR_FX ), -4 ) ); /*Q13+Q13*Q15 =>Q13->Q9*/
     359             :     /* Limit [0, 1] */
     360     1290382 :     corn = s_max( corn, 0 );
     361     1290382 :     corn = s_min( corn, 512 );
     362             : 
     363     1290382 :     Ltmp = L_mult( C_ZC_FX, 4 );                                   /*Q13*Q2 -> Q16*/
     364     1290382 :     zcn = round_fx( L_shl( L_mac( Ltmp, zc, K_ZC_FX ), 16 - 7 ) ); /*Q0*Q15 + Q16*/
     365             :     /* Limit [0, 1] */
     366     1290382 :     zcn = s_max( zcn, 0 );
     367     1290382 :     zcn = s_min( zcn, 512 );
     368             : 
     369     1290382 :     Ltmp = L_mult( C_RELE_FX, 256 );                                /*Q15*Q8 ->Q24*/
     370     1290382 :     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     1290382 :     relEn = s_max( relEn, 256 );
     373     1290382 :     relEn = s_min( relEn, 512 );
     374             : 
     375     1290382 :     Ltmp = L_mult( C_PC_FX, 2 );                                   /*Q14*Q1 -> Q16*/
     376     1290382 :     pcn = round_fx( L_shl( L_mac( Ltmp, pc, K_PC_FX ), 16 - 7 ) ); /*Q16 + Q0*Q15*/
     377             :     /* Limit [0, 1] */
     378     1290382 :     pcn = s_max( pcn, 0 );
     379     1290382 :     pcn = s_min( pcn, 512 );
     380             : 
     381     1290382 :     Ltmp = L_mult( een, 10923 );
     382     1290382 :     Ltmp = L_mac( Ltmp, corn, 21845 );
     383     1290382 :     Ltmp = L_mac( Ltmp, zcn, 10923 );
     384     1290382 :     Ltmp = L_mac( Ltmp, relEn, 10923 );
     385     1290382 :     Ltmp = L_mac( Ltmp, pcn, 10923 );
     386             : 
     387     1290382 :     fmerit1 = round_fx_o( L_shl_o( Ltmp, 16 - 10 - 1, &Overflow ), &Overflow ); /* fmerit1 ->Q15 */
     388             : 
     389             :     /*-----------------------------------------------------------------*
     390             :      * FEC classification
     391             :      *-----------------------------------------------------------------*/
     392             : 
     393     1290382 :     st->fmerit_dt = sub( st->prev_fmerit, fmerit1 ); /*Q15*/
     394     1290382 :     move16();
     395     1290382 :     st->prev_fmerit = fmerit1;
     396     1290382 :     move16();
     397             : 
     398             :     /* FEC classification */
     399     1290382 :     test();
     400     1290382 :     test();
     401     1290382 :     IF( st->localVAD == 0 || EQ_16( st->coder_type, UNVOICED ) || LT_16( relE, -1536 ) )
     402             :     {
     403      605464 :         clas = UNVOICED_CLAS;
     404      605464 :         *clas_mod = clas;
     405      605464 :         move16();
     406      605464 :         move16();
     407             :     }
     408             :     ELSE
     409             :     {
     410      684918 :         SWITCH( st->last_clas )
     411             :         {
     412      509775 :             case VOICED_CLAS:
     413             :             case ONSET:
     414             :             case VOICED_TRANSITION:
     415             : 
     416      509775 :                 IF( LT_16( fmerit1, 16056 ) ) /*0.49f*/
     417             :                 {
     418       26283 :                     clas = UNVOICED_CLAS;
     419       26283 :                     move16();
     420             :                 }
     421      483492 :                 ELSE IF( LT_16( fmerit1, 21626 ) ) /*0.66*/
     422             :                 {
     423       64855 :                     clas = VOICED_TRANSITION;
     424       64855 :                     move16();
     425             :                 }
     426             :                 ELSE
     427             :                 {
     428      418637 :                     clas = VOICED_CLAS;
     429      418637 :                     move16();
     430             :                 }
     431      509775 :                 IF( LT_16( fmerit1, 14745 /* 0.45f*/ ) )
     432             :                 {
     433       16402 :                     *clas_mod = UNVOICED_CLAS;
     434       16402 :                     move16();
     435             :                 }
     436      493373 :                 ELSE IF( LT_16( fmerit1, 21626 /* 0.66f*/ ) )
     437             :                 {
     438       74736 :                     *clas_mod = VOICED_TRANSITION;
     439       74736 :                     move16();
     440             :                 }
     441             :                 ELSE
     442             :                 {
     443      418637 :                     *clas_mod = VOICED_CLAS;
     444      418637 :                     move16();
     445             :                 }
     446      509775 :                 BREAK;
     447             : 
     448      175143 :             case UNVOICED_CLAS:
     449             :             case UNVOICED_TRANSITION:
     450      175143 :                 IF( GT_16( fmerit1, 20643 ) ) /*0.63*/
     451             :                 {
     452       68859 :                     clas = ONSET;
     453       68859 :                     move16();
     454             :                 }
     455      106284 :                 ELSE IF( GT_16( fmerit1, 19169 ) ) /*0.585*/
     456             :                 {
     457       14226 :                     clas = UNVOICED_TRANSITION;
     458       14226 :                     move16();
     459             :                 }
     460             :                 ELSE
     461             :                 {
     462       92058 :                     clas = UNVOICED_CLAS;
     463       92058 :                     move16();
     464             :                 }
     465      175143 :                 *clas_mod = clas;
     466      175143 :                 move16();
     467             : 
     468      175143 :                 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     1290382 :     if ( clas == 0 )
     486             :     {
     487      723805 :         st->tc_cnt = 0;
     488      723805 :         move16();
     489             :     }
     490             : 
     491     1290382 :     test();
     492     1290382 :     IF( GE_16( clas, VOICED_TRANSITION ) && st->tc_cnt >= 0 )
     493             :     {
     494      170051 :         st->tc_cnt = add( st->tc_cnt, 1 );
     495      170051 :         move16();
     496             :     }
     497             : 
     498     1290382 :     if ( GT_16( st->tc_cnt, 2 ) )
     499             :     {
     500       46049 :         st->tc_cnt = -1;
     501       46049 :         move16();
     502             :     }
     503     1290382 :     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     1295532 : 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     1295532 :     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     1293432 :         test();
     527     1293432 :         IF( localVAD != 0 && GE_16( tc_cnt, 1 ) )
     528             :         {
     529      124259 :             IF( EQ_16( tc_cnt, 1 ) )
     530             :             {
     531             :                 /* onset/transition frame is always coded using GC coder type */
     532       68994 :                 *coder_type = GENERIC;
     533       68994 :                 move16();
     534             :             }
     535             :             ELSE
     536             :             {
     537             :                 /* frame after onset/transition frame is coded by TC coder type */
     538       55265 :                 *coder_type = TRANSITION;
     539       55265 :                 move16();
     540             :             }
     541             :         }
     542             :     }
     543             : 
     544     1295532 :     return;
     545             : }
     546             : 
     547             : /*-------------------------------------------------------------------*
     548             :  * coder_type_modif_fx()
     549             :  *
     550             :  * Coder type modification
     551             :  *-------------------------------------------------------------------*/
     552             : 
     553        5150 : 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        5150 :     SC_VBR_ENC_HANDLE hSC_VBR = st->hSC_VBR;
     561             : 
     562             : 
     563        5150 :     IF( st->Opt_SC_VBR )
     564             :     {
     565           0 :         vbr_generic_ho = hSC_VBR->vbr_generic_ho;
     566           0 :         move16();
     567             :     }
     568             :     ELSE
     569             :     {
     570        5150 :         vbr_generic_ho = -1;
     571        5150 :         move16();
     572             :     }
     573             : 
     574        5150 :     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        3050 :         test();
     586        3050 :         test();
     587        3050 :         test();
     588        3050 :         test();
     589        3050 :         test();
     590        3050 :         if ( ( st->element_mode == 0 && GT_32( st->total_brate, ACELP_9k60 ) && EQ_16( st->coder_type, UNVOICED ) ) ||
     591        2916 :              ( st->element_mode > 0 && GT_32( st->total_brate, MAX_UNVOICED_BRATE ) && EQ_16( st->coder_type, UNVOICED ) ) )
     592             :         {
     593         134 :             st->coder_type = GENERIC;
     594         134 :             move16();
     595             :         }
     596             : 
     597             :         /* Prevent UC coding on mixed content at 9.6 kb/s */
     598        3050 :         test();
     599        3050 :         test();
     600        3050 :         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        3050 :         unmod_coder_type = st->coder_type;
     607        3050 :         move16();
     608             : 
     609             :         /* Enforce GC coder type on inactive signal (this can be later overwritten to INACTIVE) */
     610        3050 :         test();
     611        3050 :         test();
     612        3050 :         test();
     613        3050 :         test();
     614        3050 :         test();
     615        3050 :         test();
     616        3050 :         test();
     617        3120 :         if ( st->localVAD == 0 && ( (
     618         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 ) ) ) ) ||
     619         140 :                                     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        3050 :         test();
     628        3050 :         test();
     629        3050 :         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             :         }
     644        3050 :         hSC_VBR->last_7k2_coder_type = st->coder_type;
     645        3050 :         move16();
     646        3050 :         test();
     647        3050 :         if ( st->localVAD == 0 && EQ_16( st->coder_type, UNVOICED ) )
     648             :         {
     649           0 :             hSC_VBR->last_7k2_coder_type = GENERIC;
     650           0 :             move16();
     651             :         }
     652             : 
     653        3050 :         IF( st->element_mode == 0 )
     654             :         {
     655             :             /* At higher rates and with 16kHz core, allow only GC and TC coder type */
     656        3050 :             test();
     657        3050 :             test();
     658             :             // test();
     659        3050 :             if ( GT_32( st->total_brate, ACELP_16k40 ) && NE_16( st->coder_type, GENERIC ) && NE_16( st->coder_type, TRANSITION ) )
     660             :             {
     661             :                 /* onset/transition frame is always coded using GC mode */
     662         556 :                 st->coder_type = GENERIC;
     663         556 :                 move16();
     664             :             }
     665             :         }
     666             :         ELSE /*IVAS*/
     667             :         {
     668             :             /* At higher bitrates, disable UC and VC coder type; note that IC coder type is classified later */
     669           0 :             test();
     670           0 :             test();
     671           0 :             test();
     672           0 :             if ( ( GT_32( st->total_brate, MAX_VOICED_BRATE ) && EQ_16( st->coder_type, VOICED ) ) ||
     673           0 :                  ( GT_32( st->total_brate, MAX_UNVOICED_BRATE ) && EQ_16( st->coder_type, UNVOICED ) ) )
     674             :             {
     675           0 :                 st->coder_type = GENERIC;
     676           0 :                 move16();
     677             :             }
     678             :         }
     679             : 
     680             :         /* Patch for certain low-level signals for which the gain quantizer sometimes goes out of its dynamic range */
     681        3050 :         test();
     682        3050 :         test();
     683        3050 :         test();
     684        3050 :         if ( EQ_16( st->coder_type, VOICED ) && st->input_bwidth == 0 && LT_16( relE, -2560 ) && LE_32( st->total_brate, ACELP_8k00 ) )
     685             :         {
     686           0 :             st->coder_type = GENERIC;
     687           0 :             move16();
     688             :         }
     689             :     }
     690             : 
     691        5150 :     return;
     692             : }
     693     1286175 : void coder_type_modif_ivas_fx(
     694             :     Encoder_State *st, /* i/o: encoder state structure                     */
     695             :     const Word16 relE  /* i  : frame relative E to the long term average   */
     696             : )
     697             : {
     698             :     Word16 unmod_coder_type, vbr_generic_ho;
     699             : 
     700     1286175 :     SC_VBR_ENC_HANDLE hSC_VBR = st->hSC_VBR;
     701             : 
     702             : 
     703     1286175 :     IF( st->Opt_SC_VBR )
     704             :     {
     705           0 :         vbr_generic_ho = hSC_VBR->vbr_generic_ho;
     706           0 :         move16();
     707             :     }
     708             :     ELSE
     709             :     {
     710     1286175 :         vbr_generic_ho = -1;
     711     1286175 :         move16();
     712             :     }
     713             : 
     714     1286175 :     IF( EQ_16( st->codec_mode, MODE1 ) )
     715             :     {
     716             :         /*---------------------------------------------------------------------*
     717             :          * Coder type modification
     718             :          *
     719             :          * Prevent UC coder type in certain conditions
     720             :          * Prevent VC coder type in certain conditions
     721             :          * Select TC coder type in appropriate frames
     722             :          *---------------------------------------------------------------------*/
     723             : 
     724             :         /* At higher rates, use GC coding instead of UC coding to improve quality */
     725     1286175 :         test();
     726     1286175 :         test();
     727     1286175 :         test();
     728     1286175 :         test();
     729     1286175 :         test();
     730     1286175 :         if ( ( st->element_mode == 0 && GT_32( st->total_brate, ACELP_9k60 ) && EQ_16( st->coder_type, UNVOICED ) ) ||
     731     1286175 :              ( st->element_mode > 0 && GT_32( st->total_brate, MAX_UNVOICED_BRATE ) && EQ_16( st->coder_type, UNVOICED ) ) )
     732             :         {
     733       90364 :             st->coder_type = GENERIC;
     734       90364 :             move16();
     735             :         }
     736             : 
     737             :         /* Prevent UC coding on mixed content at 9.6 kb/s */
     738     1286175 :         test();
     739     1286175 :         test();
     740     1286175 :         if ( GE_32( st->total_brate, ACELP_9k60 ) && EQ_16( st->coder_type, UNVOICED ) && st->audio_frame_cnt != 0 )
     741             :         {
     742        3643 :             st->coder_type = GENERIC;
     743        3643 :             move16();
     744             :         }
     745             : 
     746     1286175 :         unmod_coder_type = st->coder_type;
     747     1286175 :         move16();
     748             : 
     749             :         /* Enforce GC coder type on inactive signal (this can be later overwritten to INACTIVE) */
     750     1286175 :         test();
     751     1286175 :         test();
     752     1286175 :         test();
     753     1286175 :         test();
     754     1286175 :         test();
     755     1286175 :         test();
     756     1286175 :         test();
     757     1462787 :         if ( st->localVAD == 0 && ( (
     758      353224 :                                         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 ) ) ) ) ||
     759      351634 :                                     EQ_16( st->coder_type, TRANSITION ) || EQ_16( st->coder_type, VOICED ) )
     760             : 
     761             :         )
     762             :         {
     763         795 :             st->coder_type = GENERIC;
     764         795 :             move16();
     765             :         }
     766             : 
     767     1286175 :         if ( EQ_16( st->Opt_SC_VBR, 1 ) )
     768             :         {
     769           0 :             test();
     770           0 :             if ( EQ_16( st->coder_type, GENERIC ) && EQ_16( unmod_coder_type, UNVOICED ) )
     771             :             {
     772           0 :                 hSC_VBR->vbr_generic_ho = 1;
     773           0 :                 move16();
     774             :             }
     775             : 
     776           0 :             if ( GT_16( st->coder_type, UNVOICED ) )
     777             :             {
     778           0 :                 hSC_VBR->vbr_generic_ho = 0;
     779           0 :                 move16();
     780             :             }
     781             : 
     782           0 :             hSC_VBR->last_7k2_coder_type = st->coder_type;
     783           0 :             move16();
     784           0 :             test();
     785           0 :             if ( st->localVAD == 0 && EQ_16( st->coder_type, UNVOICED ) )
     786             :             {
     787           0 :                 hSC_VBR->last_7k2_coder_type = GENERIC;
     788           0 :                 move16();
     789             :             }
     790             :         }
     791             : 
     792     1286175 :         IF( st->element_mode == 0 )
     793             :         {
     794             :             /* At higher rates and with 16kHz core, allow only GC and TC coder type */
     795           0 :             test();
     796           0 :             test();
     797           0 :             if ( GT_32( st->total_brate, ACELP_16k40 ) && NE_16( st->coder_type, GENERIC ) && NE_16( st->coder_type, TRANSITION ) )
     798             :             {
     799             :                 /* onset/transition frame is always coded using GC mode */
     800           0 :                 st->coder_type = GENERIC;
     801           0 :                 move16();
     802             :             }
     803             :         }
     804             :         ELSE /*IVAS*/
     805             :         {
     806             :             /* At higher bitrates, disable UC and VC coder type; note that IC coder type is classified later */
     807     1286175 :             test();
     808     1286175 :             test();
     809     1286175 :             test();
     810     2217481 :             if ( ( GT_32( st->total_brate, MAX_VOICED_BRATE ) && EQ_16( st->coder_type, VOICED ) ) ||
     811     1753023 :                  ( GT_32( st->total_brate, MAX_UNVOICED_BRATE ) && EQ_16( st->coder_type, UNVOICED ) ) )
     812             :             {
     813      354869 :                 st->coder_type = GENERIC;
     814      354869 :                 move16();
     815             :             }
     816             :         }
     817             : 
     818             :         /* Patch for certain low-level signals for which the gain quantizer sometimes goes out of its dynamic range */
     819     1286175 :         test();
     820     1286175 :         test();
     821     1286175 :         test();
     822     1286175 :         if ( EQ_16( st->coder_type, VOICED ) && EQ_16( st->input_bwidth, NB ) && LT_16( relE, -2560 ) && LE_32( st->total_brate, ACELP_8k00 ) )
     823             :         {
     824           0 :             st->coder_type = GENERIC;
     825           0 :             move16();
     826             :         }
     827             :     }
     828             : 
     829     1286175 :     return;
     830             : }

Generated by: LCOV version 1.14