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 "prot.h" 43 : #include "rom_com.h" 44 : #include "rom_enc.h" 45 : #include "wmc_auto.h" 46 : 47 : /*-----------------------------------------------------------------* 48 : * Funtion core_coder_reconfig * 49 : * ~~~~~~~~~~~~~~~~~~~ * 50 : * - reconfig core coder when switching to another frame type * 51 : *-----------------------------------------------------------------*/ 52 : 53 41416 : void core_coder_reconfig( 54 : Encoder_State *st, 55 : const int32_t last_total_brate ) 56 : { 57 41416 : TCX_ENC_HANDLE hTcxEnc = st->hTcxEnc; 58 : int16_t bwidth, i, index; 59 : 60 : /*Configuration of ACELP*/ 61 41416 : BITS_ALLOC_init_config_acelp( st->total_brate, st->narrowBand, st->nb_subfr, &( st->acelp_cfg ) ); 62 : 63 : /*Configuration of partial copy*/ 64 41416 : if ( st->Opt_RF_ON ) 65 : { 66 0 : st->hRF->acelp_cfg_rf.mode_index = 1; 67 0 : st->hRF->acelp_cfg_rf.midLpc = 0; 68 0 : st->hRF->acelp_cfg_rf.midLpc_enable = 0; 69 0 : st->hRF->acelp_cfg_rf.pre_emphasis = 0; 70 0 : st->hRF->acelp_cfg_rf.formant_enh = 1; 71 0 : st->hRF->acelp_cfg_rf.formant_tilt = 1; 72 0 : st->hRF->acelp_cfg_rf.voice_tilt = 1; 73 0 : st->hRF->acelp_cfg_rf.formant_enh_num = FORMANT_SHARPENING_G1; 74 0 : st->hRF->acelp_cfg_rf.formant_enh_den = FORMANT_SHARPENING_G2; 75 : } 76 : 77 41416 : if ( st->element_mode == IVAS_CPE_MDCT ) 78 : { 79 19062 : st->nb_bits_header_tcx = 2; /* signal class */ 80 : } 81 22354 : else if ( st->tcxonly ) 82 : { 83 1915 : st->nb_bits_header_tcx = 1 + 1; /*TCX20/TCX10 + last_core*/ 84 1915 : st->nb_bits_header_tcx += 2; /* Siganl class*/ 85 : } 86 : else 87 : { 88 20439 : st->nb_bits_header_ace = 1 + 2 + 1; /*TCX/ACELP+coder_type + last_core*/ 89 20439 : st->nb_bits_header_tcx = st->nb_bits_header_ace; 90 : 91 20439 : if ( st->hTcxCfg != NULL ) 92 : { 93 20340 : if ( st->hTcxCfg->lfacNext <= 0 ) 94 : { 95 20340 : st->nb_bits_header_ace--; /*No last_core*/ 96 : } 97 : } 98 : } 99 : 100 : /*Switch off TCX or ACELP?*/ 101 41416 : if ( st->sr_core == INT_FS_12k8 ) 102 : { 103 6096 : st->acelpEnabled = ( st->restrictedMode & 1 ) == 1; 104 6096 : st->tcx20Enabled = ( st->restrictedMode & 2 ) == 2; 105 : } 106 41416 : st->prevEnergyHF = st->currEnergyHF = 65535.0f; /* prevent block switch */ 107 : 108 : /* TCX-LTP */ 109 41416 : if ( st->hTcxEnc != NULL ) 110 : { 111 41317 : st->hTcxEnc->tcxltp = getTcxLtp( st->sr_core ); 112 : } 113 : 114 : /*Use for 12.8 kHz sampling rate and low bitrates, the conventional pulse search->better SNR*/ 115 41416 : st->acelp_autocorr = 1; 116 41416 : if ( st->total_brate <= ACELP_9k60 && st->sr_core == INT_FS_12k8 ) 117 : { 118 1892 : st->acelp_autocorr = 0; 119 : } 120 : 121 : /*Get audio bandwidth info*/ 122 41416 : if ( st->narrowBand ) 123 : { 124 0 : bwidth = NB; 125 : } 126 41416 : else if ( st->sr_core <= INT_FS_16k ) 127 : { 128 22062 : bwidth = WB; 129 : } 130 : else 131 : { 132 19354 : bwidth = SWB; 133 : } 134 : 135 : /*Scale TCX for non-active frames to adjust loudness with ACELP*/ 136 41416 : if ( st->hTcxCfg != NULL ) 137 : { 138 41317 : st->hTcxCfg->na_scale = 1.f; 139 41317 : if ( bwidth < SWB && !( st->tcxonly ) ) 140 : { 141 231714 : for ( i = 0; i < SIZE_SCALE_TABLE_TCX; i++ ) 142 : { 143 229517 : if ( ( bwidth == scaleTcxTable[i].bwmode ) && 144 104525 : ( st->total_brate >= scaleTcxTable[i].bitrateFrom ) && 145 104525 : ( st->total_brate < scaleTcxTable[i].bitrateTo ) ) 146 : { 147 18635 : if ( st->rf_mode ) 148 : { 149 0 : i--; 150 : } 151 18635 : st->hTcxCfg->na_scale = scaleTcxTable[i].scale; 152 18635 : break; 153 : } 154 : } 155 : } 156 : } 157 : 158 41416 : if ( st->element_mode > IVAS_SCE ) 159 : { 160 21794 : st->enableTcxLpc = ( st->lpcQuantization == 1 ) && ( st->total_brate <= LOWRATE_TCXLPC_MAX_BR_CPE || st->rf_mode ); 161 : } 162 : else 163 : { 164 19622 : st->enableTcxLpc = ( st->lpcQuantization == 1 ) && ( st->total_brate <= LOWRATE_TCXLPC_MAX_BR || st->rf_mode ); 165 : } 166 : 167 41416 : if ( st->ini_frame == 0 || st->last_codec_mode == MODE1 ) 168 : { 169 41416 : st->envWeighted = 0; 170 : } 171 : 172 41416 : if ( st->bwidth == SWB && ( st->total_brate == ACELP_16k40 || st->total_brate == ACELP_24k40 ) && st->element_mode == EVS_MONO ) 173 : { 174 75 : if ( st->tec_tfa == 0 ) 175 : { 176 0 : set_zero( st->hTECEnc->loBuffer, MAX_TEC_SMOOTHING_DEG ); 177 : } 178 75 : st->tec_tfa = 1; 179 : } 180 : else 181 : { 182 41341 : st->tec_tfa = 0; 183 : } 184 : 185 41416 : st->enablePlcWaveadjust = 0; 186 41416 : if ( st->total_brate >= HQ_48k ) 187 : { 188 18257 : st->enablePlcWaveadjust = 1; 189 : } 190 : 191 41416 : st->glr = 0; 192 41416 : if ( ( st->total_brate == ACELP_9k60 || st->total_brate == ACELP_16k40 || st->total_brate == ACELP_24k40 ) && st->element_mode == EVS_MONO ) 193 : { 194 75 : st->glr = 1; 195 : } 196 : 197 41416 : if ( st->glr ) 198 : { 199 75 : st->nb_bits_header_ace += G_LPC_RECOVERY_BITS; 200 : } 201 : 202 41416 : if ( hTcxEnc != NULL ) 203 : { 204 41317 : if ( st->bwidth == NB || st->bwidth == WB ) 205 : { 206 2517 : index = min( N_TCX_STARTLINE_NOISE_WB - 1, (uint16_t) max( 0, ( st->rf_mode == 0 ) ? st->frame_size_index : st->frame_size_index - 1 ) ); 207 2517 : hTcxEnc->nmStartLine = startLineWB[index]; 208 : } 209 : else /* (st->bwidth == SWB || st->bwidth == FB) */ 210 : { 211 38800 : index = min( N_TCX_STARTLINE_NOISE_SWB - 1, (uint16_t) max( 3, ( st->rf_mode == 0 ) ? st->frame_size_index : st->frame_size_index - 1 ) - 3 ); 212 38800 : if ( st->total_brate >= IVAS_96k && st->total_brate <= IVAS_192k && st->element_mode > IVAS_SCE ) 213 : { 214 6064 : index--; 215 : } 216 38800 : hTcxEnc->nmStartLine = startLineSWB[index]; 217 38800 : if ( st->total_brate == IVAS_48k && st->element_mode == IVAS_CPE_MDCT && hTcxEnc->nmStartLine * 5 < st->L_frame * 4 ) 218 : { 219 1264 : hTcxEnc->nmStartLine = ( hTcxEnc->nmStartLine * 5 ) >> 2; /* low-rate stereo is more efficient than dual-mono due to stereo processing */ 220 : } 221 : } 222 : } 223 : 224 41416 : if ( hTcxEnc != NULL ) 225 : { 226 41317 : if ( st->total_brate < ACELP_24k40 && ( ( st->total_brate > last_total_brate ) || ( st->last_codec_mode == MODE1 ) ) ) 227 : { 228 : /* low-freq memQuantZeros must be reset partially if bitrate increased */ 229 11490 : set_s( hTcxEnc->memQuantZeros, 0, hTcxEnc->nmStartLine ); 230 : } 231 : else 232 : { 233 29827 : if ( st->total_brate >= ACELP_24k40 && st->total_brate <= ACELP_32k && last_total_brate >= ACELP_13k20 && last_total_brate < ACELP_24k40 ) 234 : { 235 94 : set_s( hTcxEnc->memQuantZeros, 0, st->L_frame ); 236 : } 237 : } 238 : } 239 : 240 41416 : return; 241 : }