LCOV - code coverage report
Current view: top level - lib_com - ivas_stereo_td_bit_alloc.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ 8834b716eb6d7dfb881d5c69dd21cb18e1692722 Lines: 184 217 84.8 %
Date: 2025-07-09 08:36:12 Functions: 8 8 100.0 %

          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             : #ifdef DEBUGGING
      36             : #include "debug.h"
      37             : #endif
      38             : #include "cnst.h"
      39             : #include "stat_enc.h"
      40             : #include "rom_com.h"
      41             : #include "ivas_prot.h"
      42             : #include "ivas_rom_com.h"
      43             : #include "ivas_cnst.h"
      44             : #include "prot.h"
      45             : #include "wmc_auto.h"
      46             : 
      47             : 
      48             : /*-------------------------------------------------------------------*
      49             :  * Local constants
      50             :  *-------------------------------------------------------------------*/
      51             : 
      52             : #define NB_RATE_POSS     10                              /* Nmbr of possible FCB bitrate for half rate mode*/
      53             : #define MIN_SEC_ACB_RATE ( 2 * 8 * FRAMES_PER_SEC )      /* 2 subfr ACB bitrate */
      54             : #define MIN_SEC_LPC_RATE ( ( 24 + 2 ) * FRAMES_PER_SEC ) /* LPC min rate */
      55             : 
      56             : #define MAX_SC_FCB_RATE     ( 24 + 1 ) /* Maximum bit for a FCB subfr */
      57             : #define MIN_4SUBFR_FCB_RATE 40         /* Minimum 4 subfr bitrate (4x10 bits) */
      58             : #define MIN_GAIN_BITS       6          /* Minimum number of bits used */
      59             : 
      60             : #define TDM_UC_NORMAL_MODE_MBRATE      9000                        /* Max bitrate for normal UC if LP coded */
      61             : #define TDM_UC_NORMAL_MODE_MBRATE_LP_R 8200                        /* Normal bitrate for normal UC if LP is reused */
      62             : #define TDM_UC_NORMAL_MODE_MINBR_LP_R  7000                        /* If bitrate is below 7k, add back MID_LP_BRATE */
      63             : #define MID_LP_BRATE                   ( 31 + 5 ) * FRAMES_PER_SEC /* average bitrate for LP */
      64             : #define MAX_TDM_UC_BRATE               11000                       /* Maximum bitrate for tdm normal UC mode */
      65             : 
      66             : #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    */
      67             : #define MIN_FCB_SECRATE2 ( 2 * 16 * FRAMES_PER_SEC )                                  /* min. 2 time 14 bits FCB, used only when LPC and/or pitch is transmitted    */
      68             : 
      69             : 
      70             : /*-------------------------------------------------------------------*
      71             :  * tdm_bit_alloc()
      72             :  *
      73             :  * Bitbudget distribution between Primary and Secondary channel in TD stereo
      74             :  *-------------------------------------------------------------------*/
      75             : 
      76       14747 : void tdm_bit_alloc(
      77             :     const int16_t ivas_format,           /* i  : IVAS format                         */
      78             :     const int16_t ism_mode,              /* i  : ISM mode in combined format         */
      79             :     const int32_t element_brate_wo_meta, /* i  : element bitrate without metadata    */
      80             :     const int16_t tdm_lp_reuse_flag,     /* i  : LPC reusage flag                    */
      81             :     int32_t *total_brate_pri,            /* o  : Allocated primary channel bitrate   */
      82             :     int32_t *total_brate_sec,            /* o  : Allocated secondary channel bitrate */
      83             :     int16_t *tdm_low_rate_mode,          /* o  : secondary channel low rate mode flag*/
      84             :     const int16_t coder_type,            /* i  : secondary channel coder type        */
      85             :     const int16_t ener_ratio_idx,        /* i  : correlation ratio indexe            */
      86             :     const int16_t tdm_Pitch_reuse_flag,  /* i  : primary channel pitch reuse flag    */
      87             :     const int16_t bwidth_pri,            /* i  : bandwidth of the primary channel    */
      88             :     const int16_t bwidth_sec,            /* i  : bandwidth of the secondary channel  */
      89             :     const int16_t flag_ACELP16k_pri,     /* i  : ACELP@16kHz core flag, primary chan.*/
      90             :     const int16_t tdm_LRTD_flag,         /* i  : LRTD stereo mode flag               */
      91             :     const int16_t coder_type0,           /* i  : coder type (temporary in the encoder, from bitstream in decoder) */
      92             :     const int16_t tdm_inst_ratio_idx_ref /* i  : instantaneous correlation ratio idx */
      93             : )
      94             : {
      95             :     int16_t idx, four_subfr_fcb, two_subfr_fcb;
      96             :     float bit_rate_diff;
      97             :     int16_t BWE_brate, tmp_bits;
      98       14747 :     int16_t tdm_inst_ratio_idx = tdm_inst_ratio_idx_ref;
      99       14747 :     if ( tdm_inst_ratio_idx == TDM_NQ )
     100             :     {
     101         400 :         tdm_inst_ratio_idx = LRTD_STEREO_MID_IS_PRIM; /* Bit rate almost split half and half*/
     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       14747 :     *tdm_low_rate_mode = 1;
     108             : 
     109             :     /* Allocating different bitrate to channels */
     110       14747 :     idx = 0;
     111       14747 :     if ( element_brate_wo_meta <= IVAS_13k2 )
     112             :     {
     113           0 :         idx = 0;
     114             :     }
     115       14747 :     else if ( element_brate_wo_meta <= IVAS_16k4 )
     116             :     {
     117        2203 :         idx = 1;
     118             :     }
     119       12544 :     else if ( element_brate_wo_meta <= IVAS_24k4 )
     120             :     {
     121        4487 :         idx = 2;
     122             :     }
     123        8057 :     else if ( element_brate_wo_meta <= IVAS_32k )
     124             :     {
     125        8057 :         idx = 3;
     126             :     }
     127           0 :     else if ( element_brate_wo_meta <= IVAS_48k )
     128             :     {
     129           0 :         idx = 4;
     130             :     }
     131             : 
     132       14747 :     if ( coder_type == UNVOICED && tdm_bit_allc_tbl[idx][coder_type] >= 4200 )
     133             :     {
     134           0 :         *tdm_low_rate_mode = 0;
     135             :     }
     136             : 
     137             :     /* Secondary channel based bitrate allocation */
     138       14747 :     *total_brate_sec = tdm_bit_allc_tbl[idx][coder_type];
     139             : 
     140             :     /* secondary channel bitrate allocation based on the energy scaling ratio */
     141       14747 :     if ( ( ( ivas_format != MASA_ISM_FORMAT || ism_mode == ISM_MODE_NONE ) && ( ( coder_type != UNVOICED ) || tdm_LRTD_flag == 1 ) ) || ( ivas_format == MASA_ISM_FORMAT && ism_mode != ISM_MODE_NONE && coder_type > UNVOICED ) )
     142             :     {
     143       14683 :         bit_rate_diff = (float) ( element_brate_wo_meta - 2 * *total_brate_sec );
     144             : 
     145       14683 :         if ( tdm_LRTD_flag == 1 ) /* >  element_brate > STEREO_22k or CT0 not used */
     146             :         {
     147             :             /* further adjustment in function of the energy/correlation ratio */
     148       14267 :             if ( coder_type == INACTIVE )
     149             :             {
     150           0 :                 *total_brate_sec = max( *total_brate_sec, (int16_t) ( 0.3f * ( element_brate_wo_meta - 500 ) / 100 ) * 100 );
     151           0 :                 tmp_bits = (int16_t) ( -abs( tdm_inst_ratio_idx - 16 ) * 200 * idx );
     152             :             }
     153             :             else
     154             :             {
     155       14267 :                 *total_brate_sec = max( *total_brate_sec, (int16_t) ( 0.5f * ( element_brate_wo_meta - 500 ) / 100 ) * 100 );
     156             :                 /* tmp_bits = -abs(tdm_inst_ratio_idx-16)*200*idx; */
     157       14267 :                 tmp_bits = (int16_t) ( -abs( tdm_inst_ratio_idx - 16 ) * 100 * idx );
     158             :             }
     159             : 
     160             :             /* tmp_bits should be subtract from the secondary channel bitrate */
     161             :             /* If the primary channel doesn't correspond to the channel having the highest correlation to the mono- inverse the bitrate compensation */
     162       14267 :             if ( ( ener_ratio_idx >= LRTD_STEREO_MID_IS_PRIM && tdm_inst_ratio_idx < LRTD_STEREO_MID_IS_PRIM ) || ( ener_ratio_idx < LRTD_STEREO_MID_IS_PRIM && tdm_inst_ratio_idx >= LRTD_STEREO_MID_IS_PRIM ) )
     163             :             {
     164        6303 :                 tmp_bits *= -1;
     165             :             }
     166       14267 :             bit_rate_diff = tmp_bits;
     167             :         }
     168             :         else
     169             :         {
     170         416 :             if ( ener_ratio_idx < LRTD_STEREO_MID_IS_PRIM )
     171             :             {
     172         256 :                 bit_rate_diff = ( LRTD_STEREO_MID_IS_PRIM - ener_ratio_idx ) * bit_rate_diff * 0.05f;
     173             :             }
     174             :             else
     175             :             {
     176         160 :                 bit_rate_diff = ( ener_ratio_idx - LRTD_STEREO_MID_IS_PRIM ) * bit_rate_diff * 0.05f;
     177             :             }
     178             :         }
     179             :         /*bit_rate_diff2 = ((int16_t)(10.f*(-0.5f*ener_ratio_LR+0.5f)*bit_rate_diff)/100)*100;*/
     180       14683 :         *total_brate_sec += ( (int16_t) ( bit_rate_diff / 100 ) * 100 );
     181       14683 :         *total_brate_sec = max( *total_brate_sec, tdm_bit_allc_tbl[idx][coder_type] );
     182             : 
     183       14683 :         if ( coder_type == INACTIVE && tdm_LRTD_flag == 0 )
     184             :         {
     185           0 :             *total_brate_sec = min( *total_brate_sec, MIN_BRATE_SWB_BWE );
     186             :         }
     187             : 
     188       14683 :         if ( ( ener_ratio_idx <= 1 || ener_ratio_idx >= 29 ) && coder_type >= UNVOICED )
     189       14451 :         {
     190       14451 :             int16_t delta_brate = 0;
     191             : 
     192       14451 :             if ( bwidth_pri > WB )
     193             :             {
     194       11348 :                 delta_brate = 600; /* To slightly compensate for SWB BWE instead of WB BWE */
     195       11348 :                 if ( element_brate_wo_meta <= IVAS_16k4 )
     196             :                 {
     197        1748 :                     delta_brate = 1250; /* To compensate for SWB BWE instead of WB BWE */
     198             :                 }
     199             :             }
     200             : 
     201       14451 :             if ( element_brate_wo_meta <= IVAS_13k2 )
     202             :             {
     203           0 :                 *total_brate_sec = max( *total_brate_sec, 5600 + delta_brate ); /* ~42-47 % of the total bitrate */
     204             :             }
     205       14451 :             else if ( element_brate_wo_meta <= IVAS_16k4 )
     206             :             {
     207        2152 :                 *total_brate_sec = max( *total_brate_sec, 6500 + delta_brate ); /* ~40-43 % of the total bitrate */
     208             :             }
     209       12299 :             else if ( element_brate_wo_meta <= IVAS_24k4 )
     210             :             {
     211        4399 :                 *total_brate_sec = max( *total_brate_sec, 9000 + delta_brate ); /* ~37-39 % of the total bitrate */
     212             :             }
     213             :             else
     214             :             {
     215        7900 :                 *total_brate_sec = max( *total_brate_sec, 9600 + delta_brate ); /* ~30-32% of the total bitrate */
     216             :             }
     217             :         }
     218             :         else
     219             :         {
     220         232 :             *total_brate_sec = min( *total_brate_sec, (int16_t) ( 0.0045f * element_brate_wo_meta ) * 100 );
     221             :         }
     222             : 
     223       14683 :         *total_brate_sec = min( *total_brate_sec, 18000 );
     224             :     }
     225          64 :     else if ( coder_type == UNVOICED )
     226             :     {
     227          64 :         if ( tdm_lp_reuse_flag == 0 )
     228             :         {
     229          32 :             *total_brate_sec += ( 31 + 5 ) * FRAMES_PER_SEC;
     230             :         }
     231             :     }
     232             : 
     233       14747 :     if ( coder_type <= UNVOICED )
     234             :     {
     235         380 :         *total_brate_sec = min( *total_brate_sec, MAX_TDM_UC_BRATE );
     236             : 
     237         380 :         if ( *total_brate_sec >= TDM_UC_NORMAL_MODE_MBRATE && tdm_lp_reuse_flag == 0 )
     238             :         {
     239         272 :             *tdm_low_rate_mode = 0;
     240             :         }
     241         108 :         else if ( *total_brate_sec >= TDM_UC_NORMAL_MODE_MBRATE_LP_R )
     242             :         {
     243           0 :             *tdm_low_rate_mode = 0;
     244             :         }
     245         108 :         else if ( ( tdm_lp_reuse_flag == 0 && *total_brate_sec < TDM_UC_NORMAL_MODE_MINBR_LP_R && coder_type == UNVOICED ) || ( tdm_lp_reuse_flag == 0 && *total_brate_sec < ( tdm_bit_allc_tbl[idx][0] + MID_LP_BRATE ) ) )
     246             :         {
     247          32 :             *total_brate_sec += MID_LP_BRATE;
     248             :         }
     249             :     }
     250             : 
     251             :     /* verify that primary channel bitrate is higher than the minimum supported bitrate */
     252       14747 :     if ( flag_ACELP16k_pri )
     253             :     {
     254        8145 :         BWE_brate = SWB_TBE_1k75;
     255        8145 :         if ( element_brate_wo_meta < IVAS_24k4 )
     256             :         {
     257           0 :             BWE_brate = SWB_TBE_1k10;
     258             :         }
     259             : 
     260        8145 :         if ( bwidth_pri > WB && tdm_LRTD_flag == 0 )
     261             :         {
     262         349 :             BWE_brate += ( STEREO_BITS_ICBWE + STEREO_ICBWE_MSFLAG_BITS ) * FRAMES_PER_SEC;
     263             :         }
     264        8145 :         if ( bwidth_pri > SWB && tdm_LRTD_flag == 1 )
     265             :         {
     266        2854 :             BWE_brate += 300;
     267             :         }
     268             : 
     269        8145 :         if ( bwidth_pri == FB )
     270             :         {
     271        2899 :             BWE_brate += ( FB_TBE_1k8 - SWB_TBE_1k75 );
     272             :         }
     273             : 
     274        8145 :         if ( element_brate_wo_meta - *total_brate_sec - BWE_brate < 14000 )
     275             :         {
     276        3668 :             *total_brate_sec = element_brate_wo_meta - 14000 - BWE_brate;
     277             :         }
     278             :     }
     279             :     else
     280             :     {
     281        6602 :         BWE_brate = SWB_TBE_1k75;
     282        6602 :         if ( bwidth_pri == WB )
     283             :         {
     284        1664 :             BWE_brate = WB_BWE_0k35;
     285        1664 :             if ( tdm_LRTD_flag == 0 )
     286             :             {
     287          12 :                 BWE_brate += 250; /* ICA Brate */
     288             :             }
     289             :         }
     290        4938 :         else if ( tdm_LRTD_flag == 0 )
     291             :         {
     292          39 :             BWE_brate += 350; /* ICA Brate */
     293             :         }
     294             :     }
     295             : 
     296       14747 :     if ( coder_type0 == TRANSITION )
     297             :     {
     298          92 :         if ( element_brate_wo_meta > IVAS_13k2 )
     299             :         {
     300          92 :             *total_brate_sec = min( *total_brate_sec, element_brate_wo_meta - ( ACELP_8k00 + BWE_brate ) );
     301             :         }
     302             :         else
     303             :         {
     304           0 :             *total_brate_sec = min( *total_brate_sec, element_brate_wo_meta - ( ACELP_7k20 + BWE_brate ) );
     305             :         }
     306             :     }
     307             :     else
     308             :     {
     309       14655 :         *total_brate_sec = min( *total_brate_sec, element_brate_wo_meta - ( 5900 + BWE_brate ) );
     310             :     }
     311             : 
     312       14747 :     if ( coder_type == INACTIVE )
     313             :     {
     314           0 :         *total_brate_sec = max( *total_brate_sec, tdm_bit_allc_tbl[0][0] ); /* sanity check to ensure the secondary channel always gets the minimal bitrate it needs */
     315             :     }
     316             :     else
     317             :     {
     318       14747 :         *total_brate_sec = max( *total_brate_sec, 3500 ); /* sanity check to ensure the secondary channel always gets the minimal bitrate it needs */
     319             :     }
     320             : 
     321             :     /* Secondary channel bitrate adjusment                                                      */
     322             :     /* First, adjust the bitrate depending of what is transmitted                               */
     323             :     /* Second, choose the number of subframe for ACELP core depending of the targetted bitratre */
     324             :     /* Finally, verify that the concordance between the number of subframe, the parameters sent and the bitrate available */
     325       14747 :     if ( coder_type == GENERIC /* || coder_type == AUDIO*/ )
     326             :     {
     327             :         /* Adjust the bitrate depending of what is transmitted */
     328             :         /* If LPC are transmitted, ensure enough bits are used */
     329       14367 :         if ( tdm_lp_reuse_flag == 0 )
     330             :         {
     331             :             /* Pitch is transmitted as well, further increase the bitrate */
     332       13831 :             if ( tdm_Pitch_reuse_flag == 0 )
     333             :             {
     334       13699 :                 *total_brate_sec = max( *total_brate_sec, MIN_FCB_SECRATE2 + MIN_SEC_ACB_RATE + MIN_SEC_LPC_RATE + MIN_SIGN_RATE );
     335             : 
     336       13699 :                 if ( tdm_LRTD_flag == 1 && bwidth_sec == SWB )
     337             :                 {
     338             :                     /* ensure that there are enough bits to code SWB TBE_1k10 as well */
     339        7702 :                     *total_brate_sec = max( *total_brate_sec, MIN_FCB_SECRATE2 + MIN_SEC_ACB_RATE + MIN_SEC_LPC_RATE + MIN_SIGN_RATE + SWB_TBE_1k10 );
     340             :                 }
     341             :             }
     342             :             else /* only LPC is tranmitted -> if ( *total_brate_sec  < MIN_SEC_BRATE+MIN_SEC_LPC_RATE ) */
     343             :             {
     344         132 :                 *total_brate_sec = max( *total_brate_sec, MIN_FCB_SECRATE2 + MIN_SEC_LPC_RATE + MIN_SIGN_RATE );
     345             :             }
     346             :         }
     347         536 :         else if ( /*tdm_lp_reuse_flag == 1*/ tdm_Pitch_reuse_flag == 0 )
     348             :         {
     349         536 :             *total_brate_sec = max( *total_brate_sec, MIN_FCB_SECRATE2 + MIN_SEC_ACB_RATE + MIN_SIGN_RATE );
     350             :         }
     351             : 
     352             :         /* Choose between 2 and 4 subfr, depending of the bitrate available and prevent the gap between the 2 atlernative */
     353       14367 :         if ( tdm_LRTD_flag == 1 )
     354             :         {
     355       13951 :             four_subfr_fcb = (int16_t) ( *total_brate_sec - ( TDM_SIGNAL_BITS_READ_FROM_THE_END_OF_BS - STEREO_BITS_TCA + 1 + 4 * MIN_GAIN_BITS ) * FRAMES_PER_SEC );
     356       13951 :             two_subfr_fcb = (int16_t) ( *total_brate_sec - ( TDM_SIGNAL_BITS_READ_FROM_THE_END_OF_BS - STEREO_BITS_TCA + 1 + 2 * MIN_GAIN_BITS ) * FRAMES_PER_SEC );
     357             :         }
     358             :         else
     359             :         {
     360         416 :             four_subfr_fcb = (int16_t) ( *total_brate_sec - ( TDM_SIGNAL_BITS_READ_FROM_THE_END_OF_BS + 1 + 4 * MIN_GAIN_BITS ) * FRAMES_PER_SEC );
     361         416 :             two_subfr_fcb = (int16_t) ( *total_brate_sec - ( TDM_SIGNAL_BITS_READ_FROM_THE_END_OF_BS + 1 + 2 * MIN_GAIN_BITS ) * FRAMES_PER_SEC );
     362             :         }
     363             : 
     364       14367 :         if ( tdm_lp_reuse_flag == 0 )
     365             :         {
     366       13831 :             four_subfr_fcb -= MIN_SEC_LPC_RATE;
     367       13831 :             two_subfr_fcb -= MIN_SEC_LPC_RATE;
     368             :         }
     369             : 
     370       14367 :         if ( tdm_Pitch_reuse_flag == 0 )
     371             :         {
     372       14235 :             four_subfr_fcb -= ( MIN_SEC_ACB_RATE + 10 * FRAMES_PER_SEC );
     373       14235 :             two_subfr_fcb -= MIN_SEC_ACB_RATE;
     374             :         }
     375             : 
     376             :         /* Too much bits for the 2 subfr model but not enough for the the 4 subfr model -> slightly reduce the 2nd channel bitrate */
     377       14367 :         if ( two_subfr_fcb > 2 * MAX_SC_FCB_RATE * FRAMES_PER_SEC && four_subfr_fcb < MIN_4SUBFR_FCB_RATE * FRAMES_PER_SEC )
     378             :         {
     379           0 :             if ( tdm_LRTD_flag == 1 )
     380             :             {
     381           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;
     382             :             }
     383             :             else
     384             :             {
     385           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;
     386             :             }
     387             : 
     388           0 :             if ( tdm_lp_reuse_flag == 0 )
     389             :             {
     390           0 :                 *total_brate_sec += MIN_SEC_LPC_RATE;
     391             :             }
     392             : 
     393           0 :             if ( tdm_Pitch_reuse_flag == 0 )
     394             :             {
     395           0 :                 *total_brate_sec += MIN_SEC_ACB_RATE;
     396             :             }
     397             :         }
     398       14367 :         else if ( four_subfr_fcb >= ( 40 ) * FRAMES_PER_SEC ) /* Enough bits to have minimally 2 x 12 + 2*7 bits FCB  */
     399             :         {
     400       14367 :             *tdm_low_rate_mode = 0; /* Use normal rate mode */
     401             :         }
     402             :         else /* Possible slight increase of secondary channel bit budget to compensate for FCB limited flexibility */
     403             :         {
     404             :             int16_t tmp_rate, i;
     405           0 :             tmp_rate = two_subfr_fcb;
     406           0 :             idx = NB_RATE_POSS - 2;
     407             : 
     408           0 :             for ( i = 0; i < NB_RATE_POSS; i++ )
     409             :             {
     410           0 :                 if ( tmp_rate <= fast_FCB_rates_2sfr[i] )
     411             :                 {
     412           0 :                     idx = i;
     413           0 :                     break;
     414             :                 }
     415             :             }
     416           0 :             *total_brate_sec += ( fast_FCB_rates_2sfr[idx] - tmp_rate );
     417             :         }
     418             :         /* To prevent 13.2 kb/s for primary channel as some bitstream issues arrise with it */
     419       14367 :         if ( element_brate_wo_meta - *total_brate_sec == ACELP_13k20 )
     420             :         {
     421           0 :             *total_brate_sec += 100;
     422             :         }
     423             :     }
     424             :     /* prevent 2.4 kb/s and 2.8 kb/s as they are reserved bitrates for DTX and VBR */
     425       14747 :     if ( *total_brate_sec == PPP_NELP_2k80 || *total_brate_sec == SID_2k40 )
     426             :     {
     427           0 :         *total_brate_sec -= 100;
     428             :     }
     429             : 
     430       14747 :     *total_brate_pri = element_brate_wo_meta - *total_brate_sec;
     431             : 
     432       14747 :     return;
     433             : }
     434             : 
     435             : 
     436             : /*-------------------------------------------------------------------*
     437             :  * td_stereo_param_updt()
     438             :  *
     439             :  * copy certain TD stereo parameters from primary channel to secondary channel
     440             :  *-------------------------------------------------------------------*/
     441             : 
     442       15131 : void td_stereo_param_updt(
     443             :     const float lsp_old_PCh[],         /* i  : primary channel old LSPs            */
     444             :     const float lsf_old_PCh[],         /* i  : primary channel old LSFs            */
     445             :     const float pitch_buf_PCh[],       /* i  : primary channel pitch buffer        */
     446             :     float tdm_lsfQ_PCh[],              /* o  : Q LSFs for primary channel          */
     447             :     float tdm_Pri_pitch_buf[],         /* o  : pitch values for primary channel    */
     448             :     const int16_t flag_ACELP16k,       /* i  : ACELP@16kHz flag                    */
     449             :     const int16_t tdm_use_IAWB_Ave_lpc /* i  : flag to indicate the usage of mean inactive LP coefficients */
     450             : )
     451             : {
     452             :     int16_t i;
     453             : 
     454             :     /* Copy some primary channel information into the secondary channel structure for later usage */
     455       15131 :     if ( tdm_use_IAWB_Ave_lpc == 1 )
     456             :     {
     457           0 :         mvr2r( IAWB_Ave, tdm_lsfQ_PCh, M );
     458             :     }
     459       15131 :     else if ( flag_ACELP16k == 1 )
     460             :     {
     461             :         float lsp_temp[M];
     462        8403 :         mvr2r( lsp_old_PCh, lsp_temp, M );
     463        8403 :         lsp_convert_poly( lsp_temp, L_FRAME, 0 );
     464        8403 :         lsp2lsf( lsp_temp, tdm_lsfQ_PCh, M, INT_FS_12k8 );
     465             :     }
     466             :     else
     467             :     {
     468        6728 :         mvr2r( lsf_old_PCh, tdm_lsfQ_PCh, M );
     469             :     }
     470             : 
     471             :     /* This is only to keep the buffer up-to-date */
     472       15131 :     if ( flag_ACELP16k == 1 )
     473             :     {
     474       42015 :         for ( i = 0; i < NB_SUBFR; i++ )
     475             :         {
     476       33612 :             tdm_Pri_pitch_buf[i] = pitch_buf_PCh[i] * 0.8f;
     477       33612 :             tdm_Pri_pitch_buf[i] = max( tdm_Pri_pitch_buf[i], PIT_MIN );
     478             :         }
     479             :     }
     480             :     else
     481             :     {
     482        6728 :         mvr2r( pitch_buf_PCh, tdm_Pri_pitch_buf, NB_SUBFR );
     483             :     }
     484             : 
     485       15131 :     return;
     486             : }
     487             : 
     488             : 
     489             : /*-------------------------------------------------------------------*
     490             :  * tdm_SCh_LSF_intra_pred_zero_bits()
     491             :  *
     492             :  *
     493             :  *-------------------------------------------------------------------*/
     494             : 
     495         416 : static void tdm_SCh_LSF_intra_pred_zero_bits(
     496             :     const float *tdm_lsfQ_PCh, /* i  : primary channel LSFs             */
     497             :     float *pred_lsf_SCh,       /* o  : predicted secondary channel LSFs */
     498             :     const float *lsf_mean,     /* i  : secondary channel mean LSFs      */
     499             :     const float beta           /* i  : pull to average beta factor      */
     500             : )
     501             : {
     502             :     int16_t i;
     503             : 
     504             :     /* pulling the LSFs closer to the average */
     505        7072 :     for ( i = 0; i < M; i++ )
     506             :     {
     507        6656 :         pred_lsf_SCh[i] = beta * tdm_lsfQ_PCh[i] + ( 1.0f - beta ) * lsf_mean[i];
     508             :     }
     509             : 
     510         416 :     return;
     511             : }
     512             : 
     513             : 
     514             : /*-------------------------------------------------------------------*
     515             :  * tdm_SCh_LSF_intra_pred_tri_diag_mat()
     516             :  *
     517             :  *
     518             :  *-------------------------------------------------------------------*/
     519             : 
     520         984 : static void tdm_SCh_LSF_intra_pred_tri_diag_mat(
     521             :     float *lsf_SCh,            /* i/o: secondary channel LSFs               */
     522             :     const float *lsf_mean_in,  /* i  : secondary channel mean LSFs (in)     */
     523             :     const float *lsf_mean_out, /* i  : secondary channel mean LSFs (out)    */
     524             :     const float *prd_diag_3    /* i  : secondary channel mean LSFs          */
     525             : )
     526             : {
     527             :     int16_t i;
     528             :     float lsf_tmp[M];
     529             :     const float *prd_ptr;
     530             :     float *lsf_tmp_ptr1;
     531             :     float *lsf_tmp_ptr2;
     532             :     float *lsf_SCh_ptr;
     533             : 
     534         984 :     prd_ptr = prd_diag_3;
     535             : 
     536         984 :     v_sub( lsf_SCh, lsf_mean_in, lsf_tmp, M );
     537             : 
     538         984 :     lsf_tmp_ptr1 = lsf_tmp;
     539         984 :     lsf_SCh_ptr = lsf_SCh;
     540             : 
     541         984 :     lsf_tmp_ptr2 = lsf_tmp_ptr1;
     542         984 :     *lsf_SCh_ptr = ( *lsf_tmp_ptr1++ ) * ( *prd_ptr++ );
     543         984 :     ( *lsf_SCh_ptr++ ) += ( *lsf_tmp_ptr1 ) * ( *prd_ptr++ );
     544             : 
     545       14760 :     for ( i = 1; i < M - 1; i++ )
     546             :     {
     547       13776 :         lsf_tmp_ptr1 = lsf_tmp_ptr2;
     548       13776 :         *lsf_SCh_ptr = ( *lsf_tmp_ptr1++ ) * ( *prd_ptr++ );
     549       13776 :         lsf_tmp_ptr2 = lsf_tmp_ptr1;
     550       13776 :         *lsf_SCh_ptr += ( *lsf_tmp_ptr1++ ) * ( *prd_ptr++ );
     551       13776 :         ( *lsf_SCh_ptr++ ) += ( *lsf_tmp_ptr1 ) * ( *prd_ptr++ );
     552             :     }
     553             : 
     554         984 :     lsf_tmp_ptr1 = lsf_tmp_ptr2;
     555         984 :     *lsf_SCh_ptr = ( *lsf_tmp_ptr1++ ) * ( *prd_ptr++ );
     556         984 :     *lsf_SCh_ptr += ( *lsf_tmp_ptr1 ) * ( *prd_ptr );
     557             : 
     558         984 :     v_add( lsf_SCh, lsf_mean_out, lsf_SCh, M );
     559             : 
     560         984 :     return;
     561             : }
     562             : 
     563             : 
     564             : /*-------------------------------------------------------------------*
     565             :  * tdm_SCh_LSF_intra_pred()
     566             :  *
     567             :  *
     568             :  *-------------------------------------------------------------------*/
     569             : 
     570         416 : void tdm_SCh_LSF_intra_pred(
     571             :     const int32_t element_brate, /* i  : element bitrate                  */
     572             :     const float *tdm_lsfQ_PCh,   /* i  : primary channel LSFs             */
     573             :     float *pred_lsf_SCh          /* o  : predicted secondary channel LSFs */
     574             : )
     575             : {
     576             :     float fixed_beta;
     577             : 
     578         416 :     if ( element_brate <= IVAS_13k2 )
     579             :     {
     580           0 :         fixed_beta = 0.87F;
     581             :     }
     582         416 :     else if ( element_brate <= IVAS_16k4 )
     583             :     {
     584          51 :         fixed_beta = 0.94F;
     585             :     }
     586         365 :     else if ( element_brate <= IVAS_24k4 )
     587             :     {
     588          56 :         fixed_beta = 0.91F;
     589             :     }
     590         309 :     else if ( element_brate <= IVAS_32k )
     591             :     {
     592         309 :         fixed_beta = 0.92F;
     593             :     }
     594             :     else
     595             :     {
     596           0 :         fixed_beta = 0.91F;
     597             :     }
     598             : 
     599         416 :     tdm_SCh_LSF_intra_pred_zero_bits( tdm_lsfQ_PCh, pred_lsf_SCh, tdm_LSF_MEAN_PRED_QNT, fixed_beta );
     600             : 
     601         416 :     tdm_SCh_LSF_intra_pred_tri_diag_mat( pred_lsf_SCh, tdm_LSF_MEAN_PRED_QNT_IN, tdm_LSF_MEAN_PRED_QNT_OUT, tdm_PRED_QNT_fixed_beta_prd_diag_3 );
     602             : 
     603         416 :     return;
     604             : }
     605             : 
     606             : 
     607             : /*-------------------------------------------------------------------*
     608             :  * tdm_SCh_LSF_intra_pred_one_bit_dec()
     609             :  *
     610             :  *
     611             :  *-------------------------------------------------------------------*/
     612             : 
     613         568 : static void tdm_SCh_LSF_intra_pred_one_bit_dec(
     614             :     const float *tdm_lsfQ_PCh, /* i  : primary channel LSFs                             */
     615             :     float *pred_lsf_SCh,       /* o  : predicted secondary channel LSFs                 */
     616             :     const float *lsf_mean,     /* i  : secondary channel mean LSFs                      */
     617             :     const float *Beta_Q_x,     /* i  : beta quantization values                         */
     618             :     const int16_t beta_index   /* i  : the quantization bits for beta (-1 if beta fixed)*/
     619             : )
     620             : {
     621             :     int16_t i;
     622             :     float beta;
     623             : 
     624         568 :     beta = Beta_Q_x[beta_index];
     625             : 
     626             :     /* pulling the LSFs closer to the avergae */
     627        9656 :     for ( i = 0; i < M; i++ )
     628             :     {
     629        9088 :         pred_lsf_SCh[i] = beta * tdm_lsfQ_PCh[i] + ( 1.0f - beta ) * lsf_mean[i];
     630             :     }
     631             : 
     632         568 :     return;
     633             : }
     634             : 
     635             : 
     636             : /*-------------------------------------------------------------------*
     637             :  * tdm_SCh_LSF_intra_pred_one_bit_enc()
     638             :  *
     639             :  *
     640             :  *-------------------------------------------------------------------*/
     641             : 
     642         142 : static void tdm_SCh_LSF_intra_pred_one_bit_enc(
     643             :     const float *lsf_SCh,      /* i  : secondary channel LSFs                              */
     644             :     const float *tdm_lsfQ_PCh, /* i  : primary channel LSFs                                */
     645             :     float *pred_lsf_SCh,       /* o  : predicted secondary channel LSFs                    */
     646             :     const float *lsf_mean,     /* i  : secondary channel mean LSFs                         */
     647             :     const float *lsf_wgts_new, /* i  : Improved wgts for LSFs                              */
     648             :     const float *Beta_Q_x,     /* i  : beta quantization values                            */
     649             :     int16_t *beta_index        /* o  : the quantization bits for beta (-1 if beta fixed)   */
     650             : )
     651             : {
     652             :     int16_t i;
     653             :     float A_temp[M];
     654             :     float B_temp[M];
     655             :     float WD[2];
     656             : 
     657        2414 :     for ( i = 0; i < M; i++ )
     658             :     {
     659        2272 :         A_temp[i] = lsf_SCh[i] - lsf_mean[i];
     660        2272 :         B_temp[i] = lsf_mean[i] - tdm_lsfQ_PCh[i];
     661             :     }
     662             : 
     663         142 :     WD[0] = 0.f;
     664         142 :     WD[1] = 0.f;
     665        2414 :     for ( i = 0; i < M; i++ )
     666             :     {
     667        2272 :         WD[0] += lsf_wgts_new[i] * SQR( A_temp[i] ) + 2.0f * Beta_Q_x[0] * lsf_wgts_new[i] * A_temp[i] * B_temp[i] + lsf_wgts_new[i] * SQR( Beta_Q_x[0] ) * SQR( B_temp[i] );
     668        2272 :         WD[1] += lsf_wgts_new[i] * SQR( A_temp[i] ) + 2.0f * Beta_Q_x[1] * lsf_wgts_new[i] * A_temp[i] * B_temp[i] + lsf_wgts_new[i] * SQR( Beta_Q_x[1] ) * SQR( B_temp[i] );
     669             :     }
     670             : 
     671         142 :     if ( WD[0] < WD[1] )
     672             :     {
     673         126 :         *beta_index = 0;
     674             :     }
     675             :     else
     676             :     {
     677          16 :         *beta_index = 1;
     678             :     }
     679             : 
     680         142 :     tdm_SCh_LSF_intra_pred_one_bit_dec( tdm_lsfQ_PCh, pred_lsf_SCh, tdm_LSF_MEAN_RE_USE, Beta_Q_x, *beta_index );
     681             : 
     682         142 :     return;
     683             : }
     684             : 
     685             : 
     686             : /*-------------------------------------------------------------------*
     687             :  * tdm_SCh_lsf_reuse()
     688             :  *
     689             :  *
     690             :  *-------------------------------------------------------------------*/
     691             : 
     692         568 : void tdm_SCh_lsf_reuse(
     693             :     const int16_t enc_dec,       /* i  : encoder/decoder flag                */
     694             :     const int32_t element_brate, /* i  : element bitrate                     */
     695             :     float lsf_new[M],            /* i/o: LSFs at the end of the frame        */
     696             :     float lsp_new[M],            /* i/o: LSPs at the end of the frame        */
     697             :     const float tdm_lsfQ_PCh[M], /* i  : primary channel LSFs                */
     698             :     const float lsf_wgts[M],     /* i  : LSF weights                         */
     699             :     int16_t *beta_index          /* i/o: quantization index                  */
     700             : )
     701             : {
     702             :     const float *Beta_Q1bit_re_use;
     703             : 
     704         568 :     if ( element_brate <= IVAS_13k2 )
     705             :     {
     706           0 :         Beta_Q1bit_re_use = tdm_Beta_Q1bit_re_use_13k2;
     707             :     }
     708         568 :     else if ( element_brate <= IVAS_16k4 )
     709             :     {
     710          48 :         Beta_Q1bit_re_use = tdm_Beta_Q1bit_re_use_16k4;
     711             :     }
     712         520 :     else if ( element_brate <= IVAS_32k )
     713             :     {
     714         520 :         Beta_Q1bit_re_use = tdm_Beta_Q1bit_re_use_24k4_32k;
     715             :     }
     716             :     else
     717             :     {
     718           0 :         Beta_Q1bit_re_use = tdm_Beta_Q1bit_re_use_48k;
     719             :     }
     720             : 
     721         568 :     if ( enc_dec == ENC )
     722             :     {
     723         142 :         tdm_SCh_LSF_intra_pred_one_bit_enc( lsf_new, tdm_lsfQ_PCh, lsf_new, tdm_LSF_MEAN_RE_USE, lsf_wgts, Beta_Q1bit_re_use, beta_index );
     724             :     }
     725             :     else /* DEC */
     726             :     {
     727         426 :         tdm_SCh_LSF_intra_pred_one_bit_dec( tdm_lsfQ_PCh, lsf_new, tdm_LSF_MEAN_RE_USE, Beta_Q1bit_re_use, *beta_index );
     728             :     }
     729             : 
     730         568 :     tdm_SCh_LSF_intra_pred_tri_diag_mat( lsf_new, tdm_LSF_MEAN_RE_USE_IN, tdm_LSF_MEAN_RE_USE_OUT, tdm_RE_USE_adaptive_beta_prd_diag_3 );
     731             : 
     732         568 :     lsf2lsp( lsf_new, lsp_new, M, INT_FS_12k8 );
     733             : 
     734         568 :     return;
     735             : }

Generated by: LCOV version 1.14