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 563508 : void core_coder_reconfig( 54 : Encoder_State *st, 55 : const int32_t last_total_brate ) 56 : { 57 563508 : TCX_ENC_HANDLE hTcxEnc = st->hTcxEnc; 58 : int16_t bwidth, i, index; 59 : 60 : /*Configuration of ACELP*/ 61 563508 : BITS_ALLOC_init_config_acelp( st->total_brate, st->narrowBand, st->nb_subfr, &( st->acelp_cfg ) ); 62 : 63 : /*Configuration of partial copy*/ 64 563508 : if ( st->Opt_RF_ON ) 65 : { 66 33 : st->hRF->acelp_cfg_rf.mode_index = 1; 67 33 : st->hRF->acelp_cfg_rf.midLpc = 0; 68 33 : st->hRF->acelp_cfg_rf.midLpc_enable = 0; 69 33 : st->hRF->acelp_cfg_rf.pre_emphasis = 0; 70 33 : st->hRF->acelp_cfg_rf.formant_enh = 1; 71 33 : st->hRF->acelp_cfg_rf.formant_tilt = 1; 72 33 : st->hRF->acelp_cfg_rf.voice_tilt = 1; 73 33 : st->hRF->acelp_cfg_rf.formant_enh_num = FORMANT_SHARPENING_G1; 74 33 : st->hRF->acelp_cfg_rf.formant_enh_den = FORMANT_SHARPENING_G2; 75 : } 76 : 77 563508 : if ( st->element_mode == IVAS_CPE_MDCT ) 78 : { 79 285496 : st->nb_bits_header_tcx = 2; /* signal class */ 80 : } 81 278012 : else if ( st->tcxonly ) 82 : { 83 36740 : st->nb_bits_header_tcx = 1 + 1; /*TCX20/TCX10 + last_core*/ 84 36740 : st->nb_bits_header_tcx += 2; /* Siganl class*/ 85 : } 86 : else 87 : { 88 241272 : st->nb_bits_header_ace = 1 + 2 + 1; /*TCX/ACELP+coder_type + last_core*/ 89 241272 : st->nb_bits_header_tcx = st->nb_bits_header_ace; 90 : 91 241272 : if ( st->hTcxCfg != NULL ) 92 : { 93 240859 : if ( st->hTcxCfg->lfacNext <= 0 ) 94 : { 95 240859 : st->nb_bits_header_ace--; /*No last_core*/ 96 : } 97 : } 98 : } 99 : 100 : /*Switch off TCX or ACELP?*/ 101 563508 : if ( st->sr_core == INT_FS_12k8 ) 102 : { 103 61126 : st->acelpEnabled = ( st->restrictedMode & 1 ) == 1; 104 61126 : st->tcx20Enabled = ( st->restrictedMode & 2 ) == 2; 105 : } 106 563508 : st->prevEnergyHF = st->currEnergyHF = 65535.0f; /* prevent block switch */ 107 : 108 : /* TCX-LTP */ 109 563508 : if ( st->hTcxEnc != NULL ) 110 : { 111 563095 : 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 563508 : st->acelp_autocorr = 1; 116 563508 : if ( st->total_brate <= ACELP_9k60 && st->sr_core == INT_FS_12k8 ) 117 : { 118 17879 : st->acelp_autocorr = 0; 119 : } 120 : 121 : /*Get audio bandwidth info*/ 122 563508 : if ( st->narrowBand ) 123 : { 124 66 : bwidth = NB; 125 : } 126 563442 : else if ( st->sr_core <= INT_FS_16k ) 127 : { 128 263071 : bwidth = WB; 129 : } 130 : else 131 : { 132 300371 : bwidth = SWB; 133 : } 134 : 135 : /*Scale TCX for non-active frames to adjust loudness with ACELP*/ 136 563508 : if ( st->hTcxCfg != NULL ) 137 : { 138 563095 : st->hTcxCfg->na_scale = 1.f; 139 563095 : if ( bwidth < SWB && !( st->tcxonly ) ) 140 : { 141 2732921 : for ( i = 0; i < SIZE_SCALE_TABLE_TCX; i++ ) 142 : { 143 2724915 : if ( ( bwidth == scaleTcxTable[i].bwmode ) && 144 1247301 : ( st->total_brate >= scaleTcxTable[i].bitrateFrom ) && 145 1247301 : ( st->total_brate < scaleTcxTable[i].bitrateTo ) ) 146 : { 147 238329 : if ( st->rf_mode ) 148 : { 149 33 : i--; 150 : } 151 238329 : st->hTcxCfg->na_scale = scaleTcxTable[i].scale; 152 238329 : break; 153 : } 154 : } 155 : } 156 : } 157 : 158 563508 : if ( st->element_mode > IVAS_SCE ) 159 : { 160 329499 : st->enableTcxLpc = ( st->lpcQuantization == 1 ) && ( st->total_brate <= LOWRATE_TCXLPC_MAX_BR_CPE || st->rf_mode ); 161 : } 162 : else 163 : { 164 234009 : st->enableTcxLpc = ( st->lpcQuantization == 1 ) && ( st->total_brate <= LOWRATE_TCXLPC_MAX_BR || st->rf_mode ); 165 : } 166 : 167 563508 : if ( st->ini_frame == 0 || st->last_codec_mode == MODE1 ) 168 : { 169 563370 : st->envWeighted = 0; 170 : } 171 : 172 563508 : if ( st->bwidth == SWB && ( st->total_brate == ACELP_16k40 || st->total_brate == ACELP_24k40 ) && st->element_mode == EVS_MONO ) 173 : { 174 930 : if ( st->tec_tfa == 0 ) 175 : { 176 3 : set_zero( st->hTECEnc->loBuffer, MAX_TEC_SMOOTHING_DEG ); 177 : } 178 930 : st->tec_tfa = 1; 179 : } 180 : else 181 : { 182 562578 : st->tec_tfa = 0; 183 : } 184 : 185 563508 : st->enablePlcWaveadjust = 0; 186 563508 : if ( st->total_brate >= HQ_48k ) 187 : { 188 260340 : st->enablePlcWaveadjust = 1; 189 : } 190 : 191 563508 : st->glr = 0; 192 563508 : if ( ( st->total_brate == ACELP_9k60 || st->total_brate == ACELP_16k40 || st->total_brate == ACELP_24k40 ) && st->element_mode == EVS_MONO ) 193 : { 194 1178 : st->glr = 1; 195 : } 196 : 197 563508 : if ( st->glr ) 198 : { 199 1178 : st->nb_bits_header_ace += G_LPC_RECOVERY_BITS; 200 : } 201 : 202 563508 : if ( hTcxEnc != NULL ) 203 : { 204 563095 : if ( st->bwidth == NB || st->bwidth == WB ) 205 : { 206 41213 : 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 41213 : hTcxEnc->nmStartLine = startLineWB[index]; 208 : } 209 : else /* (st->bwidth == SWB || st->bwidth == FB) */ 210 : { 211 521882 : 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 521882 : if ( st->total_brate >= IVAS_96k && st->total_brate <= IVAS_192k && st->element_mode > IVAS_SCE ) 213 : { 214 69120 : index--; 215 : } 216 521882 : hTcxEnc->nmStartLine = startLineSWB[index]; 217 521882 : if ( st->total_brate == IVAS_48k && st->element_mode == IVAS_CPE_MDCT && hTcxEnc->nmStartLine * 5 < st->L_frame * 4 ) 218 : { 219 22392 : hTcxEnc->nmStartLine = ( hTcxEnc->nmStartLine * 5 ) >> 2; /* low-rate stereo is more efficient than dual-mono due to stereo processing */ 220 : } 221 : } 222 : } 223 : 224 563508 : if ( hTcxEnc != NULL ) 225 : { 226 563095 : 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 140074 : set_s( hTcxEnc->memQuantZeros, 0, hTcxEnc->nmStartLine ); 230 : } 231 : else 232 : { 233 423021 : if ( st->total_brate >= ACELP_24k40 && st->total_brate <= ACELP_32k && last_total_brate >= ACELP_13k20 && last_total_brate < ACELP_24k40 ) 234 : { 235 1485 : set_s( hTcxEnc->memQuantZeros, 0, st->L_frame ); 236 : } 237 : } 238 : } 239 : 240 563508 : return; 241 : }