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 <assert.h>
38 : #include <stdint.h>
39 : #include "options.h"
40 : #ifdef DEBUGGING
41 : #include "debug.h"
42 : #endif
43 : #include <math.h>
44 : #include "cnst.h"
45 : #include "prot.h"
46 : #include "rom_com.h"
47 : #include "rom_enc.h"
48 : #include "basop_proto_func.h"
49 : #include "wmc_auto.h"
50 :
51 : /*-------------------------------------------------------------------*
52 : * lpc_quantization()
53 : *
54 : *
55 : *--------------------------------------------------------------------*/
56 :
57 219005 : void lpc_quantization(
58 : Encoder_State *st,
59 : const float lsp[],
60 : const float lspmid[],
61 : float lsp_q[],
62 : float lsf_q[],
63 : float lspmid_q[],
64 : const int16_t coder_type,
65 : const int16_t acelp_midLpc,
66 : int16_t param_lpc[],
67 : int16_t nbits_lpc[],
68 : int16_t *bits_param_lpc,
69 : int16_t *no_param_lpc )
70 : {
71 : int16_t nb_indices;
72 : float lsfmid_q[M];
73 : int16_t lsfmid_idx;
74 : int16_t i;
75 : float lsf[M], lsfmid[M];
76 : int16_t force_sf;
77 : float fec_lsf[M], stab;
78 :
79 219005 : nb_indices = 0;
80 :
81 : /****** High-rate LPC quantizer *******/
82 219005 : if ( st->lpcQuantization == 0 )
83 : {
84 106898 : lsp2lsf( lsp, lsf, M, st->sr_core );
85 :
86 106898 : if ( st->core == TCX_10_CORE )
87 : {
88 2077 : lsp2lsf( lspmid, lsfmid, M, st->sr_core );
89 : }
90 :
91 106898 : qlpc_avq( lsf, lsfmid, lsf_q, lsfmid_q, param_lpc, &nb_indices, nbits_lpc, st->core, st->sr_core );
92 :
93 106898 : lsf2lsp( lsf_q, lsp_q, M, st->sr_core );
94 :
95 106898 : if ( st->core == TCX_10_CORE )
96 : {
97 2077 : lsf2lsp( lsfmid_q, lspmid_q, M, st->sr_core );
98 : }
99 :
100 106898 : assert( nb_indices <= NPRM_LPC_NEW );
101 : }
102 :
103 : /****** Low-rate LPC quantizer *******/
104 112107 : else if ( st->lpcQuantization == 1 )
105 : {
106 112107 : lsp2lsf( lsp, lsf, M, st->sr_core );
107 :
108 112107 : force_sf = 0;
109 : /*Force safety net when possible in case of transitions*/
110 112107 : if ( st->tc_cnt >= 1 || st->last_core_brate <= SID_2k40 || st->next_force_safety_net )
111 : {
112 12522 : force_sf = 1;
113 12522 : st->next_force_safety_net = 0;
114 : }
115 :
116 112107 : if ( st->next_force_safety_net == 1 && st->Opt_RF_ON )
117 : {
118 0 : force_sf = 1;
119 0 : st->next_force_safety_net = 0;
120 : }
121 :
122 112107 : if ( st->sr_core == INT_FS_16k && coder_type == UNVOICED )
123 : {
124 26 : lsf_end_enc( st, lsf, lsf_q, ENDLSF_NBITS, GENERIC, force_sf, param_lpc, no_param_lpc, bits_param_lpc, GENERIC, NULL );
125 :
126 26 : nb_indices = *no_param_lpc;
127 : }
128 : else
129 : {
130 112081 : lsf_end_enc( st, lsf, lsf_q, ENDLSF_NBITS, coder_type, force_sf, param_lpc, no_param_lpc, bits_param_lpc, coder_type, NULL );
131 :
132 112081 : nb_indices = *no_param_lpc;
133 : }
134 :
135 112107 : FEC_lsf_estim_enc( st, fec_lsf );
136 :
137 : /* FEC - calculate LSF stability */
138 112107 : stab = lsf_stab( lsf_q, fec_lsf, 0, st->L_frame );
139 :
140 112107 : if ( stab < ( STAB_FAC_LIMIT + 0.2 ) && ( coder_type == VOICED || coder_type == GENERIC ) && st->Opt_RF_ON )
141 : {
142 0 : st->next_force_safety_net = 1;
143 : }
144 :
145 112107 : lsf2lsp( lsf_q, lsp_q, M, st->sr_core );
146 :
147 112107 : *nbits_lpc = ENDLSF_NBITS;
148 : }
149 : else
150 : {
151 0 : assert( 0 );
152 : }
153 :
154 219005 : st->seed_acelp = 0;
155 1649569 : for ( i = nb_indices - 1; i >= 0; i-- )
156 : {
157 : /* rightshift before seed_acelp+param_lpc[i] to avoid overflows*/
158 1430564 : st->seed_acelp = (int16_t) ( ( ( ( st->seed_acelp ) >> 1 ) + param_lpc[i] ) * 31821L + 13849L );
159 : }
160 :
161 : /* Mid-frame LPC quantization */
162 219005 : if ( st->lpcQuantization && acelp_midLpc )
163 : {
164 733 : if ( st->rate_switching_reset == 0 )
165 : {
166 717 : lsp2lsf( lspmid, lsfmid, M, st->sr_core );
167 717 : midlsf_enc( st->lsf_old, lsf_q, lsfmid, &lsfmid_idx, M, st->Bin_E_old, st->narrowBand, st->sr_core, coder_type );
168 717 : param_lpc[nb_indices++] = lsfmid_idx;
169 :
170 717 : midlsf_dec( st->lsf_old, lsf_q, lsfmid_idx, lsfmid_q, M, coder_type, NULL, 0, 1 );
171 :
172 717 : reorder_lsf( lsfmid_q, LSF_GAP_MID, M, st->sr_core );
173 717 : lsf2lsp( lsfmid_q, lspmid_q, M, st->sr_core );
174 : }
175 : else
176 : {
177 16 : param_lpc[nb_indices++] = 0;
178 : }
179 : }
180 :
181 219005 : return;
182 : }
183 :
184 :
185 : /*-------------------------------------------------------------------*
186 : * Unified_weighting()
187 : *
188 : * LSF weighting
189 : *-------------------------------------------------------------------*/
190 :
191 446168 : void Unified_weighting(
192 : const float Bin_Ener_128[], /* i : FFT Bin energy 128 bins in two sets */
193 : const float lsf[], /* i : LSF vector */
194 : float w[], /* o : LP weighting filter (numerator) */
195 : const int16_t narrowBand, /* i : flag for Narrowband */
196 : const int16_t unvoiced, /* i : flag for Unvoiced frame */
197 : const int32_t sr_core, /* i : sampling rate of core-coder */
198 : const int16_t order /* i : LP order */
199 : )
200 : {
201 : int16_t i;
202 : float nf;
203 : const int16_t( *ptr_lsf_fit_model )[M];
204 : int16_t norm_lsf[M];
205 : float tmp, min_val;
206 : float w_fft[M];
207 : float Bin_Ener_160[160];
208 : const float *Bin_Ener;
209 : const float *Freq_w_Table;
210 : int16_t last_bin;
211 :
212 : /*Config. weighting*/
213 446168 : if ( narrowBand )
214 : {
215 0 : ptr_lsf_fit_model = lsf_unified_fit_model_nb;
216 0 : nf = 6400.f;
217 :
218 0 : last_bin = 127;
219 0 : Bin_Ener = Bin_Ener_128;
220 : }
221 446168 : else if ( sr_core == INT_FS_12k8 )
222 : {
223 221924 : ptr_lsf_fit_model = lsf_unified_fit_model_wb;
224 221924 : nf = 6400.f;
225 :
226 221924 : last_bin = 127;
227 221924 : Bin_Ener = Bin_Ener_128;
228 : }
229 : else
230 : {
231 224244 : ptr_lsf_fit_model = lsf_unified_fit_model_wbhb;
232 224244 : nf = 8000.f;
233 :
234 : /* Fill the missing part (128~159) of the bin energy */
235 224244 : last_bin = 159;
236 :
237 224244 : mvr2r( Bin_Ener_128, Bin_Ener_160, L_FFT / 2 );
238 :
239 : /* Find average bin energy (32 Energy) */
240 224244 : tmp = 0.f;
241 7400052 : for ( i = 95; i < 127; i++ )
242 : {
243 7175808 : tmp += Bin_Ener_160[i];
244 : }
245 :
246 224244 : tmp = tmp / 32.f;
247 7624296 : for ( i = 127; i < 160; i++ )
248 : {
249 7400052 : Bin_Ener_160[i] = tmp;
250 : }
251 :
252 224244 : Bin_Ener = Bin_Ener_160;
253 : }
254 :
255 : /* 1) FFT weights*/
256 446168 : if ( unvoiced )
257 : {
258 4979 : Freq_w_Table = Freq_Weight_UV;
259 : }
260 : else
261 : {
262 441189 : Freq_w_Table = Freq_Weight_Com;
263 : }
264 :
265 : /* Use Envelope */
266 446168 : min_val = 1000.0f;
267 7584856 : for ( i = 0; i < M; i++ )
268 : {
269 7138688 : norm_lsf[i] = (int16_t) ( ( lsf[i] / 50.f ) + 0.5f );
270 :
271 7138688 : if ( norm_lsf[i] == 0 )
272 : {
273 0 : w_fft[i] = Bin_Ener[1];
274 : }
275 7138688 : else if ( norm_lsf[i] == last_bin )
276 : {
277 35 : w_fft[i] = Bin_Ener[last_bin - 1];
278 : }
279 : else
280 : {
281 7138653 : tmp = max( Bin_Ener[norm_lsf[i]], Bin_Ener[norm_lsf[i] - 1] );
282 7138653 : w_fft[i] = max( Bin_Ener[norm_lsf[i] + 1], tmp );
283 : }
284 :
285 7138688 : if ( w_fft[i] < MIN_LOG_60dB )
286 : {
287 1460 : w_fft[i] = MIN_LOG_VAL_60dB;
288 : }
289 : else
290 : {
291 7137228 : w_fft[i] = (float) ( 10.0 * log10( w_fft[i] ) );
292 : }
293 :
294 7138688 : if ( w_fft[i] < min_val )
295 : {
296 2497294 : min_val = w_fft[i];
297 : }
298 : }
299 :
300 7584856 : for ( i = 0; i < M; i++ )
301 : {
302 7138688 : w_fft[i] = (float) ( sqrt( w_fft[i] - min_val ) + 2.f );
303 7138688 : w_fft[i] *= Freq_w_Table[norm_lsf[i]];
304 : }
305 :
306 : /* 2) IHM weights*/
307 446168 : w[0] = 1.f / ( lsf[0] - 0 ) + 1.f / ( lsf[1] - lsf[0] );
308 6692520 : for ( i = 1; i < order - 1; i++ )
309 : {
310 6246352 : w[i] = 1.f / ( lsf[i] - lsf[i - 1] ) + 1.f / ( lsf[i + 1] - lsf[i] );
311 : }
312 446168 : w[order - 1] = 1.f / ( lsf[order - 1] - lsf[order - 2] ) +
313 446168 : 1.f / ( nf - lsf[order - 1] );
314 :
315 : /* 3) Fitting model combining the two weights*/
316 7584856 : for ( i = 0; i < order; i++ )
317 : {
318 7138688 : w[i] *= ( nf / EVS_PI );
319 7138688 : w[i] = ( (float) ( ptr_lsf_fit_model[0][i] ) / ( 1 << 10 ) ) + w[i] * ( (float) ( ptr_lsf_fit_model[1][i] ) / ( 1 << 15 ) ) + w[i] * w[i] * ( (float) ( ptr_lsf_fit_model[2][i] ) / ( 1 << 18 ) ) + w_fft[i] * ( (float) ( ptr_lsf_fit_model[3][i] ) / ( 1 << 12 ) );
320 :
321 7138688 : if ( w[i] < 1.f / ( i + 1 ) )
322 : {
323 4 : w[i] = 1.f / ( i + 1 );
324 : }
325 : }
326 :
327 446168 : return;
328 : }
|