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 "wmc_auto.h"
45 :
46 : /*-------------------------------------------------------------------*
47 : * core_coder_mode_switch()
48 : *
49 : *
50 : *-------------------------------------------------------------------*/
51 :
52 32649 : void core_coder_mode_switch(
53 : Encoder_State *st, /* i/o: encoder state structure */
54 : const int32_t last_total_brate, /* i : last bitrate */
55 : const int16_t MCT_flag /* i : hMCT handle allocated (1) or not (0)*/
56 : )
57 : {
58 32649 : TCX_ENC_HANDLE hTcxEnc = st->hTcxEnc;
59 : int16_t i, fscale, switchWB;
60 : int32_t sr_core;
61 : int16_t bSwitchFromAmrwbIO;
62 : int16_t tcxonly_tmp;
63 :
64 32649 : switchWB = 0;
65 32649 : bSwitchFromAmrwbIO = 0;
66 32649 : if ( st->last_core == AMR_WB_CORE )
67 : {
68 0 : bSwitchFromAmrwbIO = 1;
69 : }
70 :
71 : /* force active frame for the first frame when switching from high bitrates when DTX is enabled*/
72 32649 : sr_core = getCoreSamplerateMode2( st->element_mode, st->total_brate, st->bwidth, st->flag_ACELP16k, st->rf_mode, st->is_ism_format );
73 :
74 32649 : fscale = sr2fscale( sr_core );
75 :
76 :
77 32649 : if ( st->last_codec_mode == MODE1 )
78 : {
79 32649 : switchWB = 1; /*force init when coming from MODE1*/
80 : }
81 :
82 32649 : tcxonly_tmp = getTcxonly( st->element_mode, st->total_brate, MCT_flag, st->is_ism_format );
83 :
84 32649 : if ( tcxonly_tmp != st->tcxonly )
85 : {
86 2758 : switchWB = 1; /*force init when coming from MODE1*/
87 : }
88 :
89 32649 : if ( fscale == st->fscale && !bSwitchFromAmrwbIO && !switchWB )
90 : {
91 0 : st->sr_core = sr_core;
92 0 : st->L_frame = (int16_t) ( sr_core / FRAMES_PER_SEC );
93 :
94 0 : st->tcxonly = getTcxonly( st->element_mode, st->total_brate, MCT_flag, st->is_ism_format );
95 :
96 0 : st->bits_frame_nominal = (int16_t) ( (float) st->L_frame / (float) st->fscale * (float) FSCALE_DENOM / 128.0f * (float) st->total_brate / 100.0f + 0.49f );
97 :
98 0 : st->igf = getIgfPresent( st->element_mode, st->total_brate, st->bwidth, st->rf_mode );
99 :
100 : /* switch IGF configuration */
101 0 : if ( st->igf )
102 : {
103 0 : IGFEncSetMode( st->hIGFEnc, st->total_brate, st->bwidth, st->element_mode, st->rf_mode );
104 : }
105 :
106 0 : st->hTcxCfg->tcx_coded_lines = getNumTcxCodedLines( st->bwidth );
107 0 : st->hTcxCfg->bandwidth = getTcxBandwidth( st->bwidth );
108 0 : st->hTcxCfg->tcxRateLoopOpt = ( st->tcxonly ) ? 2 : 0;
109 0 : st->hTcxCfg->tcxRateLoopOpt = ( st->element_mode == IVAS_CPE_MDCT ) ? 3 : st->hTcxCfg->tcxRateLoopOpt;
110 0 : st->hTcxCfg->ctx_hm = getCtxHm( st->element_mode, st->total_brate, st->rf_mode );
111 0 : st->hTcxCfg->resq = getResq( st->total_brate );
112 0 : hTcxEnc->tcx_lpc_shaped_ari = getTcxLpcShapedAri( st->total_brate, st->rf_mode, st->element_mode );
113 :
114 0 : st->hTcxCfg->tcxRateLoopOpt = ( st->hTcxCfg->resq && !st->tcxonly ) ? 1 : st->hTcxCfg->tcxRateLoopOpt;
115 0 : st->hTcxCfg->fIsTNSAllowed = getTnsAllowed( st->total_brate, st->igf, st->element_mode );
116 :
117 0 : if ( st->hTcxCfg->fIsTNSAllowed )
118 : {
119 0 : InitTnsConfigs( st->bwidth, st->hTcxCfg->tcx_coded_lines, st->hTcxCfg->tnsConfig, st->hIGFEnc->infoStopFrequency, st->total_brate, st->element_mode, MCT_flag );
120 :
121 0 : SetAllowTnsOnWhite( st->hTcxCfg->tnsConfig, st->element_mode == IVAS_CPE_MDCT );
122 : }
123 :
124 0 : if ( st->bwidth == NB )
125 : {
126 0 : st->narrowBand = 1;
127 0 : st->min_band = 1;
128 0 : st->max_band = 16;
129 : }
130 : else
131 : {
132 0 : st->narrowBand = 0;
133 0 : st->min_band = 0;
134 0 : st->max_band = 19;
135 : }
136 :
137 0 : for ( i = 0; i < FRAME_SIZE_NB; i++ )
138 : {
139 0 : if ( FrameSizeConfig[i].frame_bits == st->bits_frame_nominal )
140 : {
141 0 : st->frame_size_index = i;
142 0 : st->bits_frame = FrameSizeConfig[i].frame_bits;
143 0 : st->bits_frame_core = FrameSizeConfig[i].frame_net_bits;
144 0 : break;
145 : }
146 : }
147 :
148 0 : st->restrictedMode = getRestrictedMode( st->element_mode, st->total_brate, 0 );
149 :
150 0 : core_coder_reconfig( st, last_total_brate );
151 : }
152 : else
153 : {
154 32649 : st->igf = getIgfPresent( st->element_mode, st->total_brate, st->bwidth, st->rf_mode );
155 32649 : init_coder_ace_plus( st, last_total_brate, st->total_brate, MCT_flag );
156 : }
157 :
158 32649 : if ( st->igf && st->hBWE_TD != NULL )
159 : {
160 : /* reset TBE */
161 17877 : if ( ( st->bwidth == WB && st->last_extl != WB_TBE ) ||
162 17847 : ( st->bwidth == SWB && st->last_extl != SWB_TBE ) ||
163 14373 : ( st->bwidth == FB && st->last_extl != FB_TBE ) )
164 : {
165 11631 : TBEreset_enc( st->hBWE_TD, st->last_core, st->bwidth );
166 : }
167 : else
168 : {
169 6246 : set_f( st->hBWE_TD->state_lpc_syn, 0.0f, LPC_SHB_ORDER );
170 6246 : set_f( st->hBWE_TD->state_syn_shbexc, 0.0f, L_SHB_LAHEAD );
171 6246 : set_f( st->hBWE_TD->mem_stp_swb, 0.0f, LPC_SHB_ORDER );
172 6246 : set_f( st->hBWE_TD->mem_zero_swb, 0, LPC_SHB_ORDER );
173 6246 : st->hBWE_TD->gain_prec_swb = 1.0f;
174 : }
175 : }
176 :
177 32649 : if ( st->envWeighted && !st->enableTcxLpc )
178 : {
179 : /* Unweight the envelope */
180 0 : E_LPC_lsp_unweight( st->lsp_old, st->lsp_old, st->lsf_old, 1.0f / st->gamma );
181 0 : st->envWeighted = 0;
182 : }
183 :
184 32649 : if ( st->total_brate >= HQ_48k )
185 : {
186 13332 : st->enablePlcWaveadjust = 1;
187 : }
188 : else
189 : {
190 19317 : st->enablePlcWaveadjust = 0;
191 : }
192 :
193 32649 : if ( ( last_total_brate > HQ_32k || st->last_codec_mode == MODE1 ) && st->element_mode == EVS_MONO )
194 : {
195 98 : st->glr_reset = 1;
196 : }
197 :
198 32649 : return;
199 : }
|