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 <math.h>
43 : #include "cnst.h"
44 : #include "rom_com.h"
45 : #include "prot.h"
46 : #include "wmc_auto.h"
47 :
48 : /*-------------------------------------------------------------------*
49 : * Local constantes
50 : *-------------------------------------------------------------------*/
51 :
52 : #define NB_VOIC 13
53 : #define DIV_NB_VOIC ( 1.0f / NB_VOIC )
54 : #define ALPA 0.95f
55 : #define ALPAM1 ( 1.0f - ALPA )
56 : #define BETA ( ALPAM1 / 2.0f )
57 : #define AFREQ_THR 2
58 : #define GPIT_THR 0.4f
59 : #define HANGOVER_DELAY 2
60 :
61 : /*-------------------------------------------------------------------*
62 : * Pit_exc_contribution_len()
63 : *
64 : * Determine up to which band the pit contribution is significant
65 : *-------------------------------------------------------------------*/
66 :
67 : /*! r: bin where pitch contribution is significant */
68 15245 : int16_t Pit_exc_contribution_len(
69 : Encoder_State *st, /* i/o: state structure */
70 : const float *dct_res, /* i : DCT of residual */
71 : float *dct_pitex, /* i/o: DCT of pitch contribution */
72 : float *pitch_buf, /* i/o: Pitch per subframe */
73 : int16_t *hangover /* i : hangover for the time contribution switching*/
74 : )
75 : {
76 : float corr_dct_pit[MBANDS_LOC], corr_tmp;
77 : float av_corr, min_corr, ftmp;
78 : int16_t freq, i, j;
79 : int16_t last_pit_band, pit_contr_idx, last_pit_bin;
80 : GSC_ENC_HANDLE hGSCEnc;
81 : float ener_res;
82 : float ener_pit;
83 : float low_pit, F1st_harm, F8th_harm;
84 : float corr_dct_pit_tmp[MBANDS_LOC];
85 15245 : int16_t time_flg = 0;
86 : int16_t Len, max_len;
87 : int16_t tmp_dec;
88 15245 : int16_t Mbands_loc = MBANDS_LOC - 2;
89 : BSTR_ENC_HANDLE hBstr;
90 :
91 : /* initialization */
92 15245 : hGSCEnc = st->hGSCEnc;
93 15245 : hBstr = st->hBstr;
94 :
95 15245 : if ( st->L_frame == L_FRAME16k )
96 : {
97 2637 : Mbands_loc = MBANDS_LOC;
98 : }
99 :
100 15245 : minimum( pitch_buf, st->L_frame >> 6, &low_pit );
101 :
102 15245 : F1st_harm = (float) INT_FS_12k8 / low_pit;
103 15245 : if ( st->L_frame == L_FRAME16k )
104 : {
105 2637 : F1st_harm = (float) INT_FS_16k / low_pit;
106 : }
107 15245 : F8th_harm = 8.0f * F1st_harm;
108 :
109 15245 : freq = 0;
110 218704 : for ( i = 0; i < Mbands_loc; i++ )
111 : {
112 203459 : corr_tmp = 0.0f;
113 203459 : ener_res = 0.1f;
114 203459 : ener_pit = 0.1f;
115 :
116 3214883 : for ( j = 0; j < mfreq_bindiv_loc[i]; j++ )
117 : {
118 3011424 : corr_tmp += dct_res[j + freq] * dct_pitex[j + freq];
119 3011424 : ener_res += dct_res[j + freq] * dct_res[j + freq];
120 3011424 : ener_pit += dct_pitex[j + freq] * dct_pitex[j + freq];
121 : }
122 :
123 203459 : corr_dct_pit[i] = (float) ( corr_tmp / sqrt( ener_res * ener_pit ) );
124 203459 : freq += mfreq_bindiv_loc[i];
125 : }
126 :
127 : /* Smooth the inter-correlation value and skip the last band for the average (since last band is almost always 0)*/
128 15245 : corr_dct_pit_tmp[0] = ALPA * corr_dct_pit[0] + ALPAM1 * corr_dct_pit[1];
129 15245 : if ( corr_dct_pit_tmp[0] < 0.5f )
130 : {
131 8583 : corr_dct_pit_tmp[0] = 0.5f;
132 : }
133 15245 : corr_dct_pit_tmp[0] = ( corr_dct_pit_tmp[0] - 0.5f ) * 2.0f;
134 :
135 188214 : for ( i = 1; i < Mbands_loc - 1; i++ )
136 : {
137 172969 : corr_dct_pit_tmp[i] = ALPA * corr_dct_pit[i] + BETA * corr_dct_pit[i + 1] + BETA * corr_dct_pit[i - 1];
138 172969 : if ( corr_dct_pit_tmp[i] < 0.5f )
139 : {
140 141076 : corr_dct_pit_tmp[i] = 0.5f;
141 : }
142 172969 : corr_dct_pit_tmp[i] = ( corr_dct_pit_tmp[i] - 0.5f ) * 2.0f;
143 : }
144 :
145 15245 : corr_dct_pit_tmp[i] = ALPA * corr_dct_pit[i] + ALPAM1 * corr_dct_pit[i - 1];
146 :
147 15245 : if ( corr_dct_pit_tmp[i] < 0.5f )
148 : {
149 14577 : corr_dct_pit_tmp[i] = 0.5f;
150 : }
151 15245 : corr_dct_pit_tmp[i] = ( corr_dct_pit_tmp[i] - 0.5f ) * 2.0f;
152 :
153 218704 : for ( i = 0; i < Mbands_loc; i++ )
154 : {
155 203459 : corr_dct_pit[i] = corr_dct_pit_tmp[i];
156 : }
157 :
158 15245 : av_corr = DIV_NB_VOIC * corr_dct_pit[0];
159 198185 : for ( i = 1; i < NB_VOIC; i++ )
160 : {
161 182940 : av_corr += DIV_NB_VOIC * corr_dct_pit[i];
162 : }
163 :
164 : /* Find the cut-off freq similarly to HSX */
165 15245 : last_pit_band = 0;
166 :
167 15245 : av_corr *= 6400;
168 15245 : if ( st->L_frame == L_FRAME16k )
169 : {
170 2637 : av_corr *= 1.25f;
171 : }
172 :
173 15245 : if ( st->GSC_IVAS_mode >= 1 || st->core_brate < ACELP_9k60 )
174 : {
175 : /* Correlation really poor at low rate, time domain still valide */
176 10614 : av_corr *= 2.0;
177 : }
178 :
179 15245 : min_corr = (float) fabs( mfreq_loc[0] - av_corr );
180 :
181 203459 : for ( i = 1; i < Mbands_loc; i++ )
182 : {
183 188214 : ftmp = (float) fabs( mfreq_loc[i] - av_corr );
184 188214 : if ( ftmp < min_corr )
185 : {
186 39803 : last_pit_band = i;
187 39803 : min_corr = ftmp;
188 : }
189 : }
190 :
191 15245 : if ( F8th_harm > mfreq_loc[last_pit_band] )
192 : {
193 : do
194 : {
195 55606 : last_pit_band++;
196 55606 : } while ( F8th_harm >= mfreq_loc[last_pit_band] );
197 : }
198 :
199 15245 : if ( st->GSC_IVAS_mode >= 1 )
200 : {
201 4984 : last_pit_band = max( last_pit_band, 7 );
202 : }
203 :
204 15245 : if ( last_pit_band > 7 + BAND1k2 && ( st->core_brate < CFREQ_BITRATE || st->bwidth == NB ) )
205 : {
206 1421 : last_pit_band = 7 + BAND1k2;
207 : }
208 13824 : else if ( last_pit_band > 10 + BAND1k2 && st->core_brate >= CFREQ_BITRATE )
209 : {
210 335 : last_pit_band = 10 + BAND1k2;
211 : }
212 :
213 15245 : time_flg = 0;
214 15245 : if ( ( hGSCEnc->mem_last_pit_band > 0 && st->old_corr > 0.5f && st->hSpMusClas->mold_corr > 0.5f && hGSCEnc->lt_gpitch >= 1.5f * GPIT_THR ) || ( last_pit_band > 6 ) || ( last_pit_band >= 4 && hGSCEnc->lt_gpitch >= 1.5f * GPIT_THR && st->old_corr > 0.7f ) || ( last_pit_band > BAND1k2 && st->hSpMusClas->mold_corr > 0.80f && hGSCEnc->lt_gpitch >= GPIT_THR ) )
215 : {
216 10406 : tmp_dec = 1;
217 : }
218 : else
219 : {
220 4839 : tmp_dec = 0;
221 : }
222 :
223 : /* Different past and current decision */
224 15245 : if ( ( hGSCEnc->mem_last_pit_band == 0 && tmp_dec == 1 ) || ( hGSCEnc->mem_last_pit_band > 0 && tmp_dec == 0 ) )
225 : {
226 4430 : if ( *hangover == 0 )
227 : {
228 814 : time_flg = tmp_dec;
229 814 : *hangover = HANGOVER_DELAY;
230 : }
231 : else
232 : {
233 3616 : time_flg = 0;
234 3616 : if ( hGSCEnc->mem_last_pit_band > 0 )
235 : {
236 2856 : time_flg = 1;
237 : }
238 :
239 3616 : ( *hangover ) -= 1;
240 3616 : if ( *hangover < 0 )
241 : {
242 0 : *hangover = 0;
243 : }
244 : }
245 : }
246 : else
247 : {
248 10815 : time_flg = tmp_dec;
249 10815 : *hangover = HANGOVER_DELAY;
250 : }
251 :
252 : /* Decicison on final length of time contribution */
253 15245 : pit_contr_idx = 0;
254 15245 : if ( time_flg == 1 || st->coder_type != INACTIVE || st->GSC_noisy_speech )
255 : {
256 13003 : if ( st->core_brate < ACELP_9k60 && low_pit < 64 )
257 : {
258 3375 : last_pit_band = 9 + BAND1k2;
259 3375 : if ( st->bwidth == NB )
260 : {
261 0 : last_pit_band = 7 + BAND1k2;
262 : }
263 : }
264 9628 : else if ( st->core_brate < ACELP_9k60 && low_pit < 128 )
265 : {
266 2467 : last_pit_band = 5 + BAND1k2;
267 : }
268 7161 : else if ( st->core_brate < ACELP_9k60 )
269 : {
270 1828 : last_pit_band = 3 + BAND1k2;
271 : }
272 5333 : else if ( last_pit_band < BAND1k2 + 1 )
273 : {
274 706 : last_pit_band = BAND1k2 + 1;
275 : }
276 :
277 13003 : last_pit_bin = (int16_t) ( mfreq_loc[last_pit_band] / BIN_SIZE );
278 :
279 13003 : st->bpf_off = 0;
280 :
281 13003 : max_len = st->L_frame - last_pit_bin;
282 13003 : if ( st->bwidth == NB )
283 : {
284 0 : max_len = 160 - last_pit_bin;
285 : }
286 :
287 13003 : Len = 80;
288 13003 : if ( max_len < 80 )
289 : {
290 3633 : Len = max_len;
291 : }
292 :
293 13003 : if ( st->core_brate == ACELP_8k00 && st->bwidth != NB )
294 : {
295 4152 : for ( i = 0; i < max_len; i++ )
296 : {
297 4108 : dct_pitex[i + last_pit_bin] = 0.0f;
298 : }
299 : }
300 : else
301 : {
302 992534 : for ( i = 0; i < Len; i++ )
303 : {
304 979575 : dct_pitex[i + last_pit_bin] *= sm_table[i];
305 : }
306 :
307 671319 : for ( ; i < max_len; i++ )
308 : {
309 658360 : dct_pitex[i + last_pit_bin] = 0.0f;
310 : }
311 : }
312 :
313 13003 : hGSCEnc->mem_last_pit_band = last_pit_band;
314 13003 : pit_contr_idx = last_pit_band - BAND1k2;
315 : }
316 : else
317 : {
318 2242 : set_f( dct_pitex, 0.0f, st->L_frame );
319 2242 : st->bpf_off = 1;
320 2242 : last_pit_bin = 0;
321 2242 : last_pit_band = 0;
322 2242 : pit_contr_idx = 0;
323 2242 : hGSCEnc->mem_last_pit_band = 0;
324 2242 : set_f( pitch_buf, (float) L_SUBFR, NB_SUBFR16k );
325 :
326 : /* pitch contribution useless - delete all previously written indices belonging to pitch contribution */
327 674842 : for ( i = TAG_ACELP_SUBFR_LOOP_START; i < TAG_ACELP_SUBFR_LOOP_END; i++ )
328 : {
329 672600 : delete_indice( hBstr, i );
330 : }
331 :
332 2242 : delete_indice( hBstr, IND_ES_PRED );
333 : }
334 :
335 15245 : if ( st->core_brate < CFREQ_BITRATE )
336 : {
337 9908 : if ( st->core_brate < ACELP_9k60 )
338 : {
339 7841 : if ( pit_contr_idx > 0 )
340 : {
341 7670 : pit_contr_idx = 1;
342 : }
343 :
344 7841 : if ( st->coder_type == INACTIVE )
345 : {
346 335 : push_indice( hBstr, IND_PIT_CONTR_IDX, pit_contr_idx, 1 );
347 : }
348 : }
349 : else
350 : {
351 2067 : push_indice( hBstr, IND_PIT_CONTR_IDX, pit_contr_idx, 3 );
352 : }
353 : }
354 : else
355 : {
356 5337 : push_indice( hBstr, IND_PIT_CONTR_IDX, pit_contr_idx, 4 );
357 : }
358 :
359 15245 : return last_pit_bin;
360 : }
|