LCOV - code coverage report
Current view: top level - lib_com - ivas_stereo_td_bit_alloc_fx.c (source / functions) Hit Total Coverage
Test: Coverage on main enc/dec/rend @ ca3146eb9de8185ed0247945c643267826a32a94 Lines: 262 356 73.6 %
Date: 2025-08-26 01:31:27 Functions: 7 8 87.5 %

          Line data    Source code
       1             : /******************************************************************************************************
       2             : 
       3             :    (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
       4             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
       5             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
       6             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
       7             :    contributors to this repository. All Rights Reserved.
       8             : 
       9             :    This software is protected by copyright law and by international treaties.
      10             :    The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
      11             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
      12             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
      13             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
      14             :    contributors to this repository retain full ownership rights in their respective contributions in
      15             :    the software. This notice grants no license of any kind, including but not limited to patent
      16             :    license, nor is any license granted by implication, estoppel or otherwise.
      17             : 
      18             :    Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
      19             :    contributions.
      20             : 
      21             :    This software is provided "AS IS", without any express or implied warranties. The software is in the
      22             :    development stage. It is intended exclusively for experts who have experience with such software and
      23             :    solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
      24             :    and fitness for a particular purpose are hereby disclaimed and excluded.
      25             : 
      26             :    Any dispute, controversy or claim arising under or in relation to providing this software shall be
      27             :    submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
      28             :    accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
      29             :    the United Nations Convention on Contracts on the International Sales of Goods.
      30             : 
      31             : *******************************************************************************************************/
      32             : 
      33             : #include <stdint.h>
      34             : #include "options.h"
      35             : #include "cnst.h"
      36             : #include "stat_enc.h"
      37             : #include "rom_com.h"
      38             : #include "ivas_rom_com.h"
      39             : #include "ivas_cnst.h"
      40             : #include "prot_fx.h"
      41             : #include "wmc_auto.h"
      42             : #include "ivas_prot_fx.h"
      43             : #include "ivas_rom_com_fx.h"
      44             : 
      45             : 
      46             : /*-------------------------------------------------------------------*
      47             :  * Local constants
      48             :  *-------------------------------------------------------------------*/
      49             : 
      50             : #define NB_RATE_POSS     10                              /* Nmbr of possible FCB bitrate for half rate mode*/
      51             : #define MIN_SEC_ACB_RATE ( 2 * 8 * FRAMES_PER_SEC )      /* 2 subfr ACB bitrate */
      52             : #define MIN_SEC_LPC_RATE ( ( 24 + 2 ) * FRAMES_PER_SEC ) /* LPC min rate */
      53             : 
      54             : #define MAX_SC_FCB_RATE     ( 24 + 1 ) /* Maximum bit for a FCB subfr */
      55             : #define MIN_4SUBFR_FCB_RATE 40         /* Minimum 4 subfr bitrate (4x10 bits) */
      56             : #define MIN_GAIN_BITS       6          /* Minimum number of bits used */
      57             : 
      58             : #define TDM_UC_NORMAL_MODE_MBRATE      9000                        /* Max bitrate for normal UC if LP coded */
      59             : #define TDM_UC_NORMAL_MODE_MBRATE_LP_R 8200                        /* Normal bitrate for normal UC if LP is reused */
      60             : #define TDM_UC_NORMAL_MODE_MINBR_LP_R  7000                        /* If bitrate is below 7k, add back MID_LP_BRATE */
      61             : #define MID_LP_BRATE                   ( 31 + 5 ) * FRAMES_PER_SEC /* average bitrate for LP */
      62             : #define MAX_TDM_UC_BRATE               11000                       /* Maximum bitrate for tdm normal UC mode */
      63             : 
      64             : #define MIN_SIGN_RATE    ( TDM_SIGNAL_BITS_READ_FROM_THE_END_OF_BS * FRAMES_PER_SEC ) /* min. 2 time 12 bits FCB, used only when LPC and/or pitch is transmitted    */
      65             : #define MIN_FCB_SECRATE2 ( 2 * 16 * FRAMES_PER_SEC )                                  /* min. 2 time 14 bits FCB, used only when LPC and/or pitch is transmitted    */
      66             : 
      67             : 
      68             : /*-------------------------------------------------------------------*
      69             :  * tdm_bit_alloc()
      70             :  *
      71             :  * Bitbudget distribution between Primary and Secondary channel in TD stereo
      72             :  *-------------------------------------------------------------------*/
      73        3630 : void tdm_bit_alloc(
      74             :     const Word16 ivas_format,           /* i  : IVAS format                          Q0*/
      75             :     const Word16 ism_mode,              /* i  : ISM mode in combined format          Q0*/
      76             :     const Word32 element_brate_wo_meta, /* i  : element bitrate without metadata     Q0*/
      77             :     const Word16 tdm_lp_reuse_flag,     /* i  : LPC reusage flag                     Q0*/
      78             :     Word32 *total_brate_pri,            /* o  : Allocated primary channel bitrate    Q0*/
      79             :     Word32 *total_brate_sec,            /* o  : Allocated secondary channel bitrate  Q0*/
      80             :     Word16 *tdm_low_rate_mode,          /* o  : secondary channel low rate mode flag Q0*/
      81             :     const Word16 coder_type,            /* i  : secondary channel coder type         Q0*/
      82             :     const Word16 ener_ratio_idx,        /* i  : correlation ratio indexe             Q0*/
      83             :     const Word16 tdm_Pitch_reuse_flag,  /* i  : primary channel pitch reuse flag     Q0*/
      84             :     const Word16 bwidth_pri,            /* i  : bandwidth of the primary channel     Q0*/
      85             :     const Word16 bwidth_sec,            /* i  : bandwidth of the secondary channel   Q0*/
      86             :     const Word16 flag_ACELP16k_pri,     /* i  : ACELP@16kHz core flag, primary chan. Q0*/
      87             :     const Word16 tdm_LRTD_flag,         /* i  : LRTD stereo mode flag                Q0*/
      88             :     const Word16 coder_type0,           /* i  : coder type (temporary in the encoder, from bitstream in decoder) Q0*/
      89             :     const Word16 tdm_inst_ratio_idx_ref /* i  : instantaneous correlation ratio idx Q0*/
      90             : )
      91             : {
      92             :     Word16 idx, four_subfr_fcb, two_subfr_fcb;
      93             :     Word32 bit_rate_diff_fx;
      94             :     Word32 BWE_brate;
      95             :     Word16 tmp_bits;
      96        3630 :     Word16 tdm_inst_ratio_idx = tdm_inst_ratio_idx_ref; /*Q0*/
      97        3630 :     move16();
      98        3630 :     if ( EQ_16( tdm_inst_ratio_idx, TDM_NQ ) )
      99             :     {
     100         100 :         tdm_inst_ratio_idx = LRTD_STEREO_MID_IS_PRIM; /* Bit rate almost split half and half*/
     101         100 :         move16();
     102             :     }
     103             : 
     104             :     /* Decision on using the low rate mode or the normal mode */
     105             :     /* default is using the low rate mode for the secondary channel coding*/
     106             :     /* UC and IC are automatically coded with low rate mode */
     107        3630 :     *tdm_low_rate_mode = 1;
     108        3630 :     move16();
     109             : 
     110             :     /* Allocating different bitrate to channels */
     111        3630 :     idx = 0;
     112        3630 :     move16();
     113        3630 :     IF( LE_32( element_brate_wo_meta, IVAS_13k2 ) )
     114             :     {
     115           0 :         idx = 0;
     116           0 :         move16();
     117             :     }
     118        3630 :     ELSE IF( LE_32( element_brate_wo_meta, IVAS_16k4 ) )
     119             :     {
     120         562 :         idx = 1;
     121         562 :         move16();
     122             :     }
     123        3068 :     ELSE IF( LE_32( element_brate_wo_meta, IVAS_24k4 ) )
     124             :     {
     125        1113 :         idx = 2;
     126        1113 :         move16();
     127             :     }
     128        1955 :     ELSE IF( LE_32( element_brate_wo_meta, IVAS_32k ) )
     129             :     {
     130        1955 :         idx = 3;
     131        1955 :         move16();
     132             :     }
     133           0 :     ELSE IF( LE_32( element_brate_wo_meta, IVAS_48k ) )
     134             :     {
     135           0 :         idx = 4;
     136           0 :         move16();
     137             :     }
     138             : 
     139        3630 :     test();
     140        3630 :     if ( EQ_16( coder_type, UNVOICED ) && GE_16( tdm_bit_allc_tbl[idx][coder_type], 4200 ) )
     141             :     {
     142           0 :         *tdm_low_rate_mode = 0;
     143           0 :         move16();
     144             :     }
     145             : 
     146             :     /* Secondary channel based bitrate allocation */
     147        3630 :     *total_brate_sec = tdm_bit_allc_tbl[idx][coder_type]; /*Q0*/
     148        3630 :     move16();
     149             : 
     150        3630 :     test();
     151        3630 :     test();
     152        3630 :     test();
     153        3630 :     test();
     154        3630 :     test();
     155        3630 :     test();
     156             :     /* secondary channel bitrate allocation based on the energy scaling ratio */
     157        3630 :     IF( ( ( NE_16( ivas_format, MASA_ISM_FORMAT ) || ism_mode == ISM_MODE_NONE ) && ( ( NE_16( coder_type, UNVOICED ) ) || EQ_16( tdm_LRTD_flag, 1 ) ) ) || ( EQ_16( ivas_format, MASA_ISM_FORMAT ) && ism_mode != ISM_MODE_NONE && GT_16( coder_type, UNVOICED ) ) )
     158        3614 :     {
     159        3614 :         bit_rate_diff_fx = L_sub( element_brate_wo_meta, L_shl( *total_brate_sec, 1 ) ); /*Q0*/
     160             : 
     161        3614 :         IF( EQ_16( tdm_LRTD_flag, 1 ) ) /* >  element_brate > STEREO_22k or CT0 not used */
     162             :         {
     163             :             /* further adjustment in function of the energy/correlation ratio */
     164        3549 :             IF( coder_type == INACTIVE )
     165             :             {
     166           0 :                 Word32 res_fix = 0;
     167           0 :                 move32();
     168           0 :                 res_fix = Mpy_32_32( 644245095 /*0.3f in Q31*/, L_sub( element_brate_wo_meta, 500 ) ); /*Q0*/
     169           0 :                 res_fix = imult3216( Mpy_32_32( L_abs( res_fix ), 21474837 /*0.01 in Q31*/ ), 100 );   /*Q0*/
     170           0 :                 if ( res_fix < 0 )
     171             :                 {
     172           0 :                     res_fix = L_negate( res_fix );
     173             :                 }
     174           0 :                 *total_brate_sec = L_max( *total_brate_sec, res_fix ); /*Q0*/
     175           0 :                 move32();
     176             : 
     177           0 :                 tmp_bits = imult1616( negate( abs_s( sub( tdm_inst_ratio_idx, 16 ) ) ), imult1616( idx, 200 ) ); /*Q0*/
     178             :             }
     179             :             ELSE
     180             :             {
     181        3549 :                 Word32 res_fix = 0;
     182        3549 :                 move32();
     183        3549 :                 res_fix = Mpy_32_32( 1073741824 /*0.5f in Q31*/, L_sub( element_brate_wo_meta, 500 ) ); /*Q0*/
     184        3549 :                 res_fix = imult3216( Mpy_32_32( L_abs( res_fix ), 21474837 /*0.01 in Q31*/ ), 100 );    /*Q0*/
     185        3549 :                 if ( res_fix < 0 )
     186             :                 {
     187           0 :                     res_fix = L_negate( res_fix );
     188             :                 }
     189        3549 :                 *total_brate_sec = L_max( *total_brate_sec, res_fix ); /*Q0*/
     190        3549 :                 move32();
     191             : 
     192        3549 :                 tmp_bits = imult1616( negate( abs_s( sub( tdm_inst_ratio_idx, 16 ) ) ), imult1616( 100, idx ) ); /*Q0*/
     193             :             }
     194             : 
     195             :             /* tmp_bits should be subtract from the secondary channel bitrate */
     196             :             /* IF the primary channel doesn't correspond to the channel having the highest correlation to the mono- inverse the bitrate compensation */
     197        3549 :             test();
     198        3549 :             test();
     199        3549 :             test();
     200        3549 :             if ( ( GE_16( ener_ratio_idx, LRTD_STEREO_MID_IS_PRIM ) && LT_16( tdm_inst_ratio_idx, LRTD_STEREO_MID_IS_PRIM ) ) || ( LT_16( ener_ratio_idx, LRTD_STEREO_MID_IS_PRIM ) && GE_16( tdm_inst_ratio_idx, LRTD_STEREO_MID_IS_PRIM ) ) )
     201             :             {
     202        1558 :                 tmp_bits = negate( tmp_bits );
     203             :             }
     204        3549 :             bit_rate_diff_fx = tmp_bits;
     205        3549 :             move16();
     206             :         }
     207             :         ELSE
     208             :         {
     209          65 :             IF( LT_16( ener_ratio_idx, LRTD_STEREO_MID_IS_PRIM ) )
     210             :             {
     211             : 
     212          65 :                 bit_rate_diff_fx = imult3216( bit_rate_diff_fx, sub( LRTD_STEREO_MID_IS_PRIM, ener_ratio_idx ) ); /*Q0*/
     213          65 :                 bit_rate_diff_fx = L_shr( bit_rate_diff_fx, 1 );                                                  /*Q0*/
     214          65 :                 bit_rate_diff_fx = Mpy_32_16_1( L_abs( bit_rate_diff_fx ), 3277 /*0.1f in Q15*/ );                /*Q0*/
     215          65 :                 if ( bit_rate_diff_fx < 0 )
     216             :                 {
     217           0 :                     bit_rate_diff_fx = L_negate( bit_rate_diff_fx );
     218             :                 }
     219             :             }
     220             :             ELSE
     221             :             {
     222           0 :                 bit_rate_diff_fx = imult3216( bit_rate_diff_fx, sub( ener_ratio_idx, LRTD_STEREO_MID_IS_PRIM ) ); /*Q0*/
     223           0 :                 bit_rate_diff_fx = L_shr( bit_rate_diff_fx, 1 );                                                  /*Q0*/
     224           0 :                 bit_rate_diff_fx = Mpy_32_16_1( L_abs( bit_rate_diff_fx ), 3277 /*0.1f in Q15*/ );                /*Q0*/
     225           0 :                 if ( bit_rate_diff_fx < 0 )
     226             :                 {
     227           0 :                     bit_rate_diff_fx = L_negate( bit_rate_diff_fx );
     228             :                 }
     229             :             }
     230             :         }
     231             :         /*bit_rate_diff2 = ((Word16)(10.f*(-0.5f*ener_ratio_LR+0.5f)*bit_rate_diff)/100)*100;*/
     232        3614 :         Word32 temp = imult3216( Mpy_32_16_1( L_abs( bit_rate_diff_fx ), 328 /*0.01f in Q15*/ ), 100 ); /*Q0*/
     233        3614 :         if ( bit_rate_diff_fx < 0 )
     234             :         {
     235        1920 :             temp = L_negate( temp );
     236             :         }
     237        3614 :         *total_brate_sec = L_add( *total_brate_sec, temp ); /*Q0*/
     238        3614 :         move32();
     239             : 
     240        3614 :         *total_brate_sec = L_max( *total_brate_sec, tdm_bit_allc_tbl[idx][coder_type] ); /*Q0*/
     241        3614 :         move32();
     242             : 
     243        3614 :         test();
     244        3614 :         IF( ( coder_type == INACTIVE ) && tdm_LRTD_flag == 0 )
     245             :         {
     246           0 :             *total_brate_sec = L_min( *total_brate_sec, MIN_BRATE_SWB_BWE ); /*Q0*/
     247           0 :             move32();
     248             :         }
     249             : 
     250        3614 :         test();
     251        3614 :         test();
     252        3614 :         IF( ( LE_16( ener_ratio_idx, 1 ) || GE_16( ener_ratio_idx, 29 ) ) && GE_16( coder_type, UNVOICED ) )
     253        3563 :         {
     254        3563 :             Word16 delta_brate = 0;
     255        3563 :             move16();
     256             : 
     257        3563 :             IF( GT_16( bwidth_pri, WB ) )
     258             :             {
     259        2783 :                 delta_brate = 600; /* To slightly compensate for SWB BWE instead of WB BWE Q0*/
     260        2783 :                 move16();
     261        2783 :                 if ( LE_32( element_brate_wo_meta, IVAS_16k4 ) )
     262             :                 {
     263         446 :                     delta_brate = 1250; /* To compensate for SWB BWE instead of WB BWE Q0*/
     264         446 :                     move16();
     265             :                 }
     266             :             }
     267             : 
     268        3563 :             IF( LE_32( element_brate_wo_meta, IVAS_13k2 ) )
     269             :             {
     270           0 :                 *total_brate_sec = L_max( *total_brate_sec, add( 5600, delta_brate ) ); /* ~42-47 % of the total bitrate */
     271           0 :                 move32();
     272             :             }
     273        3563 :             ELSE IF( LE_32( element_brate_wo_meta, IVAS_16k4 ) )
     274             :             {
     275         547 :                 *total_brate_sec = L_max( *total_brate_sec, add( 6500, delta_brate ) ); /* ~40-43 % of the total bitrate Q0*/
     276         547 :                 move32();
     277             :             }
     278        3016 :             ELSE IF( LE_32( element_brate_wo_meta, IVAS_24k4 ) )
     279             :             {
     280        1091 :                 *total_brate_sec = L_max( *total_brate_sec, add( 9000, delta_brate ) ); /* ~37-39 % of the total bitrate Q0*/
     281        1091 :                 move32();
     282             :             }
     283             :             ELSE
     284             :             {
     285        1925 :                 *total_brate_sec = L_max( *total_brate_sec, add( 9600, delta_brate ) ); /* ~30-32% of the total bitrate Q0*/
     286        1925 :                 move32();
     287             :             }
     288             :         }
     289             :         ELSE
     290             :         {
     291          51 :             *total_brate_sec = L_min( *total_brate_sec, Mpy_32_32( 9663677 /*0.0045f in Q31*/, element_brate_wo_meta ) * 100 ); /*Q0*/
     292          51 :             move32();
     293             :         }
     294             : 
     295        3614 :         *total_brate_sec = L_min( *total_brate_sec, 18000 ); /*Q0*/
     296        3614 :         move32();
     297             :     }
     298          16 :     ELSE IF( EQ_16( coder_type, UNVOICED ) )
     299             :     {
     300          16 :         IF( tdm_lp_reuse_flag == 0 )
     301             :         {
     302           8 :             *total_brate_sec = L_add( *total_brate_sec, imult3216( FRAMES_PER_SEC, 36 ) ); /*Q0*/
     303           8 :             move32();
     304             :         }
     305             :     }
     306             : 
     307        3630 :     IF( LE_16( coder_type, UNVOICED ) )
     308             :     {
     309          94 :         *total_brate_sec = L_min( *total_brate_sec, MAX_TDM_UC_BRATE ); /*Q0*/
     310          94 :         move32();
     311             : 
     312          94 :         test();
     313          94 :         test();
     314          94 :         test();
     315          94 :         test();
     316          94 :         test();
     317          94 :         IF( GE_32( *total_brate_sec, TDM_UC_NORMAL_MODE_MBRATE ) && tdm_lp_reuse_flag == 0 )
     318             :         {
     319          68 :             *tdm_low_rate_mode = 0;
     320          68 :             move16();
     321             :         }
     322          26 :         ELSE IF( GE_32( *total_brate_sec, TDM_UC_NORMAL_MODE_MBRATE_LP_R ) )
     323             :         {
     324           0 :             *tdm_low_rate_mode = 0;
     325           0 :             move16();
     326             :         }
     327          26 :         ELSE IF( ( tdm_lp_reuse_flag == 0 && LT_32( *total_brate_sec, TDM_UC_NORMAL_MODE_MINBR_LP_R ) && EQ_16( coder_type, UNVOICED ) ) || ( tdm_lp_reuse_flag == 0 && LE_32( *total_brate_sec, L_deposit_l( add( tdm_bit_allc_tbl[idx][0], MID_LP_BRATE ) ) ) ) )
     328             :         {
     329           8 :             *total_brate_sec = L_add( *total_brate_sec, MID_LP_BRATE ); /*Q0*/
     330           8 :             move32();
     331             :         }
     332             :     }
     333             : 
     334             :     /* verify that primary channel bitrate is higher than the minimum supported bitrate */
     335        3630 :     IF( flag_ACELP16k_pri )
     336             :     {
     337        1977 :         BWE_brate = SWB_TBE_1k75;
     338        1977 :         move32();
     339             : 
     340        1977 :         test();
     341        1977 :         test();
     342        1977 :         if ( LT_32( element_brate_wo_meta, IVAS_24k4 ) )
     343             :         {
     344           0 :             BWE_brate = SWB_TBE_1k10;
     345           0 :             move32();
     346             :         }
     347             : 
     348        1977 :         test();
     349        1977 :         if ( GT_16( bwidth_pri, WB ) && tdm_LRTD_flag == 0 )
     350             :         {
     351          46 :             BWE_brate = L_add( BWE_brate, ( STEREO_BITS_ICBWE + STEREO_ICBWE_MSFLAG_BITS ) * FRAMES_PER_SEC );
     352             :         }
     353        1977 :         test();
     354        1977 :         if ( GT_16( bwidth_pri, SWB ) && EQ_16( tdm_LRTD_flag, 1 ) )
     355             :         {
     356         702 :             BWE_brate = L_add( BWE_brate, 300 );
     357             :         }
     358             : 
     359        1977 :         if ( EQ_16( bwidth_pri, FB ) )
     360             :         {
     361         714 :             BWE_brate = L_add( BWE_brate, ( FB_TBE_1k8 - SWB_TBE_1k75 ) );
     362             :         }
     363             : 
     364        1977 :         IF( LT_32( ( L_sub( ( L_sub( element_brate_wo_meta, *total_brate_sec ) ), BWE_brate ) ), 14000 ) )
     365             :         {
     366         870 :             *total_brate_sec = L_sub( element_brate_wo_meta, L_add( 14000, BWE_brate ) ); /*Q0*/
     367         870 :             move32();
     368             :         }
     369             :     }
     370             :     ELSE
     371             :     {
     372        1653 :         BWE_brate = SWB_TBE_1k75;
     373        1653 :         move32();
     374        1653 :         IF( EQ_16( bwidth_pri, WB ) )
     375             :         {
     376         416 :             BWE_brate = WB_BWE_0k35;
     377         416 :             move32();
     378         416 :             if ( tdm_LRTD_flag == 0 )
     379             :             {
     380           3 :                 BWE_brate = L_add( BWE_brate, 250 ); /* ICA Brate Q0*/
     381             :             }
     382             :         }
     383        1237 :         ELSE IF( tdm_LRTD_flag == 0 )
     384             :         {
     385          12 :             BWE_brate = L_add( BWE_brate, 350 ); /* ICA Brate Q0*/
     386             :         }
     387             :     }
     388             : 
     389        3630 :     IF( EQ_16( coder_type0, TRANSITION ) )
     390             :     {
     391          22 :         IF( GT_32( element_brate_wo_meta, IVAS_13k2 ) )
     392             :         {
     393          22 :             *total_brate_sec = L_min( *total_brate_sec, L_sub( element_brate_wo_meta, L_add( ACELP_8k00, BWE_brate ) ) ); /*Q0*/
     394          22 :             move32();
     395             :         }
     396             :         ELSE
     397             :         {
     398           0 :             *total_brate_sec = L_min( *total_brate_sec, L_sub( element_brate_wo_meta, L_add( ACELP_7k20, BWE_brate ) ) ); /*Q0*/
     399           0 :             move32();
     400             :         }
     401             :     }
     402             :     ELSE
     403             :     {
     404        3608 :         *total_brate_sec = L_min( *total_brate_sec, L_sub( element_brate_wo_meta, L_add( 5900, BWE_brate ) ) ); /*Q0*/
     405        3608 :         move32();
     406             :     }
     407             : 
     408        3630 :     IF( coder_type == INACTIVE )
     409             :     {
     410           0 :         *total_brate_sec = L_max( *total_brate_sec, tdm_bit_allc_tbl[0][0] ); /* sanity check to ensure the secondary channel always gets the minimal bitrate it needs Q0*/
     411           0 :         move32();
     412             :     }
     413             :     ELSE
     414             :     {
     415        3630 :         *total_brate_sec = L_max( *total_brate_sec, 3500 ); /* sanity check to ensure the secondary channel always gets the minimal bitrate it needs Q0*/
     416        3630 :         move32();
     417             :     }
     418             : 
     419             :     /* Secondary channel bitrate adjusment                                                      */
     420             :     /* First, adjust the bitrate depending of what is transmitted                               */
     421             :     /* Second, choose the number of subframe for ACELP core depending of the targetted bitratre */
     422             :     /* Finally, verify that the concordance between the number of subframe, the parameters sent and the bitrate available */
     423        3630 :     IF( EQ_16( coder_type, GENERIC ) /* || coder_type == AUDIO*/ )
     424             :     {
     425             :         /* Adjust the bitrate depending of what is transmitted */
     426             :         /* IF LPC are transmitted, ensure enough bits are used */
     427        3536 :         IF( tdm_lp_reuse_flag == 0 )
     428             :         {
     429             :             /* Pitch is transmitted as well, further increase the bitrate */
     430        3403 :             IF( tdm_Pitch_reuse_flag == 0 )
     431             :             {
     432        3371 :                 *total_brate_sec = L_max( *total_brate_sec, MIN_FCB_SECRATE2 + MIN_SEC_ACB_RATE + MIN_SEC_LPC_RATE + MIN_SIGN_RATE ); /*Q0*/
     433        3371 :                 move32();
     434             : 
     435        3371 :                 test();
     436        3371 :                 IF( EQ_16( tdm_LRTD_flag, 1 ) && EQ_16( bwidth_sec, SWB ) )
     437             :                 {
     438             :                     /* ensure that there are enough bits to code SWB TBE_1k10 as well */
     439        1917 :                     *total_brate_sec = L_max( *total_brate_sec, MIN_FCB_SECRATE2 + MIN_SEC_ACB_RATE + MIN_SEC_LPC_RATE + MIN_SIGN_RATE + SWB_TBE_1k10 ); /*Q0*/
     440        1917 :                     move32();
     441             :                 }
     442             :             }
     443             :             ELSE /* only LPC is tranmitted -> IF ( *total_brate_sec  < MIN_SEC_BRATE+MIN_SEC_LPC_RATE ) */
     444             :             {
     445          32 :                 *total_brate_sec = L_max( *total_brate_sec, MIN_FCB_SECRATE2 + MIN_SEC_LPC_RATE + MIN_SIGN_RATE ); /*Q0*/
     446          32 :                 move32();
     447             :             }
     448             :         }
     449         133 :         ELSE IF( /*tdm_lp_reuse_flag == 1*/ tdm_Pitch_reuse_flag == 0 )
     450             :         {
     451         133 :             *total_brate_sec = L_max( *total_brate_sec, MIN_FCB_SECRATE2 + MIN_SEC_ACB_RATE + MIN_SIGN_RATE ); /*Q0*/
     452         133 :             move32();
     453             :         }
     454             : 
     455             :         /* Choose between 2 and 4 subfr, depending of the bitrate available and prevent the gap between the 2 atlernative */
     456        3536 :         IF( EQ_16( tdm_LRTD_flag, 1 ) )
     457             :         {
     458        3471 :             four_subfr_fcb = extract_l( L_sub( *total_brate_sec, ( TDM_SIGNAL_BITS_READ_FROM_THE_END_OF_BS - STEREO_BITS_TCA + 1 + 4 * MIN_GAIN_BITS ) * FRAMES_PER_SEC ) ); /*Q0*/
     459        3471 :             two_subfr_fcb = extract_l( L_sub( *total_brate_sec, ( TDM_SIGNAL_BITS_READ_FROM_THE_END_OF_BS - STEREO_BITS_TCA + 1 + 2 * MIN_GAIN_BITS ) * FRAMES_PER_SEC ) );  /*Q0*/
     460             :         }
     461             :         ELSE
     462             :         {
     463          65 :             four_subfr_fcb = extract_l( L_sub( *total_brate_sec, ( TDM_SIGNAL_BITS_READ_FROM_THE_END_OF_BS + 1 + 4 * MIN_GAIN_BITS ) * FRAMES_PER_SEC ) ); /*Q0*/
     464          65 :             two_subfr_fcb = extract_l( L_sub( *total_brate_sec, ( TDM_SIGNAL_BITS_READ_FROM_THE_END_OF_BS + 1 + 2 * MIN_GAIN_BITS ) * FRAMES_PER_SEC ) );  /*Q0*/
     465             :         }
     466             : 
     467        3536 :         IF( tdm_lp_reuse_flag == 0 )
     468             :         {
     469        3403 :             four_subfr_fcb = sub( four_subfr_fcb, MIN_SEC_LPC_RATE ); /*Q0*/
     470        3403 :             two_subfr_fcb = sub( two_subfr_fcb, MIN_SEC_LPC_RATE );   /*Q0*/
     471             :         }
     472             : 
     473        3536 :         IF( tdm_Pitch_reuse_flag == 0 )
     474             :         {
     475        3504 :             four_subfr_fcb = sub( four_subfr_fcb, ( MIN_SEC_ACB_RATE + 10 * FRAMES_PER_SEC ) ); /*Q0*/
     476        3504 :             two_subfr_fcb = sub( two_subfr_fcb, MIN_SEC_ACB_RATE );                             /*Q0*/
     477             :         }
     478             : 
     479             :         /* Too much bits for the 2 subfr model but not enough for the the 4 subfr model -> slightly reduce the 2nd channel bitrate */
     480        3536 :         test();
     481        3536 :         IF( GT_16( two_subfr_fcb, 2 * MAX_SC_FCB_RATE * FRAMES_PER_SEC ) && LT_16( four_subfr_fcb, MIN_4SUBFR_FCB_RATE * FRAMES_PER_SEC ) )
     482             :         {
     483           0 :             IF( EQ_16( tdm_LRTD_flag, 1 ) )
     484             :             {
     485           0 :                 *total_brate_sec = 2 * MAX_SC_FCB_RATE * FRAMES_PER_SEC + ( TDM_SIGNAL_BITS_READ_FROM_THE_END_OF_BS - STEREO_BITS_TCA + 1 + 2 * MIN_GAIN_BITS ) * FRAMES_PER_SEC; /*Q0*/
     486           0 :                 move32();
     487             :             }
     488             :             ELSE
     489             :             {
     490           0 :                 *total_brate_sec = 2 * MAX_SC_FCB_RATE * FRAMES_PER_SEC + ( TDM_SIGNAL_BITS_READ_FROM_THE_END_OF_BS + 1 + 2 * MIN_GAIN_BITS ) * FRAMES_PER_SEC; /*Q0*/
     491           0 :                 move32();
     492             :             }
     493             : 
     494           0 :             IF( tdm_lp_reuse_flag == 0 )
     495             :             {
     496           0 :                 *total_brate_sec = L_add( *total_brate_sec, MIN_SEC_LPC_RATE ); /*Q0*/
     497           0 :                 move32();
     498             :             }
     499             : 
     500           0 :             IF( tdm_Pitch_reuse_flag == 0 )
     501             :             {
     502           0 :                 *total_brate_sec = L_add( *total_brate_sec, MIN_SEC_ACB_RATE ); /*Q0*/
     503           0 :                 move32();
     504             :             }
     505             :         }
     506        3536 :         ELSE IF( GE_16( four_subfr_fcb, 40 * FRAMES_PER_SEC ) ) /* Enough bits to have minimally 2 x 12 + 2*7 bits FCB  */
     507             :         {
     508        3536 :             *tdm_low_rate_mode = 0; /* Use normal rate mode */
     509        3536 :             move16();
     510             :         }
     511             :         ELSE /* Possible slight increase of secondary channel bit budget to compensate for FCB limited flexibility */
     512             :         {
     513             :             Word16 tmp_rate, i;
     514           0 :             tmp_rate = two_subfr_fcb; /*Q0*/
     515           0 :             move16();
     516           0 :             idx = sub( NB_RATE_POSS, 2 );
     517             : 
     518           0 :             FOR( i = 0; i < NB_RATE_POSS; i++ ){
     519           0 :                 IF( LE_16( tmp_rate, fast_FCB_rates_2sfr[i] ) ){
     520           0 :                     idx = i;
     521           0 :             move16();
     522           0 :             BREAK;
     523             :         }
     524             :     }
     525           0 :     *total_brate_sec = L_add( *total_brate_sec, sub( fast_FCB_rates_2sfr[idx], tmp_rate ) ); /*Q0*/
     526           0 :     move32();
     527             : }
     528             : 
     529             : /* To prevent 13.2 kb/s for primary channel as some bitstream issues arrise with it */
     530        3536 : IF( EQ_32( L_sub( element_brate_wo_meta, *total_brate_sec ), ACELP_13k20 ) )
     531             : {
     532           0 :     *total_brate_sec = L_add( *total_brate_sec, 100 ); /*Q0*/
     533           0 :     move32();
     534             : }
     535             : }
     536             : 
     537             : /* prevent 2.4 kb/s and 2.8 kb/s as they are reserved bitrates for DTX and VBR */
     538        3630 : test();
     539        3630 : IF( EQ_32( *total_brate_sec, PPP_NELP_2k80 ) || EQ_32( *total_brate_sec, SID_2k40 ) )
     540             : {
     541           0 :     *total_brate_sec = L_sub( *total_brate_sec, 100 ); /*Q0*/
     542           0 :     move32();
     543             : }
     544             : 
     545        3630 : *total_brate_pri = L_sub( element_brate_wo_meta, *total_brate_sec ); /*Q0*/
     546        3630 : move32();
     547             : 
     548        3630 : return;
     549             : }
     550             : 
     551             : /*-------------------------------------------------------------------*
     552             :  * td_stereo_param_updt()
     553             :  *
     554             :  * copy certain TD stereo parameters from primary channel to secondary channel
     555             :  *-------------------------------------------------------------------*/
     556             : 
     557        3686 : void td_stereo_param_updt_fx(
     558             :     const Word16 lsp_old_PCh_fx[],   /* i  : primary channel old LSPs                                     Q15 */
     559             :     const Word16 lsf_old_PCh_fx[],   /* i  : primary channel old LSFs                             Qlog2(2.56) */
     560             :     const Word16 pitch_buf_PCh_fx[], /* i  : primary channel pitch buffer                                  Q6 */
     561             : #ifndef NONBE_1325_TD_STEREO_QUANT_LSF_SEC
     562             :     Word16 tdm_lspQ_PCh_fx[], /* o  : Q LSPs for primary channel                                   Q15 */
     563             : #endif
     564             :     Word16 tdm_lsfQ_PCh_fx[],         /* o  : Q LSFs for primary channel                           Qlog2(2.56) */
     565             :     Word16 tdm_Pri_pitch_buf_fx[],    /* o  : pitch values for primary channel                              Q6 */
     566             :     const Word16 flag_ACELP16k,       /* i  : ACELP@16kHz flag                                                 Q0*/
     567             :     const Word16 tdm_use_IAWB_Ave_lpc /* i  : flag to indicate the usage of mean inactive LP coefficients      Q0*/
     568             : )
     569             : {
     570             :     Word16 i;
     571             : 
     572             :     /* Copy some primary channel information into the secondary channel structure for later usage */
     573        3686 :     IF( EQ_16( tdm_use_IAWB_Ave_lpc, 1 ) )
     574             :     {
     575             :         /*not being assert*/
     576           0 :         Copy( IAWB_Ave_fx, tdm_lsfQ_PCh_fx, M ); /*Qlog2(2.56)*/
     577             : #ifndef NONBE_1325_TD_STEREO_QUANT_LSF_SEC
     578             : 
     579             : #ifdef FIX_1111_TDM_LSP_BUFFER
     580             :         IF( tdm_lspQ_PCh_fx != NULL )
     581             :         {
     582             :             lsf2lsp_fx( tdm_lsfQ_PCh_fx, tdm_lspQ_PCh_fx, M, INT_FS_12k8 );
     583             :         }
     584             : #else
     585             :         lsf2lsp_fx( tdm_lsfQ_PCh_fx, tdm_lspQ_PCh_fx, M, INT_FS_12k8 );
     586             : #endif
     587             : #endif
     588             :     }
     589        3686 :     ELSE IF( EQ_16( flag_ACELP16k, 1 ) )
     590             :     {
     591             : #ifdef FIX_1111_TDM_LSP_BUFFER
     592             : #ifndef NONBE_1325_TD_STEREO_QUANT_LSF_SEC
     593             :         IF( tdm_lspQ_PCh_fx != NULL )
     594             :         {
     595             :             Copy( lsp_old_PCh_fx, tdm_lspQ_PCh_fx, M ); /*Q15*/
     596             :             lsp_convert_poly_fx( tdm_lspQ_PCh_fx, L_FRAME, 0 );
     597             :             lsp2lsf_fx( tdm_lspQ_PCh_fx, tdm_lsfQ_PCh_fx, M, INT_FS_12k8 );
     598             :         }
     599             :         ELSE
     600             :         {
     601             : #endif
     602             :             Word16 lsp_temp[M];
     603        2015 :             Copy( lsp_old_PCh_fx, lsp_temp, M ); /*Q15*/
     604        2015 :             lsp_convert_poly_fx( lsp_temp, L_FRAME, 0 );
     605        2015 :             lsp2lsf_fx( lsp_temp, tdm_lsfQ_PCh_fx, M, INT_FS_12k8 );
     606             : #ifndef NONBE_1325_TD_STEREO_QUANT_LSF_SEC
     607             :         }
     608             : #endif
     609             : #else
     610             :         Copy( lsp_old_PCh_fx, tdm_lspQ_PCh_fx, M ); /*Q15*/
     611             :         lsp_convert_poly_fx( tdm_lspQ_PCh_fx, L_FRAME, 0 );
     612             :         lsp2lsf_fx( tdm_lspQ_PCh_fx, tdm_lsfQ_PCh_fx, M, INT_FS_12k8 );
     613             : #endif
     614             :     }
     615             :     ELSE
     616             :     {
     617             : #ifndef NONBE_1325_TD_STEREO_QUANT_LSF_SEC
     618             : #ifdef FIX_1111_TDM_LSP_BUFFER
     619             :         IF( tdm_lspQ_PCh_fx != NULL )
     620             :         {
     621             :             Copy( lsp_old_PCh_fx, tdm_lspQ_PCh_fx, M ); /*Q15*/
     622             :         }
     623             : #else
     624             :         Copy( lsp_old_PCh_fx, tdm_lspQ_PCh_fx, M ); /*Q15*/
     625             : #endif
     626             : #endif
     627        1671 :         Copy( lsf_old_PCh_fx, tdm_lsfQ_PCh_fx, M ); /*Qlog2(2.56)*/
     628             :     }
     629             : 
     630        3686 :     IF( EQ_16( flag_ACELP16k, 1 ) )
     631             :     {
     632             :         Word16 tmp16;
     633        2015 :         Word16 mult_factor = 26214 /*0.8f in Q15*/;
     634        2015 :         move16();
     635       10075 :         FOR( i = 0; i < NB_SUBFR; i++ )
     636             :         {
     637        8060 :             tmp16 = mult_r( pitch_buf_PCh_fx[i], mult_factor );          /* Convert 16kHz to 12.8 kHz pitch values Q6*/
     638        8060 :             tdm_Pri_pitch_buf_fx[i] = s_max( tmp16, shl( PIT_MIN, 6 ) ); /* pitch is represented in Q6*/
     639        8060 :             move16();
     640             :         }
     641             :     }
     642             :     ELSE
     643             :     {
     644        1671 :         Copy( pitch_buf_PCh_fx, tdm_Pri_pitch_buf_fx, NB_SUBFR ); /*Q6*/
     645             :     }
     646             : 
     647        3686 :     return;
     648             : }
     649             : 
     650             : /*-------------------------------------------------------------------*
     651             :  * tdm_SCh_LSF_intra_pred_zero_bits_fx()
     652             :  *
     653             :  *
     654             :  *-------------------------------------------------------------------*/
     655             : 
     656          66 : static void tdm_SCh_LSF_intra_pred_zero_bits_fx(
     657             :     const Word16 *tdm_lsfQ_PCh_fx, /* i  : primary channel LSFs             x2.56*/
     658             :     Word16 *pred_lsf_SCh_fx,       /* o  : predicted secondary channel LSFs x2.56*/
     659             :     const Word16 *lsf_mean_fx,     /* i  : secondary channel mean LSFs      x2.56*/
     660             :     const Word16 beta_fx           /* i  : pull to average beta factor      Q15*/
     661             : )
     662             : {
     663             :     Word16 i;
     664             :     /* pulling the LSFs closer to the average */
     665        1122 :     FOR( i = 0; i < M; i++ )
     666             :     {
     667        1056 :         pred_lsf_SCh_fx[i] = add( mult_r( beta_fx, tdm_lsfQ_PCh_fx[i] ), mult_r( sub( 32767 /*1.0f in Q15*/, beta_fx ), lsf_mean_fx[i] ) );
     668        1056 :         move16();
     669             :     }
     670             : 
     671          66 :     return;
     672             : }
     673             : 
     674             : /*-------------------------------------------------------------------*
     675             :  * tdm_SCh_LSF_intra_pred_tri_diag_mat_fx()
     676             :  *
     677             :  *
     678             :  *-------------------------------------------------------------------*/
     679             : 
     680         207 : static void tdm_SCh_LSF_intra_pred_tri_diag_mat_fx(
     681             :     Word16 *lsf_SCh_fx,            /* i/o: secondary channel LSFs               x2.56*/
     682             :     const Word16 *lsf_mean_in_fx,  /* i  : secondary channel mean LSFs (in)     x2.56*/
     683             :     const Word16 *lsf_mean_out_fx, /* i  : secondary channel mean LSFs (out)    x2.56*/
     684             :     const Word16 *prd_diag_3_fx    /* i  : secondary channel mean LSFs          Q15*/
     685             : )
     686             : {
     687             :     Word16 i;
     688             :     Word16 lsf_tmp_fx[M];
     689             :     const Word16 *prd_ptr_fx;
     690             :     Word16 *lsf_tmp_ptr1_fx;
     691             :     Word16 *lsf_tmp_ptr2_fx;
     692             :     Word16 *lsf_SCh_ptr_fx;
     693             : 
     694         207 :     prd_ptr_fx = prd_diag_3_fx; /*Q15*/
     695             : 
     696         207 :     v_sub_16( lsf_SCh_fx, lsf_mean_in_fx, lsf_tmp_fx, M );
     697             : 
     698         207 :     lsf_tmp_ptr1_fx = lsf_tmp_fx;      /*x2.56*/
     699         207 :     lsf_SCh_ptr_fx = lsf_SCh_fx;       /*x2.56*/
     700         207 :     lsf_tmp_ptr2_fx = lsf_tmp_ptr1_fx; /*x2.56*/
     701             : 
     702         207 :     *lsf_SCh_ptr_fx = mult_r( *lsf_tmp_ptr1_fx++, *prd_ptr_fx++ ); // Q2.56 + 15 -15
     703         207 :     move16();
     704         207 :     *lsf_SCh_ptr_fx = add( *lsf_SCh_ptr_fx, mult_r( *lsf_tmp_ptr1_fx, *prd_ptr_fx++ ) ); // Q2.56 + 15 -15
     705         207 :     move16();
     706         207 :     lsf_SCh_ptr_fx++;
     707             : 
     708        3105 :     FOR( i = 1; i < M - 1; i++ )
     709             :     {
     710        2898 :         lsf_tmp_ptr1_fx = lsf_tmp_ptr2_fx;                             // Q2.56
     711        2898 :         *lsf_SCh_ptr_fx = mult_r( *lsf_tmp_ptr1_fx++, *prd_ptr_fx++ ); // Q2.56 + 15 -15
     712        2898 :         move16();
     713        2898 :         lsf_tmp_ptr2_fx = lsf_tmp_ptr1_fx;                                                             /*x2.56*/
     714        2898 :         *lsf_SCh_ptr_fx = add( *lsf_SCh_ptr_fx, mult_r( ( *lsf_tmp_ptr1_fx++ ), ( *prd_ptr_fx++ ) ) ); /*x2.56*/
     715        2898 :         move16();
     716        2898 :         ( *lsf_SCh_ptr_fx ) = add( *lsf_SCh_ptr_fx, mult_r( ( *lsf_tmp_ptr1_fx ), ( *prd_ptr_fx++ ) ) ); /*x2.56*/
     717        2898 :         move16();
     718        2898 :         lsf_SCh_ptr_fx++;
     719             :     }
     720             : 
     721         207 :     lsf_tmp_ptr1_fx = lsf_tmp_ptr2_fx;                                 /*x2.56*/
     722         207 :     *lsf_SCh_ptr_fx = mult_r( ( *lsf_tmp_ptr1_fx ), ( *prd_ptr_fx ) ); /*x2.56*/
     723         207 :     move16();
     724         207 :     lsf_tmp_ptr1_fx++;
     725         207 :     prd_ptr_fx++;
     726         207 :     *lsf_SCh_ptr_fx = add( mult_r( ( *lsf_tmp_ptr1_fx ), ( *prd_ptr_fx ) ), *lsf_SCh_ptr_fx ); /*x2.56*/
     727         207 :     move16();
     728             : 
     729         207 :     v_add_16( lsf_SCh_fx, lsf_mean_out_fx, lsf_SCh_fx, M ); /*x2.56*/
     730             : 
     731         207 :     return;
     732             : }
     733             : 
     734             : /*-------------------------------------------------------------------*
     735             :  * tdm_SCh_LSF_intra_pred_fx()
     736             :  *
     737             :  *
     738             :  *-------------------------------------------------------------------*/
     739             : 
     740          66 : void tdm_SCh_LSF_intra_pred_fx(
     741             :     const Word32 element_brate,    /* i  : element bitrate                  Q0*/
     742             :     const Word16 *tdm_lsfQ_PCh_fx, /* i  : primary channel LSFs             x2.56*/
     743             :     Word16 *pred_lsf_SCh_fx        /* o  : predicted secondary channel LSFs x2.56*/
     744             : )
     745             : {
     746             :     Word16 fixed_beta;
     747             : 
     748          66 :     IF( LE_32( element_brate, IVAS_13k2 ) )
     749             :     {
     750           0 :         fixed_beta = 28508; // 0.87f in Q15
     751           0 :         move16();
     752             :     }
     753          66 :     ELSE IF( LE_32( element_brate, IVAS_16k4 ) )
     754             :     {
     755          15 :         fixed_beta = 30801; // 0.94f in Q15
     756          15 :         move16();
     757             :     }
     758          51 :     ELSE IF( element_brate <= IVAS_24k4 )
     759             :     {
     760          14 :         fixed_beta = 29818; // 0.91f in Q15
     761          14 :         move16();
     762             :     }
     763          37 :     ELSE IF( LE_32( element_brate, IVAS_32k ) )
     764             :     {
     765          37 :         fixed_beta = 30146; // 0.92f in Q15
     766          37 :         move16();
     767             :     }
     768             :     ELSE
     769             :     {
     770           0 :         fixed_beta = 29818; // 0.91f in Q15
     771           0 :         move16();
     772             :     }
     773             : 
     774          66 :     tdm_SCh_LSF_intra_pred_zero_bits_fx( tdm_lsfQ_PCh_fx, pred_lsf_SCh_fx, tdm_LSF_MEAN_PRED_QNT_fx, fixed_beta );
     775             : 
     776          66 :     tdm_SCh_LSF_intra_pred_tri_diag_mat_fx( pred_lsf_SCh_fx, tdm_LSF_MEAN_PRED_QNT_IN_fx, tdm_LSF_MEAN_PRED_QNT_OUT_fx, tdm_PRED_QNT_fixed_beta_prd_diag_3_fx );
     777             : 
     778          66 :     return;
     779             : }
     780             : 
     781             : /*-------------------------------------------------------------------*
     782             :  * tdm_SCh_LSF_intra_pred_one_bit_dec_fx()
     783             :  *
     784             :  *
     785             :  *-------------------------------------------------------------------*/
     786             : 
     787         141 : static void tdm_SCh_LSF_intra_pred_one_bit_dec_fx(
     788             :     const Word16 *tdm_lsfQ_PCh_fx, /* i  : primary channel LSFs                              x2.56*/
     789             :     Word16 *pred_lsf_SCh_fx,       /* o  : predicted secondary channel LSFs                  x2.56*/
     790             :     const Word16 *lsf_mean_fx,     /* i  : secondary channel mean LSFs                       x2.56*/
     791             :     const Word16 *Beta_Q_x_fx,     /* i  : beta quantization values                          Q15*/
     792             :     const Word16 beta_index_fx     /* i  : the quantization bits for beta (-1 if beta fixed) Q0*/
     793             : )
     794             : {
     795             :     Word16 i;
     796             :     Word16 beta_fx;
     797             : 
     798         141 :     beta_fx = Beta_Q_x_fx[beta_index_fx]; // Q15
     799         141 :     move16();
     800             : 
     801             :     /* pulling the LSFs closer to the avergae */
     802        2397 :     FOR( i = 0; i < M; i++ )
     803             :     {
     804        2256 :         pred_lsf_SCh_fx[i] = add( mult_r( beta_fx, tdm_lsfQ_PCh_fx[i] ), mult_r( sub( 32767 /*1.0f in Q15*/, beta_fx ), lsf_mean_fx[i] ) ); /*x2.56*/
     805        2256 :         move16();
     806             :     }
     807             : 
     808         141 :     return;
     809             : }
     810             : 
     811             : /*-------------------------------------------------------------------*
     812             :  * tdm_SCh_LSF_intra_pred_one_bit_enc_fx()
     813             :  *
     814             :  *
     815             :  *-------------------------------------------------------------------*/
     816             : 
     817           0 : static void tdm_SCh_LSF_intra_pred_one_bit_enc_fx(
     818             :     const Word16 *lsf_SCh_fx,      /* i  : secondary channel LSFs                              x2.56*/
     819             :     const Word16 *tdm_lsfQ_PCh_fx, /* i  : primary channel LSFs                                x2.56*/
     820             :     Word16 *pred_lsf_SCh_fx,       /* o  : predicted secondary channel LSFs                    x2.56*/
     821             :     const Word16 *lsf_mean_fx,     /* i  : secondary channel mean LSFs                         x2.56*/
     822             :     const Word16 *lsf_wgts_new_fx, /* i  : Improved wgts for LSFs                              Q8*/
     823             :     const Word16 *Beta_Q_x_fx,     /* i  : beta quantization values                            Q15*/
     824             :     Word16 *beta_index             /* o  : the quantization bits for beta (-1 if beta fixed)   Q0*/
     825             : )
     826             : {
     827             :     Word16 i;
     828             : 
     829             :     Word16 A_temp_fx[M];
     830             :     Word16 B_temp_fx[M];
     831           0 :     Word32 WD_fx[2] = { 0 };
     832           0 :     move32();
     833           0 :     move32();
     834             : 
     835           0 :     FOR( i = 0; i < M; i++ )
     836             :     {
     837           0 :         A_temp_fx[i] = sub( lsf_SCh_fx[i], lsf_mean_fx[i] ); /*x2.56*/
     838           0 :         move16();
     839           0 :         B_temp_fx[i] = sub( lsf_mean_fx[i], tdm_lsfQ_PCh_fx[i] ); /*x2.56*/
     840           0 :         move16();
     841             :     }
     842             : 
     843           0 :     FOR( i = 0; i < M; i++ )
     844             :     {
     845           0 :         Word32 wgt_A_temp = Mpy_32_16_1( L_mult( A_temp_fx[i], A_temp_fx[i] ), lsf_wgts_new_fx[i] );  /*2*(x2.56)-7*/
     846           0 :         Word32 wgt_B_temp = Mpy_32_16_1( L_mult( B_temp_fx[i], B_temp_fx[i] ), lsf_wgts_new_fx[i] );  /*2*(x2.56)-7*/
     847           0 :         Word32 wgt_AB_temp = Mpy_32_16_1( L_mult( A_temp_fx[i], B_temp_fx[i] ), lsf_wgts_new_fx[i] ); /*2*(x2.56)-7*/
     848           0 :         Word32 Beta_Q_x0 = L_mult( Beta_Q_x_fx[0], Beta_Q_x_fx[0] );                                  // Q31
     849           0 :         Word32 Beta_Q_x1 = L_mult( Beta_Q_x_fx[1], Beta_Q_x_fx[1] );                                  // Q31
     850             : 
     851           0 :         WD_fx[0] = L_add( WD_fx[0], L_add( L_add( wgt_A_temp, Mpy_32_16_1( L_shl( wgt_AB_temp, 1 ), Beta_Q_x_fx[0] ) ), Mpy_32_32( wgt_B_temp, Beta_Q_x0 ) ) ); /*2*(x2.56)-7*/
     852           0 :         move32();
     853           0 :         WD_fx[1] = L_add( WD_fx[1], L_add( L_add( wgt_A_temp, Mpy_32_16_1( L_shl( wgt_AB_temp, 1 ), Beta_Q_x_fx[1] ) ), Mpy_32_32( wgt_B_temp, Beta_Q_x1 ) ) ); /*2*(x2.56)-7*/
     854           0 :         move32();
     855             :     }
     856             : 
     857           0 :     IF( LT_32( WD_fx[0], WD_fx[1] ) )
     858             :     {
     859           0 :         *beta_index = 0;
     860           0 :         move16();
     861             :     }
     862             :     ELSE
     863             :     {
     864           0 :         *beta_index = 1;
     865           0 :         move16();
     866             :     }
     867             : 
     868           0 :     tdm_SCh_LSF_intra_pred_one_bit_dec_fx( tdm_lsfQ_PCh_fx, pred_lsf_SCh_fx, tdm_LSF_MEAN_RE_USE_fx, Beta_Q_x_fx, *beta_index );
     869             : 
     870           0 :     return;
     871             : }
     872             : 
     873             : /*-------------------------------------------------------------------*
     874             :  * tdm_SCh_lsf_reuse_fx()
     875             :  *
     876             :  *
     877             :  *-------------------------------------------------------------------*/
     878             : 
     879         141 : void tdm_SCh_lsf_reuse_fx(
     880             :     const Word16 enc_dec,            /* i  : encoder/decoder flag                Q0*/
     881             :     const Word32 element_brate,      /* i  : element bitrate                     Q0*/
     882             :     Word16 lsf_new_fx[M],            /* i/o: LSFs at the end of the frame        x2.56*/
     883             :     Word16 lsp_new_fx[M],            /* i/o: LSPs at the end of the frame        Q15*/
     884             :     const Word16 tdm_lsfQ_PCh_fx[M], /* i  : primary channel LSFs                x2.56*/
     885             :     const Word16 lsf_wgts_fx[M],     /* i  : LSF weights                         Q8*/
     886             :     Word16 *beta_index               /* i/o: quantization index                  Q0*/
     887             : )
     888             : {
     889             :     const Word16 *Beta_Q1bit_re_use_fx;
     890             : 
     891         141 :     IF( LE_32( element_brate, IVAS_13k2 ) )
     892             :     {
     893           0 :         Beta_Q1bit_re_use_fx = tdm_Beta_Q1bit_re_use_13k2_fx; /*Q15*/
     894             :     }
     895         141 :     ELSE IF( LE_32( element_brate, IVAS_16k4 ) )
     896             :     {
     897          12 :         Beta_Q1bit_re_use_fx = tdm_Beta_Q1bit_re_use_16k4_fx; /*Q15*/
     898             :     }
     899         129 :     ELSE IF( LE_32( element_brate, IVAS_32k ) )
     900             :     {
     901         129 :         Beta_Q1bit_re_use_fx = tdm_Beta_Q1bit_re_use_24k4_32k_fx; /*Q15*/
     902             :     }
     903             :     ELSE
     904             :     {
     905           0 :         Beta_Q1bit_re_use_fx = tdm_Beta_Q1bit_re_use_48k_fx; /*Q15*/
     906             :     }
     907             : 
     908         141 :     IF( enc_dec == ENC )
     909             :     {
     910           0 :         tdm_SCh_LSF_intra_pred_one_bit_enc_fx( lsf_new_fx, tdm_lsfQ_PCh_fx, lsf_new_fx, tdm_LSF_MEAN_RE_USE_fx, lsf_wgts_fx, Beta_Q1bit_re_use_fx, beta_index );
     911             :     }
     912             :     ELSE /* DEC */
     913             :     {
     914         141 :         tdm_SCh_LSF_intra_pred_one_bit_dec_fx( tdm_lsfQ_PCh_fx, lsf_new_fx, tdm_LSF_MEAN_RE_USE_fx, Beta_Q1bit_re_use_fx, *beta_index );
     915             :     }
     916             : 
     917         141 :     tdm_SCh_LSF_intra_pred_tri_diag_mat_fx( lsf_new_fx, tdm_LSF_MEAN_RE_USE_IN_fx, tdm_LSF_MEAN_RE_USE_OUT_fx, tdm_RE_USE_adaptive_beta_prd_diag_3_fx );
     918             : 
     919         141 :     lsf2lsp_fx( lsf_new_fx, lsp_new_fx, M, INT_FS_12k8 );
     920             : 
     921         141 :     return;
     922             : }

Generated by: LCOV version 1.14