LCOV - code coverage report
Current view: top level - lib_com - fine_gain_bits_fx.c (source / functions) Hit Total Coverage
Test: Coverage on main @ b9bfbe380d1c207f5198ba67a82398b3d313550e Lines: 65 66 98.5 %
Date: 2025-11-15 04:01:59 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 <stdint.h>
       6             : #include "options.h" /* Compilation switches                   */
       7             : #include "rom_com.h" /* Static table prototypes                */
       8             : #include "prot_fx.h" /* Function prototypes                    */
       9             : #include "ivas_prot_fx.h"
      10             : 
      11             : 
      12             : /*--------------------------------------------------------------------------
      13             :  * subband_gain_bits()
      14             :  *
      15             :  * HQ core encoder
      16             :  *--------------------------------------------------------------------------*/
      17         916 : static void subband_gain_bits_fx(
      18             :     const Word16 *Rk,     /* i  : bit allocation per band Q3 */
      19             :     const Word16 N,       /* i  : number of bands         */
      20             :     Word16 *bits,         /* o  : gain bits per band      */
      21             :     const Word16 *sfmsize /* i  : Size of bands           */
      22             : )
      23             : {
      24             :     Word16 i, b, tot;
      25             :     Word16 bps;
      26             : 
      27         916 :     tot = 0;
      28         916 :     move16();
      29             : 
      30       36000 :     FOR( i = 0; i < N; i++ )
      31             :     {
      32             :         /*bps = (short)(Rk[i]*((word16)min(32767, ceil(32767.0f/sfmsize[i]);  inexact C-integer division approx. */
      33       35084 :         bps = extract_l( L_shr( L_mult0( Rk[i], inv_tbl_fx[sfmsize[i]] ), 18 ) ); /* 3+15 */
      34       35084 :         if ( EQ_32( L_shl( L_mult0( sfmsize[i], add( bps, 1 ) ), 3 ), Rk[i] ) )
      35             :         {
      36        7182 :             bps = add( bps, 1 );
      37             :         }
      38             : 
      39       35084 :         bps = s_min( 7, bps );
      40       35084 :         b = fine_gain_bits[bps];
      41       35084 :         move16();
      42       35084 :         bits[i] = b;
      43       35084 :         move16();
      44       35084 :         tot = add( tot, b );
      45             :     }
      46             : 
      47         916 :     if ( tot == 0 )
      48             :     {
      49             :         /* If no gain bits were assigned, use one bit anyway for potential PVQ overage */
      50          36 :         bits[0] = 1;
      51          36 :         move16();
      52             :     }
      53             : 
      54         916 :     return;
      55             : }
      56             : 
      57             : /*--------------------------------------------------------------------------*
      58             :  * assign_gain_bits()
      59             :  *
      60             :  * Assign gain adjustment bits and update bit budget
      61             :  *--------------------------------------------------------------------------*/
      62             : 
      63       26455 : Word16 assign_gain_bits_fx(                           /* o  : Number of assigned gain bits          */
      64             :                             const Word16 core,        /* i  : HQ core                               */
      65             :                             const Word16 BANDS,       /* i  : Number of bands                       */
      66             :                             const Word16 *band_width, /* i  : Sub band bandwidth                    */
      67             :                             Word16 *Rk,               /* i/o: Bit allocation/Adjusted bit alloc. Q3 */
      68             :                             Word16 *gain_bits_array,  /* o  : Assigned gain bits                    */
      69             :                             Word16 *Rcalc             /* o  : Bit budget for shape quantizer     Q3 */
      70             : )
      71             : {
      72             :     Word16 gain_bits_tot;
      73             :     Word16 i;
      74             : 
      75             :     /* Allocate gain bits for every subband used, based on bit rate and bandwidth */
      76       26455 :     IF( EQ_16( core, HQ_CORE ) )
      77             :     {
      78         916 :         subband_gain_bits_fx( Rk, BANDS, gain_bits_array, band_width );
      79             :     }
      80             :     ELSE
      81             :     {
      82       25539 :         set16_fx( gain_bits_array, 0, BANDS );
      83             :     }
      84             : 
      85             :     /* Re-adjust bit budget for gain quantization */
      86       26455 :     gain_bits_tot = 0;
      87       26455 :     move16();
      88       26455 :     *Rcalc = 0;
      89       26455 :     move16();
      90      247128 :     FOR( i = 0; i < BANDS; i++ )
      91             :     {
      92      220673 :         IF( Rk[i] > 0 )
      93             :         {
      94      192096 :             Rk[i] = sub( Rk[i], shl( gain_bits_array[i], 3 ) );
      95      192096 :             move16();
      96      192096 :             gain_bits_tot = add( gain_bits_tot, gain_bits_array[i] );
      97      192096 :             *Rcalc = add( *Rcalc, Rk[i] );
      98      192096 :             move16();
      99             :         }
     100             :     }
     101             : 
     102       26455 :     return gain_bits_tot;
     103             : }
     104             : 
     105       15180 : static void ivas_subband_gain_bits_fx(
     106             :     const Word16 *Rk,     /* i  : bit allocation per band Q3 */
     107             :     const Word16 N,       /* i  : number of bands         */
     108             :     Word16 *bits,         /* o  : gain bits per band      */
     109             :     const Word16 *sfmsize /* i  : Size of bands           */
     110             : )
     111             : {
     112             :     Word16 i, b, tot;
     113             :     Word16 bps;
     114             : 
     115       15180 :     tot = 0;
     116       15180 :     move16();
     117             : 
     118      632444 :     FOR( i = 0; i < N; i++ )
     119             :     {
     120             :         /*bps = (short)(Rk[i]*((word16)min(32767, ceil(32767.0f/sfmsize[i]);  inexact C-integer division approx. */
     121      617264 :         bps = extract_l( L_shr( L_mult0( Rk[i], fg_inv_tbl_fx[sfmsize[i] >> 3] ), 18 ) ); /* 3+15 */
     122      617264 :         if ( EQ_32( L_shl( L_mult0( sfmsize[i], add( bps, 1 ) ), 3 ), Rk[i] ) )
     123             :         {
     124           0 :             bps = add( bps, 1 );
     125             :         }
     126             : 
     127      617264 :         bps = s_min( 7, bps );
     128      617264 :         b = fine_gain_bits[bps];
     129      617264 :         move16();
     130      617264 :         bits[i] = b;
     131      617264 :         move16();
     132      617264 :         tot = add( tot, b );
     133             :     }
     134             : 
     135       15180 :     if ( tot == 0 )
     136             :     {
     137             :         /* If no gain bits were assigned, use one bit anyway for potential PVQ overage */
     138        2833 :         bits[0] = 1;
     139        2833 :         move16();
     140             :     }
     141             : 
     142       15180 :     return;
     143             : }
     144             : 
     145       40624 : Word16 ivas_assign_gain_bits_fx(                           /* o  : Number of assigned gain bits          */
     146             :                                  const Word16 core,        /* i  : HQ core                               */
     147             :                                  const Word16 BANDS,       /* i  : Number of bands                       */
     148             :                                  const Word16 *band_width, /* i  : Sub band bandwidth                    */
     149             :                                  Word16 *Rk,               /* i/o: Bit allocation/Adjusted bit alloc. Q3 */
     150             :                                  Word16 *gain_bits_array,  /* o  : Assigned gain bits                    */
     151             :                                  Word16 *Rcalc             /* o  : Bit budget for shape quantizer     Q3 */
     152             : )
     153             : {
     154             :     Word16 gain_bits_tot;
     155             :     Word16 i;
     156             : 
     157             :     /* Allocate gain bits for every subband used, based on bit rate and bandwidth */
     158       40624 :     IF( EQ_16( core, HQ_CORE ) )
     159             :     {
     160       15180 :         ivas_subband_gain_bits_fx( Rk, BANDS, gain_bits_array, band_width );
     161             :     }
     162             :     ELSE
     163             :     {
     164       25444 :         set16_fx( gain_bits_array, 0, BANDS );
     165             :     }
     166             : 
     167             :     /* Re-adjust bit budget for gain quantization */
     168       40624 :     gain_bits_tot = 0;
     169       40624 :     move16();
     170       40624 :     *Rcalc = 0;
     171       40624 :     move16();
     172      845534 :     FOR( i = 0; i < BANDS; i++ )
     173             :     {
     174      804910 :         IF( Rk[i] > 0 )
     175             :         {
     176      485600 :             Rk[i] = sub( Rk[i], shl( gain_bits_array[i], 3 ) );
     177      485600 :             move16();
     178      485600 :             gain_bits_tot = add( gain_bits_tot, gain_bits_array[i] );
     179      485600 :             *Rcalc = add( *Rcalc, Rk[i] );
     180      485600 :             move16();
     181             :         }
     182             :     }
     183             : 
     184       40624 :     return gain_bits_tot;
     185             : }

Generated by: LCOV version 1.14