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 : /*==================================================================================== 34 : EVS Codec 3GPP TS26.443 Nov 04, 2021. Version 12.14.0 / 13.10.0 / 14.6.0 / 15.4.0 / 16.3.0 35 : ====================================================================================*/ 36 : 37 : #include <stdint.h> 38 : #include "options.h" 39 : #ifdef DEBUGGING 40 : #include "debug.h" 41 : #endif 42 : #include "cnst.h" 43 : #include "prot.h" 44 : #include "rom_com.h" 45 : #include "wmc_auto.h" 46 : 47 : /*------------------------------------------------------------------* 48 : * Local constants 49 : *------------------------------------------------------------------*/ 50 : 51 : #define RATEWIN 600 /* length of the rate control window. This is 600 active speech frames. This equals roughly 12s of active speech */ 52 : 53 : /*------------------------------------------------------------------* 54 : * update_average_rate() 55 : * 56 : * SC-VBR update average data rate 57 : *------------------------------------------------------------------*/ 58 : 59 1403 : void update_average_rate( 60 : SC_VBR_ENC_HANDLE hSC_VBR, /* i/o: SC-VBR state structure */ 61 : const int32_t core_brate /* i : core bitrate */ 62 : ) 63 : { 64 : float avratetarg; /* target rate for next RATEWIN active frames */ 65 : float target; /* target set by VBR_ADR_MAX_TARGET*RATEWIN*10 */ 66 : 67 1403 : if ( hSC_VBR->numactive == RATEWIN ) /* goes into rate control only the numactive ==RATEWIN. So rate control is triggered after each RATEWIN avtive frames */ 68 : { 69 : /* after 1000 blocks of RATEWIN frames, we change the way we control the average rate by using 70 : st->global_avr_rate=0.99*st->global_avr_rate+0.01*st->sum_of_rates. This will avoid 71 : veriables growing indefinitely while providing a good long term average rate */ 72 : 73 2 : if ( hSC_VBR->global_frame_cnt < 1000 ) 74 : { 75 2 : hSC_VBR->global_frame_cnt++; 76 2 : hSC_VBR->global_avr_rate = ( hSC_VBR->global_avr_rate * ( hSC_VBR->global_frame_cnt - 1 ) + hSC_VBR->sum_of_rates ) / hSC_VBR->global_frame_cnt; 77 : } 78 : else 79 : { 80 0 : hSC_VBR->global_avr_rate = 0.01f * hSC_VBR->sum_of_rates + 0.99f * hSC_VBR->global_avr_rate; 81 : } 82 : 83 2 : if ( hSC_VBR->sum_of_rates == 0 ) 84 : { 85 0 : hSC_VBR->sum_of_rates = (float) ( RATEWIN * VBR_ADR_MAX_TARGET * 10 ); 86 : } 87 : 88 2 : target = VBR_ADR_MAX_TARGET * 10 * RATEWIN; 89 : 90 2 : if ( target < hSC_VBR->global_avr_rate ) /* Action is taken to reduce the averge rate. Only initiated if the global rate > target rate */ 91 : { 92 : /* Check the VAD SNR values to table the noisey/not noisey decision */ 93 : 94 2 : if ( hSC_VBR->SNR_THLD < 67 ) /* Currently in QFF mode. The bumpup thresholds are slightly relaxed for noisy speech. */ 95 : { 96 : /* Increase the threshold so the the bumpup procedure is done using the noisy thresholds. 97 : Use 3.5 steps to quickly ramp up the rate control to reduce the settling time */ 98 0 : hSC_VBR->SNR_THLD += 3.5f; 99 : } 100 2 : else if ( hSC_VBR->mode_QQF == 0 && hSC_VBR->sum_of_rates > target ) /* Now SNR_THLD is in the max allowed. Sill the global average is higher and 101 : last RATEWIN frames have a higher agerage than the target rate. Now slightly 102 : more aggresive rate control is used by changing the mode to QQF. Still the 103 : same strict bumpups (more bumpups,higher rate) are used. */ 104 : { 105 : /* Kick in QQF mode */ 106 0 : hSC_VBR->mode_QQF = 1; 107 : } 108 2 : else if ( hSC_VBR->sum_of_rates > target ) /* Actions (1) and (2) are not sufficient to control the rate. Still the last RATEWIN active 109 : frames have a higher average rate than the target rate. More aggresive rate control is 110 : needed. At this point the rate_control flag is set. This will enable the more relaxed 111 : bump up thresholds (less bump ups->reduced rate)*/ 112 : { 113 : /* Relaxed bump ups are used */ 114 2 : hSC_VBR->rate_control = 1; 115 : 116 : /* This will be triggered only if the gloabl average rate is considerablly higher than the target rate. 117 : Keep a higher threshold to avoid short term rate increases over the target rate. */ 118 2 : if ( hSC_VBR->global_avr_rate > ( target + 420.0f ) ) /* Last resort rate control. This is a safer rate control mechanism by increasing NELPS */ 119 : { 120 2 : hSC_VBR->Last_Resort = 1; /* compute based on a larger window as the last resort */ 121 : } 122 : else 123 : { 124 0 : hSC_VBR->Last_Resort = 0; 125 : } 126 : } 127 0 : else if ( hSC_VBR->sum_of_rates < target ) /* If the average rate of last RATEWIN frames is controlled by above actions, disable the most 128 : aggresive rate control mechanisms. Still keep QQF mode as the global rate is not under 129 : the target rate*/ 130 : { 131 0 : hSC_VBR->Last_Resort = 0; 132 0 : hSC_VBR->mode_QQF = 1; 133 0 : hSC_VBR->rate_control = 0; 134 : } 135 : } 136 : else 137 : { 138 : /* floding back to lesser and leser aggresive rate control mechanisms gradually if global rate is under control */ 139 0 : hSC_VBR->Last_Resort = 0; 140 : 141 0 : if ( hSC_VBR->rate_control == 1 ) 142 : { 143 0 : hSC_VBR->rate_control = 0; 144 : } 145 0 : else if ( hSC_VBR->mode_QQF == 1 ) /* now rate control is not active and still the global rate is below the target. so go to QFF mode */ 146 : { 147 0 : hSC_VBR->mode_QQF = 0; 148 : } 149 : else 150 : { 151 0 : if ( hSC_VBR->SNR_THLD >= 60 ) 152 : { 153 0 : hSC_VBR->SNR_THLD -= 1.5f; 154 : } 155 : else 156 : { 157 0 : hSC_VBR->SNR_THLD = 60.0f; 158 : } 159 : } 160 : } 161 : 162 2 : if ( hSC_VBR->global_avr_rate < target - 120 ) /* In QFF mode and global rate is less than target rate-0.2kbps. We can send some Q frames 163 : to F frames to improve the quality */ 164 : { 165 : /* kick in bouncing back from Q to F */ 166 0 : hSC_VBR->Q_to_F = 1; 167 : 168 : /* average rate for next 600ms = global_rate * 2 - rate of the past RATEWIN active frames */ 169 0 : avratetarg = (float) ( ( RATEWIN * 10 ) * 2 * VBR_ADR_MAX_TARGET - hSC_VBR->global_avr_rate ); 170 : 171 : /* compute the percentage of frames that needed to be sent to F. st->pattern_m is computed as % val * 1000. eg. if % is 10%, then 172 : st->pattern_m=100 . Later this value is used in voiced.enc to bump up 10% of PPP frames to F frames. */ 173 0 : hSC_VBR->pattern_m = (int16_t) ( 1000 * ( avratetarg - 6.15f * RATEWIN * 10 ) / ( 10 * RATEWIN * 0.1f ) ); 174 : 175 0 : if ( hSC_VBR->pattern_m < 0 ) 176 : { 177 0 : hSC_VBR->pattern_m = 0; /* no bump up will ever happen */ 178 : } 179 : 180 0 : if ( hSC_VBR->pattern_m > 1000 ) 181 : { 182 0 : hSC_VBR->pattern_m = 1000; /* 10% of bump ups */ 183 : } 184 : 185 0 : hSC_VBR->patterncount = 0; 186 : } 187 : else 188 : { 189 2 : hSC_VBR->Q_to_F = 0; 190 : } 191 : 192 2 : hSC_VBR->sum_of_rates = 0; 193 2 : hSC_VBR->numactive = 0; 194 : } 195 : 196 1403 : hSC_VBR->numactive++; 197 : 198 : /* sum the total number of bits (in kbytes) * 10 here */ 199 1403 : hSC_VBR->sum_of_rates += ( core_brate / 1000.0f ) * 10; 200 : 201 1403 : return; 202 : }