LCOV - code coverage report
Current view: top level - lib_enc - scale_enc_fx.c (source / functions) Hit Total Coverage
Test: Coverage on main @ e95243e9e67ddeb69dddf129509de1b3d95b402e Lines: 104 107 97.2 %
Date: 2025-09-14 03:13:15 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /*====================================================================================
       2             :     EVS Codec 3GPP TS26.452 Aug 12, 2021. Version 16.3.0
       3             :   ====================================================================================*/
       4             : 
       5             : #include "options.h"    /* Compilation switches                   */
       6             : #include "cnst.h"       /* Common constants                       */
       7             : #include "rom_com_fx.h" /* Static table prototypes                */
       8             : //#include "prot_fx.h"       /* Function prototypes                    */
       9             : #include "prot_fx.h"     /* Function prototypes                    */
      10             : #include "prot_fx_enc.h" /* Function prototypes                    */
      11             : #include "stl.h"
      12             : 
      13             : /*-------------------------------------------------------------------*
      14             :  * Scale_wsp
      15             :  *
      16             :  * Find scaling factor for weighted speech input
      17             :  *-------------------------------------------------------------------*/
      18        3100 : void Scale_wsp(
      19             :     Word16 *wsp,            /* i  : Weigthed speech                      */
      20             :     Word16 *old_wsp_max,    /* i  : Last weigthed speech maximal valu    */
      21             :     Word16 *shift,          /* i/o: Scaling of current frame             */
      22             :     Word16 *Q_exp,          /* i/o: Differential scaling factor          */
      23             :     Word16 *old_wsp_shift,  /* i/o: Last wsp scaling                     */
      24             :     Word16 *old_wsp,        /* i/o: Old weighted speech buffer           */
      25             :     Word16 *mem_decim2,     /* i/o: Decimation buffer                    */
      26             :     Word16 *old_wsp12k8,    /* i/o: wsp memory @ 12.8 kHz used in pitol2 */
      27             :     const Word16 Len_p_look /* i  : L_frame + look ahead                                        Q0*/
      28             : )
      29             : {
      30             :     Word16 max, i, tmp;
      31             : 
      32             :     /* find maximum value on wsp[] for 12 bits scaling */
      33        3100 :     max = 0;
      34        3100 :     move16();
      35     1143900 :     FOR( i = 0; i < Len_p_look; i++ )
      36             :     {
      37     1140800 :         tmp = abs_s( wsp[i] );
      38     1140800 :         max = s_max( max, tmp );
      39             :     }
      40        3100 :     tmp = *old_wsp_max;
      41        3100 :     move16();
      42        3100 :     tmp = s_max( max, tmp );
      43        3100 :     *old_wsp_max = max;
      44        3100 :     move16();
      45             : 
      46        3100 :     *shift = sub( norm_s( tmp ), 3 );
      47        3100 :     move16();
      48             : 
      49        3100 :     *shift = s_min( *shift, 0 );
      50        3100 :     move16(); /* shift = 0..-3 */
      51             : 
      52             : 
      53        3100 :     Scale_sig( wsp, Len_p_look, *shift );
      54             :     /* scale old_wsp (warning: exp must be Q_new-Q_old) */
      55        3100 :     *Q_exp = add( *Q_exp, sub( *shift, *old_wsp_shift ) );
      56        3100 :     move16();
      57        3100 :     *old_wsp_shift = *shift;
      58        3100 :     move16();
      59        3100 :     Scale_sig( old_wsp12k8, L_WSP_MEM, *Q_exp ); /* Already scaled with premphasis */
      60        3100 :     Scale_sig( old_wsp, ( L_WSP_MEM - L_INTERPOL ) / OPL_DECIM, *Q_exp );
      61        3100 :     Scale_sig( mem_decim2, 3, *Q_exp );
      62             : 
      63        3100 :     Copy( old_wsp12k8, wsp - L_WSP_MEM, L_WSP_MEM ); /* Now memory and wsp vector have the same scaling */
      64        3100 : }
      65             : 
      66             : /*-------------------------------------------------------------------*
      67             :  * Preemph_scaled
      68             :  *
      69             :  * Find scaled preemphasis vector and its scaling factor
      70             :  *-------------------------------------------------------------------*/
      71        6200 : void Preemph_scaled(
      72             :     Word16 new_speech[],          /* i  : Speech to scale already on 14 bits                            Q_new*/
      73             :     Word16 *Q_new,                /* o  : Scaling factor                                                                        */
      74             :     Word16 *mem_preemph,          /* i/o: Preemph memory                                                                        Q(-1)*/
      75             :     Word16 *Q_max,                /* i/o: Q_new limitation                                                                      */
      76             :     const Word16 Preemph_factor,  /* i  : Preemphasis factor                                                            Q15*/
      77             :     const Word16 bits,            /* i  : Bit to remove from the output to (15-bits)            */
      78             :     const Word16 bit1,            /* i  : Limit the output scaling to ((15-bits)-bit1) bits     */
      79             :     const Word16 L_Q_mem,         /* i  : Number of old scaling to take into account            Q0*/
      80             :     const Word16 Lframe,          /* i  : Frame length                                                                          Q0*/
      81             :     const Word16 last_coder_type, /* i  : coder_type                                                                            Q0*/
      82             :     const Word16 Search_scaling   /* i  : enable the search of a proper scaling factor          Q0*/
      83             : )
      84             : {
      85             :     Word16 i, tmp_fixed;
      86             :     Word16 mu, shift, QVal;
      87             :     Word32 L_tmp, L_maxloc;
      88             :     Word16 Q_min;
      89             : #if !defined( ISSUE_1867_replace_overflow_libenc )
      90             : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
      91             :     Flag Overflow = 0;
      92             :     move32();
      93             : #endif
      94             : #endif
      95             : 
      96             :     /*---------------------------------------------------------------*
      97             :      * Perform fixed preemphasis through 1 - g z^-1 *
      98             :      * Scale signal to get maximum of precision in filtering *
      99             :      *---------------------------------------------------------------*/
     100             : 
     101             :     BASOP_SATURATE_WARNING_OFF_EVS
     102             : 
     103             : #ifdef ISSUE_1867_replace_overflow_libenc
     104        6200 :     QVal = shl_sat( 1, sub( 15, bits ) ); //?sat
     105             : #else
     106             :     Overflow = 0;
     107             :     QVal = shl_sat( 1, sub( 15, bits ) );
     108             : #endif
     109             :     BASOP_SATURATE_WARNING_ON_EVS
     110        6200 :     mu = shr( Preemph_factor, bits ); /* Q15 --> Q(15-bits) */
     111             : 
     112        6200 :     IF( EQ_16( Search_scaling, 1 ) )
     113             :     {
     114             :         /* get max of new preemphased samples (L_FRAME+L_FILT) */
     115             : 
     116        6200 :         L_tmp = L_mult( new_speech[0], QVal );
     117        6200 :         L_tmp = L_msu_sat( L_tmp, *mem_preemph, mu );
     118        6200 :         L_maxloc = L_abs( L_tmp );
     119             : 
     120     1785600 :         FOR( i = 1; i < Lframe; i++ )
     121             :         {
     122             :             /* Equivalent to tmp = max((abs(x[i] - mu*x[i-1]),tmp)
     123             :              * finds the max of preemphasized signal */
     124     1779400 :             L_tmp = L_mult( new_speech[i], QVal );
     125             : #ifdef ISSUE_1867_replace_overflow_libenc
     126     1779400 :             L_tmp = L_msu_sat( L_tmp, new_speech[i - 1], mu );
     127             : #else
     128             :             L_tmp = L_msu_o( L_tmp, new_speech[i - 1], mu, &Overflow );
     129             : #endif
     130     1779400 :             L_tmp = L_abs( L_tmp );
     131     1779400 :             L_maxloc = L_max( L_tmp, L_maxloc );
     132             :         }
     133             : 
     134             :         /* get scaling factor for new and previous samples */
     135             :         /* limit scaling to Q_MAX to keep dynamic for ringing in low signal */
     136             :         /* limit scaling to Q_MAX also to avoid a[0]<1 in syn_filt_32 */
     137        6200 :         tmp_fixed = s_max( extract_h( L_maxloc ), 1 );
     138             : 
     139             :         /* output on 14 bits: needed unless the resampling itself removes 1 bit*/
     140        6200 :         shift = sub( norm_s( tmp_fixed ), add( bits, bit1 ) );
     141        6200 :         shift = s_max( shift, 0 );
     142        6200 :         shift = s_min( shift, Q_MAX );
     143             : 
     144        6200 :         minimum_fx( Q_max, L_Q_mem, &Q_min );
     145        6200 :         *Q_new = s_min( shift, Q_min );
     146        6200 :         move16();
     147             : 
     148        6200 :         IF( tmp_fixed == 0 )
     149             :         {
     150           0 :             IF( NE_16( last_coder_type, UNVOICED ) )
     151             :             {
     152           0 :                 *Q_new = s_min( *Q_new, 1 );
     153           0 :                 move16();
     154             :             }
     155             :         }
     156             : 
     157       31000 :         FOR( i = L_Q_mem - 1; i > 0; i-- )
     158             :         {
     159       24800 :             Q_max[i] = Q_max[i - 1];
     160       24800 :             move16();
     161             :         }
     162        6200 :         Q_max[i] = shift;
     163        6200 :         move16();
     164             :     }
     165             : 
     166             :     /*---------------------------------------------------------------*
     167             :      * preemphasis with scaling (L_FRAME+L_FILT)
     168             :      * now do the actual preemphasis, since we have the
     169             :      * proper scaling factor.
     170             :      * Done backwards to save storage space
     171             :      *---------------------------------------------------------------*/
     172             : 
     173        6200 :     tmp_fixed = new_speech[Lframe - 1]; // Q_new
     174        6200 :     move16();
     175             : 
     176     1785600 :     FOR( i = sub( Lframe, 1 ); i > 0; i-- )
     177             :     {
     178     1779400 :         L_tmp = L_mult( new_speech[i], QVal );
     179             : #ifdef ISSUE_1867_replace_overflow_libenc
     180     1779400 :         L_tmp = L_msu_sat( L_tmp, new_speech[i - 1], mu );
     181             : #else
     182             :         L_tmp = L_msu_o( L_tmp, new_speech[i - 1], mu, &Overflow );
     183             : #endif
     184     1779400 :         L_tmp = L_shl( L_tmp, *Q_new );
     185     1779400 :         new_speech[i] = round_fx_sat( L_tmp ); // Q_new
     186             :     }
     187             : 
     188        6200 :     L_tmp = L_mult( new_speech[0], QVal );
     189        6200 :     L_tmp = L_msu_sat( L_tmp, *mem_preemph, mu );
     190        6200 :     L_tmp = L_shl_sat( L_tmp, *Q_new );
     191        6200 :     new_speech[0] = round_fx_sat( L_tmp );
     192        6200 :     *mem_preemph = tmp_fixed;
     193        6200 :     move16();
     194        6200 : }
     195             : /*-------------------------------------------------------------------*
     196             :  * Scale_mem
     197             :  *
     198             :  * Rescale memories
     199             :  *-------------------------------------------------------------------*/
     200        3100 : Word32 Scale_mem_pre_proc(                       /* o  : Min energy scaled           */
     201             :                            Word16 ini_frame_fx,  /* i  : Frame number                Q0*/
     202             :                            Word16 Q_exp,         /* i  : Diff scaling factor         */
     203             :                            Word16 *Q_new,        /* i/o: Absolute scaling factor     */
     204             :                            Word16 *old_speech,   /* i/o: Speech memory               */
     205             :                            Word16 *mem_wsp,      /* i/o: wsp vector memory           st->mem_wsp_q*/
     206             :                            Word32 *enrO,         /* i/o: Enr mem                     q_enrO*/
     207             :                            Word32 *bckr,         /* i/o: Back ground_fx ener mem     q_bckr*/
     208             :                            Word32 *ave_enr,      /* i/o: Ave_enr mem                 Q_new + QSCALE*/
     209             :                            Word32 *ave_enr2,     /* i/o: Ave_enr2 mem                Q_new + QSCALE*/
     210             :                            Word32 *st_fr_bands1, /* i/o: spectrum per critical bands of the previous frame  Q_new + QSCALE*/
     211             :                            Word32 *st_fr_bands2, /* i/o: spectrum per critical bands 2 frames ago           Q_new + QSCALE*/
     212             :                            Word32 *st_Bin_E_old )
     213             : {
     214             :     Word16 i;
     215             :     Word32 e_min_scaled;
     216             : #ifndef ISSUE_1867_replace_overflow_libenc
     217             : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
     218             :     Flag Overflow = 0;
     219             : #endif
     220             : #endif
     221             : 
     222        3100 :     e_min_scaled = L_shr_r( L_add( L_shr( E_MIN_FXQ15, sub( 14, add( *Q_new, QSCALE ) ) ), 1 ), 1 );
     223             : 
     224             :     /* scale previous samples and memory (Q_exp - Q_new - Q_old) */
     225             :     /* Scale( x, y, z ) : shift left vector x of size y by z bits ) */
     226        3100 :     IF( Q_exp != 0 )
     227             :     {
     228         567 :         IF( old_speech != NULL )
     229             :         {
     230         567 :             Scale_sig( old_speech, L_INP_MEM, Q_exp );
     231             :         }
     232         567 :         Scale_sig( mem_wsp, 1, Q_exp );
     233         567 :         IF( ini_frame_fx == 0 )
     234             :         {
     235             :             /* Scaling noise vectors if frame ==1*/
     236           3 :             Scale_sig32( enrO, NB_BANDS, *Q_new );
     237           3 :             Scale_sig32( bckr, NB_BANDS, *Q_new );
     238           3 :             Scale_sig32( ave_enr, NB_BANDS, *Q_new );
     239           3 :             Scale_sig32( ave_enr2, NB_BANDS, *Q_new );
     240           3 :             Scale_sig32( st_fr_bands1, NB_BANDS, *Q_new );
     241           3 :             Scale_sig32( st_fr_bands2, NB_BANDS, *Q_new );
     242           3 :             Scale_sig32( st_Bin_E_old, L_FFT / 2, *Q_new );
     243             :         }
     244             :         ELSE
     245             :         {
     246             :             /* Do scaling and valide minimum energy value */
     247       11844 :             FOR( i = 0; i < NB_BANDS; i++ )
     248             :             {
     249             : #ifdef ISSUE_1867_replace_overflow_libenc
     250       11280 :                 enrO[i] = L_max( L_shl_sat( enrO[i], Q_exp ), e_min_scaled );
     251       11280 :                 bckr[i] = L_max( L_shl_sat( bckr[i], Q_exp ), e_min_scaled );
     252       11280 :                 ave_enr[i] = L_max( L_shl_sat( ave_enr[i], Q_exp ), e_min_scaled );
     253       11280 :                 ave_enr2[i] = L_max( L_shl_sat( ave_enr2[i], Q_exp ), e_min_scaled );
     254       11280 :                 st_fr_bands1[i] = L_max( L_shl_sat( st_fr_bands1[i], Q_exp ), e_min_scaled );
     255       11280 :                 st_fr_bands2[i] = L_max( L_shl_sat( st_fr_bands2[i], Q_exp ), e_min_scaled );
     256             : #else
     257             :                 enrO[i] = L_max( L_shl_o( enrO[i], Q_exp, &Overflow ), e_min_scaled );
     258             :                 bckr[i] = L_max( L_shl_o( bckr[i], Q_exp, &Overflow ), e_min_scaled );
     259             :                 ave_enr[i] = L_max( L_shl_o( ave_enr[i], Q_exp, &Overflow ), e_min_scaled );
     260             :                 ave_enr2[i] = L_max( L_shl_o( ave_enr2[i], Q_exp, &Overflow ), e_min_scaled );
     261             :                 st_fr_bands1[i] = L_max( L_shl_o( st_fr_bands1[i], Q_exp, &Overflow ), e_min_scaled );
     262             :                 st_fr_bands2[i] = L_max( L_shl_o( st_fr_bands2[i], Q_exp, &Overflow ), e_min_scaled );
     263             : #endif
     264       11280 :                 move32();
     265       11280 :                 move32();
     266       11280 :                 move32();
     267       11280 :                 move32();
     268       11280 :                 move32();
     269       11280 :                 move32();
     270             :             }
     271             :         }
     272             :     }
     273        3100 :     return e_min_scaled;
     274             : }
     275             : 
     276        3100 : void Scale_mem_enc(
     277             :     Word16 Q_exp,              /* i  : Diff scaling factor         */
     278             :     Word16 *old_speech16k,     /* i/o: Speech memory               */
     279             :     Word16 *old_exc,           /* i/o: excitation memory           */
     280             :     Word16 *old_bwe_exc,       /* i/o: BWE excitation memory       */
     281             :     Word16 *mem_w0,            /* i/o: target vector memory        */
     282             :     Word16 *mem_syn,           /* i/o: synthesis memory            */
     283             :     Word16 *mem_syn2,          /* i/o: synthesis memory            */
     284             :     Word16 *mem_deemp_preQ_fx, /*i/o: deemphasis memory for the high rate celp codec */
     285             :     Word16 *last_exc_dct_in,
     286             :     Word16 *old_input_lp )
     287             : {
     288             :     /* scale previous samples and memory (Q_exp - Q_new - Q_old) */
     289             :     /* Scale( x, y, z ) : shift left vector x of size y by z bits ) */
     290        3100 :     IF( Q_exp != 0 )
     291             :     {
     292         582 :         Scale_sig( old_speech16k, L_INP_MEM, Q_exp );
     293         582 :         Scale_sig( mem_w0, 1, Q_exp );
     294             :         /* Scaling excitation */
     295         582 :         Scale_sig( old_exc, L_EXC_MEM, Q_exp );
     296         582 :         Scale_sig( old_bwe_exc, PIT16k_MAX * 2, Q_exp );
     297         582 :         Scale_sig( mem_syn, M, Q_exp );
     298         582 :         Scale_sig( mem_syn2, M, Q_exp );
     299         582 :         Scale_sig( last_exc_dct_in, L_FRAME, Q_exp );
     300         582 :         Scale_sig( mem_deemp_preQ_fx, 1, Q_exp );
     301         582 :         Scale_sig( old_input_lp, NS2SA( 16000, ACELP_LOOK_NS + DELAY_SWB_TBE_16k_NS + DELAY_FIR_RESAMPL_NS ), Q_exp );
     302             :     }
     303             : 
     304        3100 :     return;
     305             : }

Generated by: LCOV version 1.14