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 136355 : 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 136355 : int16_t time_flg = 0;
86 : int16_t Len, max_len;
87 : int16_t tmp_dec;
88 136355 : int16_t Mbands_loc = MBANDS_LOC - 2;
89 : BSTR_ENC_HANDLE hBstr;
90 :
91 : /* initialization */
92 136355 : hGSCEnc = st->hGSCEnc;
93 136355 : hBstr = st->hBstr;
94 :
95 136355 : if ( st->L_frame == L_FRAME16k )
96 : {
97 22563 : Mbands_loc = MBANDS_LOC;
98 : }
99 :
100 136355 : minimum( pitch_buf, st->L_frame >> 6, &low_pit );
101 :
102 136355 : F1st_harm = (float) INT_FS_12k8 / low_pit;
103 136355 : if ( st->L_frame == L_FRAME16k )
104 : {
105 22563 : F1st_harm = (float) INT_FS_16k / low_pit;
106 : }
107 136355 : F8th_harm = 8.0f * F1st_harm;
108 :
109 136355 : freq = 0;
110 1954096 : for ( i = 0; i < Mbands_loc; i++ )
111 : {
112 1817741 : corr_tmp = 0.0f;
113 1817741 : ener_res = 0.1f;
114 1817741 : ener_pit = 0.1f;
115 :
116 28719917 : for ( j = 0; j < mfreq_bindiv_loc[i]; j++ )
117 : {
118 26902176 : corr_tmp += dct_res[j + freq] * dct_pitex[j + freq];
119 26902176 : ener_res += dct_res[j + freq] * dct_res[j + freq];
120 26902176 : ener_pit += dct_pitex[j + freq] * dct_pitex[j + freq];
121 : }
122 :
123 1817741 : corr_dct_pit[i] = (float) ( corr_tmp / sqrt( ener_res * ener_pit ) );
124 1817741 : 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 136355 : corr_dct_pit_tmp[0] = ALPA * corr_dct_pit[0] + ALPAM1 * corr_dct_pit[1];
129 136355 : if ( corr_dct_pit_tmp[0] < 0.5f )
130 : {
131 81020 : corr_dct_pit_tmp[0] = 0.5f;
132 : }
133 136355 : corr_dct_pit_tmp[0] = ( corr_dct_pit_tmp[0] - 0.5f ) * 2.0f;
134 :
135 1681386 : for ( i = 1; i < Mbands_loc - 1; i++ )
136 : {
137 1545031 : corr_dct_pit_tmp[i] = ALPA * corr_dct_pit[i] + BETA * corr_dct_pit[i + 1] + BETA * corr_dct_pit[i - 1];
138 1545031 : if ( corr_dct_pit_tmp[i] < 0.5f )
139 : {
140 1306669 : corr_dct_pit_tmp[i] = 0.5f;
141 : }
142 1545031 : corr_dct_pit_tmp[i] = ( corr_dct_pit_tmp[i] - 0.5f ) * 2.0f;
143 : }
144 :
145 136355 : corr_dct_pit_tmp[i] = ALPA * corr_dct_pit[i] + ALPAM1 * corr_dct_pit[i - 1];
146 :
147 136355 : if ( corr_dct_pit_tmp[i] < 0.5f )
148 : {
149 131622 : corr_dct_pit_tmp[i] = 0.5f;
150 : }
151 136355 : corr_dct_pit_tmp[i] = ( corr_dct_pit_tmp[i] - 0.5f ) * 2.0f;
152 :
153 1954096 : for ( i = 0; i < Mbands_loc; i++ )
154 : {
155 1817741 : corr_dct_pit[i] = corr_dct_pit_tmp[i];
156 : }
157 :
158 136355 : av_corr = DIV_NB_VOIC * corr_dct_pit[0];
159 1772615 : for ( i = 1; i < NB_VOIC; i++ )
160 : {
161 1636260 : av_corr += DIV_NB_VOIC * corr_dct_pit[i];
162 : }
163 :
164 : /* Find the cut-off freq similarly to HSX */
165 136355 : last_pit_band = 0;
166 :
167 136355 : av_corr *= 6400;
168 136355 : if ( st->L_frame == L_FRAME16k )
169 : {
170 22563 : av_corr *= 1.25f;
171 : }
172 :
173 136355 : if ( st->GSC_IVAS_mode >= 1 || st->core_brate < ACELP_9k60 )
174 : {
175 : /* Correlation really poor at low rate, time domain still valide */
176 94071 : av_corr *= 2.0;
177 : }
178 :
179 136355 : min_corr = (float) fabs( mfreq_loc[0] - av_corr );
180 :
181 1817741 : for ( i = 1; i < Mbands_loc; i++ )
182 : {
183 1681386 : ftmp = (float) fabs( mfreq_loc[i] - av_corr );
184 1681386 : if ( ftmp < min_corr )
185 : {
186 285062 : last_pit_band = i;
187 285062 : min_corr = ftmp;
188 : }
189 : }
190 :
191 136355 : if ( F8th_harm > mfreq_loc[last_pit_band] )
192 : {
193 : do
194 : {
195 599166 : last_pit_band++;
196 599166 : } while ( F8th_harm >= mfreq_loc[last_pit_band] );
197 : }
198 :
199 136355 : if ( st->GSC_IVAS_mode >= 1 )
200 : {
201 67822 : last_pit_band = max( last_pit_band, 7 );
202 : }
203 :
204 136355 : if ( last_pit_band > 7 + BAND1k2 && ( st->core_brate < CFREQ_BITRATE || st->bwidth == NB ) )
205 : {
206 13101 : last_pit_band = 7 + BAND1k2;
207 : }
208 123254 : else if ( last_pit_band > 10 + BAND1k2 && st->core_brate >= CFREQ_BITRATE )
209 : {
210 3518 : last_pit_band = 10 + BAND1k2;
211 : }
212 :
213 136355 : time_flg = 0;
214 136355 : 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 102945 : tmp_dec = 1;
217 : }
218 : else
219 : {
220 33410 : tmp_dec = 0;
221 : }
222 :
223 : /* Different past and current decision */
224 136355 : if ( ( hGSCEnc->mem_last_pit_band == 0 && tmp_dec == 1 ) || ( hGSCEnc->mem_last_pit_band > 0 && tmp_dec == 0 ) )
225 : {
226 25408 : if ( *hangover == 0 )
227 : {
228 4373 : time_flg = tmp_dec;
229 4373 : *hangover = HANGOVER_DELAY;
230 : }
231 : else
232 : {
233 21035 : time_flg = 0;
234 21035 : if ( hGSCEnc->mem_last_pit_band > 0 )
235 : {
236 15952 : time_flg = 1;
237 : }
238 :
239 21035 : ( *hangover ) -= 1;
240 21035 : if ( *hangover < 0 )
241 : {
242 0 : *hangover = 0;
243 : }
244 : }
245 : }
246 : else
247 : {
248 110947 : time_flg = tmp_dec;
249 110947 : *hangover = HANGOVER_DELAY;
250 : }
251 :
252 : /* Decicison on final length of time contribution */
253 136355 : pit_contr_idx = 0;
254 136355 : if ( time_flg == 1 || st->coder_type != INACTIVE || st->GSC_noisy_speech )
255 : {
256 115900 : if ( st->core_brate < ACELP_9k60 && low_pit < 64 )
257 : {
258 28267 : last_pit_band = 9 + BAND1k2;
259 28267 : if ( st->bwidth == NB )
260 : {
261 119 : last_pit_band = 7 + BAND1k2;
262 : }
263 : }
264 87633 : else if ( st->core_brate < ACELP_9k60 && low_pit < 128 )
265 : {
266 16735 : last_pit_band = 5 + BAND1k2;
267 : }
268 70898 : else if ( st->core_brate < ACELP_9k60 )
269 : {
270 9373 : last_pit_band = 3 + BAND1k2;
271 : }
272 61525 : else if ( last_pit_band < BAND1k2 + 1 )
273 : {
274 5450 : last_pit_band = BAND1k2 + 1;
275 : }
276 :
277 115900 : last_pit_bin = (int16_t) ( mfreq_loc[last_pit_band] / BIN_SIZE );
278 :
279 115900 : st->bpf_off = 0;
280 :
281 115900 : max_len = st->L_frame - last_pit_bin;
282 115900 : if ( st->bwidth == NB )
283 : {
284 298 : max_len = 160 - last_pit_bin;
285 : }
286 :
287 115900 : Len = 80;
288 115900 : if ( max_len < 80 )
289 : {
290 32997 : Len = max_len;
291 : }
292 :
293 115900 : if ( st->core_brate == ACELP_8k00 && st->bwidth != NB )
294 : {
295 103486 : for ( i = 0; i < max_len; i++ )
296 : {
297 102431 : dct_pitex[i + last_pit_bin] = 0.0f;
298 : }
299 : }
300 : else
301 : {
302 8758175 : for ( i = 0; i < Len; i++ )
303 : {
304 8643330 : dct_pitex[i + last_pit_bin] *= sm_table[i];
305 : }
306 :
307 5577272 : for ( ; i < max_len; i++ )
308 : {
309 5462427 : dct_pitex[i + last_pit_bin] = 0.0f;
310 : }
311 : }
312 :
313 115900 : hGSCEnc->mem_last_pit_band = last_pit_band;
314 115900 : pit_contr_idx = last_pit_band - BAND1k2;
315 : }
316 : else
317 : {
318 20455 : set_f( dct_pitex, 0.0f, st->L_frame );
319 20455 : st->bpf_off = 1;
320 20455 : last_pit_bin = 0;
321 20455 : last_pit_band = 0;
322 20455 : pit_contr_idx = 0;
323 20455 : hGSCEnc->mem_last_pit_band = 0;
324 20455 : set_f( pitch_buf, (float) L_SUBFR, NB_SUBFR16k );
325 :
326 : /* pitch contribution useless - delete all previously written indices belonging to pitch contribution */
327 6156955 : for ( i = TAG_ACELP_SUBFR_LOOP_START; i < TAG_ACELP_SUBFR_LOOP_END; i++ )
328 : {
329 6136500 : delete_indice( hBstr, i );
330 : }
331 :
332 20455 : delete_indice( hBstr, IND_ES_PRED );
333 : }
334 :
335 136355 : if ( st->core_brate < CFREQ_BITRATE )
336 : {
337 79221 : if ( st->core_brate < ACELP_9k60 )
338 : {
339 55734 : if ( pit_contr_idx > 0 )
340 : {
341 54375 : pit_contr_idx = 1;
342 : }
343 :
344 55734 : if ( st->coder_type == INACTIVE )
345 : {
346 3565 : push_indice( hBstr, IND_PIT_CONTR_IDX, pit_contr_idx, 1 );
347 : }
348 : }
349 : else
350 : {
351 23487 : push_indice( hBstr, IND_PIT_CONTR_IDX, pit_contr_idx, 3 );
352 : }
353 : }
354 : else
355 : {
356 57134 : push_indice( hBstr, IND_PIT_CONTR_IDX, pit_contr_idx, 4 );
357 : }
358 :
359 136355 : return last_pit_bin;
360 : }
|