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 function prototypes
50 : *-----------------------------------------------------------------*/
51 :
52 : static void gain_trans_enc( float *gain_trans, float exc[], int16_t *quant_index, int16_t *quant_sign );
53 :
54 : static void tc_enc( Encoder_State *st, const int16_t i_subfr, int16_t *tc_subfr, int16_t *position, const float *h1, const float *xn, float *exc, float *y1, int16_t *T0_min, int16_t *T0_max, int16_t *T0, int16_t *T0_frac, float *gain_pit, float g_corr[], float *bwe_exc );
55 :
56 :
57 : /*-----------------------------------------------------------------*
58 : * transition_enc()
59 : *
60 : * Principal function for adaptive excitation construction in TC
61 : *-----------------------------------------------------------------*/
62 :
63 64283 : void transition_enc(
64 : Encoder_State *st, /* i/o: encoder state structure */
65 : const int16_t i_subfr, /* i : subframe index */
66 : int16_t *tc_subfr, /* i/o: TC subframe index */
67 : int16_t *Jopt_flag, /* i : joint optimization flag */
68 : int16_t *position, /* i/o: maximum of residual signal index */
69 : int16_t *T0, /* i/o: close loop integer pitch */
70 : int16_t *T0_frac, /* i/o: close loop fractional part of the pitch */
71 : int16_t *T0_min, /* i/o: lower limit for close-loop search */
72 : int16_t *T0_max, /* i/o: higher limit for close-loop search */
73 : float *exc, /* i/o: pointer to excitation signal frame */
74 : float *y1, /* o : zero-memory filtered adaptive excitation */
75 : const float *h1, /* i : weighted filter input response */
76 : const float *xn, /* i : target vector */
77 : float *xn2, /* o : target vector for innovation search */
78 : float *gp_cl, /* i/o: memory of gain of pitch clipping algorithm */
79 : float *gain_pit, /* o : adaptive excitation gain */
80 : float *g_corr, /* o : ACELP correlation values */
81 : int16_t *clip_gain, /* i/o: adaptive gain clipping flag */
82 : float **pt_pitch, /* o : floating pitch values */
83 : float *bwe_exc, /* o : excitation for SWB TBE */
84 : int16_t *unbits_ACELP /* i/o: unused bits */
85 : )
86 : {
87 : int16_t i, pit_flag, pit_start, pit_limit, index, nBits;
88 : int16_t limit_flag, mult_Top;
89 : int16_t lp_select, lp_flag;
90 64283 : BSTR_ENC_HANDLE hBstr = st->hBstr;
91 : int16_t offset;
92 : int16_t T_op[2];
93 :
94 : /* set limit_flag to 0 for restrained limits, and 1 for extended limits */
95 64283 : limit_flag = 0;
96 :
97 64283 : pit_start = PIT_MIN;
98 :
99 64283 : if ( st->L_frame == L_FRAME16k )
100 : {
101 34095 : T_op[0] = (int16_t) ( st->pitch[0] * 1.25f + 0.5f );
102 34095 : T_op[1] = (int16_t) ( st->pitch[1] * 1.25f + 0.5f );
103 : }
104 : else
105 : {
106 30188 : T_op[0] = st->pitch[0];
107 30188 : T_op[1] = st->pitch[1];
108 : }
109 :
110 64283 : lp_flag = st->acelp_cfg.ltf_mode;
111 :
112 64283 : if ( i_subfr == 0 )
113 : {
114 14366 : mult_Top = 1;
115 14366 : if ( limit_flag == 0 )
116 : {
117 14366 : if ( st->L_frame == L_FRAME && T_op[1] < PIT_MIN )
118 : {
119 908 : mult_Top = 2;
120 : }
121 :
122 14366 : if ( st->L_frame == L_FRAME16k && T_op[1] < PIT16k_MIN )
123 : {
124 704 : mult_Top = 2;
125 : }
126 : }
127 :
128 14366 : limit_T0( st->L_frame, 8, 0, limit_flag, mult_Top * T_op[1], 0, T0_min, T0_max );
129 : }
130 :
131 : /*-----------------------------------------------------------------*
132 : * zero adaptive excitation signal construction
133 : *-----------------------------------------------------------------*/
134 :
135 64283 : if ( *tc_subfr > i_subfr )
136 : {
137 19158 : *gain_pit = 0.0f;
138 19158 : *clip_gain = 0;
139 19158 : g_corr[0] = 1.0f;
140 19158 : g_corr[1] = 1.0f;
141 :
142 19158 : set_f( &exc[i_subfr], 0, L_SUBFR ); /* set excitation for current subrame to 0 */
143 :
144 19158 : if ( st->L_frame == L_FRAME )
145 : {
146 7746 : set_f( &bwe_exc[i_subfr * HIBND_ACB_L_FAC], 0, (int16_t) ( L_SUBFR * HIBND_ACB_L_FAC ) ); /* set past excitation buffer to 0 */
147 : }
148 : else
149 : {
150 11412 : set_f( &bwe_exc[i_subfr * 2], 0, (int16_t) ( L_SUBFR * 2 ) ); /* set past excitation buffer to 0 */
151 : }
152 :
153 19158 : set_f( y1, 0, L_SUBFR ); /* set filtered adaptive excitation to 0 */
154 19158 : mvr2r( xn, xn2, L_SUBFR ); /* target vector for codebook search */
155 19158 : *T0 = L_SUBFR;
156 19158 : *T0_frac = 0;
157 :
158 19158 : **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f; /* save subframe pitch values */
159 : }
160 :
161 : /*-----------------------------------------------------------------*
162 : * glottal codebook contribution construction
163 : *-----------------------------------------------------------------*/
164 :
165 45125 : else if ( *tc_subfr == i_subfr )
166 : {
167 14366 : if ( st->L_frame == L_FRAME )
168 : {
169 7547 : set_f( bwe_exc - PIT_MAX * HIBND_ACB_L_FAC, 0, PIT_MAX * HIBND_ACB_L_FAC ); /* set past excitation buffer to 0 */
170 : }
171 : else
172 : {
173 6819 : set_f( bwe_exc - PIT16k_MAX * 2, 0, PIT16k_MAX * 2 ); /* set past excitation buffer to 0 */
174 : }
175 :
176 14366 : tc_enc( st, i_subfr, tc_subfr, position, h1, xn, exc, y1, T0_min, T0_max, T0, T0_frac, gain_pit, g_corr, bwe_exc );
177 :
178 14366 : if ( *tc_subfr == TC_0_0 )
179 : {
180 : /* this is called only to compute unused bits */
181 1750 : config_acelp1( ENC, st->total_brate, st->core_brate, st->core, st->extl, st->extl_brate, L_FRAME, -1, &( st->acelp_cfg ), hBstr->nb_bits_tot, TRANSITION, -1, TC_0_0, 3, NULL, unbits_ACELP, st->element_mode, &i /*dummy*/, 0 /*tdm_lp_reuse_flag*/, 0 /*tdm_low_rate_mode*/, st->idchan, st->active_cnt, 0 /*tdm_Pitch_reuse_flag*/, st->tdm_LRTD_flag, 0 /*GSC_IVAS_mode*/ );
182 : }
183 :
184 14366 : *clip_gain = gp_clip( st->element_mode, st->core_brate, st->voicing, i_subfr, TRANSITION, xn, gp_cl );
185 :
186 14366 : updt_tar( xn, xn2, y1, *gain_pit, L_SUBFR );
187 :
188 14366 : **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f; /* save subframe pitch values */
189 14366 : *Jopt_flag = 1;
190 : }
191 :
192 : /*--------------------------------------------------------------*
193 : * other subframes -> GENERIC encoding type,
194 : * standard adaptive excitation contribution
195 : * - exemption only in case when first glottal impulse is
196 : * in the 1st subframe and the second one in 2nd subframe
197 : * and later
198 : *--------------------------------------------------------------*/
199 :
200 30759 : else if ( *tc_subfr < i_subfr )
201 : {
202 30759 : if ( st->L_frame == L_FRAME )
203 : {
204 14895 : *Jopt_flag = 1;
205 :
206 : /* pit_flag for T0 bits number coding determination */
207 14895 : if ( ( ( i_subfr - *tc_subfr ) == L_SUBFR ) || ( ( i_subfr - *tc_subfr ) == L_SUBFR - TC_0_0 ) )
208 : {
209 5726 : pit_flag = 0;
210 : }
211 : else
212 : {
213 9169 : pit_flag = L_SUBFR;
214 : }
215 14895 : if ( *tc_subfr == TC_0_0 )
216 : {
217 5250 : if ( i_subfr == L_SUBFR )
218 : {
219 1750 : limit_T0( L_FRAME, 8, pit_flag, limit_flag, *T0, 0, T0_min, T0_max );
220 : }
221 5250 : pit_flag = 1;
222 : }
223 :
224 : /*----------------------------------------------------------*
225 : * if tc_subfr==0, change tc_subfr corresponding to the
226 : * second glot. impulse position
227 : *----------------------------------------------------------*/
228 :
229 14895 : if ( ( *tc_subfr == 0 ) && ( i_subfr == L_SUBFR ) )
230 : {
231 2255 : if ( PIT_MIN > ( *position ) )
232 : {
233 1425 : pit_start = L_SUBFR - ( *position );
234 : }
235 : else
236 : {
237 830 : pit_start = PIT_MIN;
238 : }
239 2255 : if ( pit_start < PIT_MIN )
240 : {
241 114 : pit_start = PIT_MIN;
242 : }
243 :
244 2255 : pit_limit = 2 * pit_start + ( *position );
245 :
246 : /* Find the closed loop pitch period */
247 2255 : *T0 = pitch_fr4( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, pit_flag, limit_flag, pit_start, pit_limit, L_FRAME, L_SUBFR );
248 :
249 2255 : offset = tbe_celp_exc_offset( *T0, *T0_frac );
250 363055 : for ( i = 0; i < L_SUBFR * HIBND_ACB_L_FAC; i++ )
251 : {
252 360800 : bwe_exc[i + i_subfr * HIBND_ACB_L_FAC] = bwe_exc[i + i_subfr * HIBND_ACB_L_FAC - offset];
253 : }
254 :
255 2255 : if ( ( *T0 ) > ( 2 * L_SUBFR - ( *position ) ) )
256 : {
257 912 : if ( ( *T0 ) + ( *position ) >= 3 * L_SUBFR )
258 : {
259 : /* second glottal impulse is in the 4th subframe */
260 221 : *tc_subfr = TC_0_192;
261 : }
262 : else
263 : {
264 : /* second glottal impulse is in the 3rd subframe */
265 691 : *tc_subfr = TC_0_128;
266 : }
267 : }
268 1343 : else if ( ( *tc_subfr == 0 ) && ( i_subfr == L_SUBFR ) )
269 : {
270 : /* second glottal impulse is in the 2nd subframe */
271 1343 : *tc_subfr = TC_0_64;
272 : }
273 : }
274 :
275 14895 : if ( i_subfr - *tc_subfr <= L_SUBFR )
276 : {
277 5726 : config_acelp1( ENC, st->total_brate, st->core_brate, st->core, st->extl, st->extl_brate, st->L_frame, -1, &( st->acelp_cfg ), hBstr->nb_bits_tot, TRANSITION, -1, *tc_subfr, 2, NULL, unbits_ACELP, st->element_mode, &i /*dummy*/, 0 /*tdm_lp_reuse_flag*/, 0 /*tdm_low_rate_mode*/, st->idchan, st->active_cnt, 0 /*tdm_Pitch_reuse_flag*/, st->tdm_LRTD_flag, 0 /*GSC_IVAS_mode*/ );
278 : }
279 :
280 : /*-----------------------------------------------------------------*
281 : * get number of bits for pitch encoding
282 : *-----------------------------------------------------------------*/
283 :
284 14895 : nBits = st->acelp_cfg.pitch_bits[i_subfr / L_SUBFR];
285 :
286 : /*-----------------------------------------------------------------*
287 : * Find adaptive part of excitation, encode pitch period
288 : *-----------------------------------------------------------------*/
289 :
290 : /* first glottal impulse is in the 1st subrame */
291 14895 : if ( ( i_subfr == L_SUBFR ) && ( *tc_subfr >= TC_0_128 ) )
292 : {
293 : /*--------------------------------------------------------*
294 : * second glottal impulse is in the 3rd or 4th subframe
295 : * - build exc[] in 2nd subframe
296 : *--------------------------------------------------------*/
297 :
298 912 : *T0 = 2 * L_SUBFR;
299 912 : *T0_frac = 0;
300 912 : *Jopt_flag = 0;
301 :
302 912 : set_f( &exc[i_subfr], 0, (int16_t) ( L_SUBFR + 1 ) );
303 912 : set_f( &bwe_exc[i_subfr * HIBND_ACB_L_FAC], 0, (int16_t) ( L_SUBFR * HIBND_ACB_L_FAC ) );
304 : }
305 13983 : else if ( ( i_subfr == L_SUBFR ) && ( *tc_subfr == TC_0_64 ) )
306 : {
307 : /*--------------------------------------------------------*
308 : * second glottal impulse is in the 2nd subframe,
309 : * - build exc[] in 2nd subframe
310 : *--------------------------------------------------------*/
311 :
312 1343 : if ( ( *T0 ) + ( *position ) < L_SUBFR )
313 : {
314 : /* impulse must be in the 2nd subframe (not in 1st) */
315 8 : *T0 = L_SUBFR - ( *position );
316 8 : *T0_frac = 0;
317 : }
318 :
319 1343 : if ( ( *T0 ) + ( *position ) >= 2 * L_SUBFR )
320 : {
321 : /* impulse must be in the 2nd subframe (not in 3rd) */
322 30 : *T0 = 2 * L_SUBFR - 1 - ( *position );
323 30 : *T0_frac = 2;
324 : }
325 :
326 1343 : limit_T0( L_FRAME, 8, pit_flag, limit_flag, *T0, 0, T0_min, T0_max ); /* find T0_min and T0_max for delta search */
327 :
328 : /* 7bit ENCODER */
329 1343 : index = ( *T0 - pit_start ) * 2 + *T0_frac / 2;
330 1343 : push_indice( hBstr, IND_PITCH, index, nBits );
331 :
332 : /* Find the adaptive codebook vector - ACELP long-term prediction */
333 1343 : pred_lt4( &exc[i_subfr], &exc[i_subfr], *T0, *T0_frac, L_SUBFR + 1, inter4_2, L_INTERPOL2, PIT_UP_SAMP );
334 :
335 1343 : offset = tbe_celp_exc_offset( *T0, *T0_frac );
336 216223 : for ( i = 0; i < L_SUBFR * HIBND_ACB_L_FAC; i++ )
337 : {
338 214880 : bwe_exc[i + i_subfr * HIBND_ACB_L_FAC] = bwe_exc[i + i_subfr * HIBND_ACB_L_FAC - offset];
339 : }
340 : }
341 12640 : else if ( ( i_subfr == 2 * L_SUBFR ) && ( *tc_subfr == TC_0_128 ) )
342 : {
343 : /*--------------------------------------------------------*
344 : * second glottal impulse is in the 3rd subframe
345 : * - build exc[] in 3rd subframe
346 : *--------------------------------------------------------*/
347 :
348 691 : pit_start = 2 * L_SUBFR - ( *position );
349 :
350 691 : pit_flag = 0;
351 :
352 691 : *T0 = pitch_fr4( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, pit_flag, limit_flag, pit_start, 3 * L_SUBFR, L_FRAME, L_SUBFR );
353 :
354 691 : if ( ( *T0 ) + ( *position ) < 2 * L_SUBFR )
355 : {
356 : /* impulse must be in the 3rd subframe (not in 2nd) */
357 23 : *T0 = 2 * L_SUBFR - ( *position );
358 23 : *T0_frac = 0;
359 : }
360 691 : if ( ( *T0 ) + ( *position ) >= 3 * L_SUBFR )
361 : {
362 : /* impulse must be in the 3rd subframe (not in 4th) */
363 0 : *T0 = 3 * L_SUBFR - 1 - ( *position );
364 0 : *T0_frac = 2;
365 : }
366 :
367 691 : limit_T0( L_FRAME, 8, pit_flag, limit_flag, *T0, 0, T0_min, T0_max ); /* find T0_min and T0_max for delta search */
368 :
369 691 : index = ( *T0 - pit_start ) * 2 + *T0_frac / 2;
370 691 : push_indice( hBstr, IND_PITCH, index, nBits );
371 :
372 : /* Find the adaptive codebook vector - ACELP long-term prediction */
373 691 : pred_lt4( &exc[i_subfr], &exc[i_subfr], *T0, *T0_frac, L_SUBFR + 1, inter4_2, L_INTERPOL2, PIT_UP_SAMP );
374 :
375 691 : offset = tbe_celp_exc_offset( *T0, *T0_frac );
376 111251 : for ( i = 0; i < L_SUBFR * HIBND_ACB_L_FAC; i++ )
377 : {
378 110560 : bwe_exc[i + i_subfr * HIBND_ACB_L_FAC] = bwe_exc[i + i_subfr * HIBND_ACB_L_FAC - offset];
379 : }
380 : }
381 11949 : else if ( ( i_subfr == 2 * L_SUBFR ) && ( *tc_subfr == TC_0_192 ) )
382 : {
383 : /*--------------------------------------------------------*
384 : * second glottal impulse is in the 4th subframe
385 : * - build exc[] in 3rd subframe
386 : *--------------------------------------------------------*/
387 :
388 221 : *T0 = 4 * L_SUBFR;
389 221 : *T0_frac = 0;
390 221 : *Jopt_flag = 0;
391 :
392 221 : set_f( &exc[i_subfr], 0, (int16_t) ( L_SUBFR + 1 ) );
393 221 : set_f( &bwe_exc[i_subfr * HIBND_ACB_L_FAC], 0, (int16_t) ( L_SUBFR * HIBND_ACB_L_FAC ) );
394 : }
395 11728 : else if ( ( i_subfr == 3 * L_SUBFR ) && ( *tc_subfr == TC_0_192 ) )
396 : {
397 : /*--------------------------------------------------------*
398 : * second glottal impulse is in the 4th subframe
399 : * - build exc[] in 4th subframe
400 : *--------------------------------------------------------*/
401 : /* always T0_frac = 0 */
402 221 : pit_flag = 0;
403 :
404 221 : *T0 = pitch_fr4( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, pit_flag, limit_flag, PIT_MIN, PIT_FR1_8b, L_FRAME, L_SUBFR );
405 :
406 221 : if ( ( *T0 ) + ( *position ) < 3 * L_SUBFR )
407 : {
408 : /* impulse must be in the 4th subframe (not in 3rd) */
409 12 : *T0 = 3 * L_SUBFR - ( *position );
410 12 : *T0_frac = 0;
411 : }
412 :
413 221 : pit_start = 3 * L_SUBFR - ( *position );
414 221 : pit_limit = 2 * L_FRAME - PIT_MAX - 2 - 2 * ( *position );
415 :
416 221 : if ( *T0 < pit_limit )
417 : {
418 168 : index = ( *T0 - pit_start ) * 2 + *T0_frac / 2;
419 : }
420 : else
421 : {
422 53 : index = *T0 - pit_limit + ( pit_limit - pit_start ) * 2;
423 53 : *T0_frac = 0;
424 : }
425 :
426 221 : push_indice( hBstr, IND_PITCH, index, nBits );
427 :
428 : /* Find the adaptive codebook vector - ACELP long-term prediction */
429 221 : pred_lt4( &exc[i_subfr], &exc[i_subfr], *T0, *T0_frac, L_SUBFR + 1, inter4_2, L_INTERPOL2, PIT_UP_SAMP );
430 :
431 221 : offset = tbe_celp_exc_offset( *T0, *T0_frac );
432 35581 : for ( i = 0; i < L_SUBFR * HIBND_ACB_L_FAC; i++ )
433 : {
434 35360 : bwe_exc[i + i_subfr * HIBND_ACB_L_FAC] = bwe_exc[i + i_subfr * HIBND_ACB_L_FAC - offset];
435 : }
436 : }
437 11507 : else if ( ( i_subfr == 3 * L_SUBFR ) && ( *tc_subfr == TC_0_128 ) )
438 : {
439 : /*--------------------------------------------------------*
440 : * second glottal impulse in the 3rd subframe
441 : * build exc[] in 4th subframe
442 : *--------------------------------------------------------*/
443 :
444 691 : pit_flag = L_SUBFR;
445 691 : *T0 = pitch_fr4( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, pit_flag, limit_flag, PIT_MIN, PIT_FR1_8b, L_FRAME, L_SUBFR );
446 691 : index = delta_pit_enc( 2, *T0, *T0_frac, *T0_min );
447 691 : push_indice( hBstr, IND_PITCH, index, nBits );
448 :
449 : /* Find the adaptive codebook vector - ACELP long-term prediction */
450 691 : pred_lt4( &exc[i_subfr], &exc[i_subfr], *T0, *T0_frac, L_SUBFR + 1, inter4_2, L_INTERPOL2, PIT_UP_SAMP );
451 :
452 691 : offset = tbe_celp_exc_offset( *T0, *T0_frac );
453 111251 : for ( i = 0; i < L_SUBFR * HIBND_ACB_L_FAC; i++ )
454 : {
455 110560 : bwe_exc[i + i_subfr * HIBND_ACB_L_FAC] = bwe_exc[i + i_subfr * HIBND_ACB_L_FAC - offset];
456 : }
457 : }
458 :
459 : /*------------------------------------------------------------*
460 : * first glottal impulse is NOT in the 1st subframe,
461 : * or two impulses are in the 1st subframe
462 : *------------------------------------------------------------*/
463 : else
464 : {
465 10816 : if ( nBits == 8 || nBits == 5 )
466 : {
467 1051 : if ( !( ( *tc_subfr == 0 ) && ( i_subfr == L_SUBFR ) ) )
468 : {
469 1051 : *T0 = pitch_fr4( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, pit_flag, limit_flag, PIT_MIN, PIT_FR1_8b, L_FRAME, L_SUBFR );
470 : }
471 : }
472 : else
473 : {
474 9765 : *T0 = pitch_fr4( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, pit_flag, limit_flag, PIT_FR2_9b, PIT_FR1_9b, L_FRAME, L_SUBFR );
475 : }
476 :
477 10816 : pit_Q_enc( hBstr, 0, nBits, 8, pit_flag, limit_flag, *T0, *T0_frac, T0_min, T0_max );
478 :
479 : /* Find the adaptive codebook vector - ACELP long-term prediction */
480 10816 : pred_lt4( &exc[i_subfr], &exc[i_subfr], *T0, *T0_frac, L_SUBFR + 1, inter4_2, L_INTERPOL2, PIT_UP_SAMP );
481 :
482 10816 : offset = tbe_celp_exc_offset( *T0, *T0_frac );
483 1741376 : for ( i = 0; i < L_SUBFR * HIBND_ACB_L_FAC; i++ )
484 : {
485 1730560 : bwe_exc[i + i_subfr * HIBND_ACB_L_FAC] = bwe_exc[i + i_subfr * HIBND_ACB_L_FAC - offset];
486 : }
487 : }
488 :
489 14895 : if ( *Jopt_flag == 0 )
490 : {
491 : /* adaptive/TC excitation is zero */
492 1133 : mvr2r( xn, xn2, L_SUBFR );
493 1133 : g_corr[0] = 0.0f;
494 1133 : g_corr[1] = 0.0f;
495 1133 : *clip_gain = 0;
496 : }
497 : else
498 : {
499 13762 : *clip_gain = gp_clip( st->element_mode, st->core_brate, st->voicing, i_subfr, TRANSITION, xn, gp_cl );
500 :
501 13762 : lp_select = lp_filt_exc_enc( MODE1, TRANSITION, i_subfr, exc, h1, xn, y1, xn2, L_SUBFR, st->L_frame, g_corr, *clip_gain, gain_pit, &lp_flag );
502 :
503 13762 : if ( lp_flag == NORMAL_OPERATION )
504 : {
505 10841 : push_indice( hBstr, IND_LP_FILT_SELECT, lp_select, 1 );
506 : }
507 : }
508 :
509 14895 : **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f; /* save subframe pitch values */
510 :
511 : /*---------------------------------------------------------------------*
512 : * fill the pitch buffer - needed for post-processing
513 : *---------------------------------------------------------------------*/
514 :
515 14895 : if ( ( *tc_subfr >= 2 * L_SUBFR ) && ( i_subfr == 3 * L_SUBFR ) )
516 : {
517 562 : ( *pt_pitch ) -= 3;
518 562 : **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
519 562 : ( *pt_pitch )++;
520 562 : **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
521 562 : ( *pt_pitch )++;
522 562 : **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
523 562 : ( *pt_pitch )++;
524 : }
525 14333 : else if ( ( *tc_subfr == L_SUBFR ) && ( i_subfr == 2 * L_SUBFR ) )
526 : {
527 1159 : ( *pt_pitch ) -= 2;
528 1159 : **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
529 1159 : ( *pt_pitch )++;
530 1159 : **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
531 1159 : ( *pt_pitch )++;
532 : }
533 13174 : else if ( ( *tc_subfr == TC_0_64 ) && ( i_subfr == L_SUBFR ) )
534 : {
535 1343 : ( *pt_pitch ) -= 1;
536 1343 : **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
537 1343 : ( *pt_pitch )++;
538 : }
539 11831 : else if ( ( *tc_subfr == TC_0_128 ) && ( i_subfr == 2 * L_SUBFR ) )
540 : {
541 691 : ( *pt_pitch ) -= 2;
542 691 : **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
543 691 : ( *pt_pitch )++;
544 691 : **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
545 691 : ( *pt_pitch )++;
546 : }
547 11140 : else if ( ( *tc_subfr == TC_0_192 ) && ( i_subfr == 3 * L_SUBFR ) )
548 : {
549 221 : ( *pt_pitch ) -= 3;
550 221 : **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
551 221 : ( *pt_pitch )++;
552 221 : **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
553 221 : ( *pt_pitch )++;
554 221 : **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
555 221 : ( *pt_pitch )++;
556 : }
557 : }
558 : else /* L_frame == L_FRAME16k */
559 : {
560 15864 : if ( i_subfr >= 2 * L_SUBFR )
561 : {
562 13060 : limit_flag = 1;
563 : }
564 :
565 15864 : if ( i_subfr <= 2 * L_SUBFR )
566 : {
567 6627 : if ( i_subfr < 2 * L_SUBFR )
568 : {
569 2804 : mult_Top = 1;
570 2804 : if ( T_op[0] < PIT16k_MIN )
571 : {
572 165 : mult_Top = 2;
573 : }
574 2804 : limit_T0( L_FRAME16k, 8, 0, limit_flag, mult_Top * T_op[0], 0, T0_min, T0_max ); /* TC0 second subfr. */
575 : }
576 : else
577 : {
578 3823 : limit_T0( L_FRAME16k, 8, 0, limit_flag, T_op[1], 0, T0_min, T0_max ); /* TC0 third subfr., or TC64 third subfr. */
579 : }
580 : }
581 :
582 : /*-----------------------------------------------------------------*
583 : * get number of bits for pitch encoding
584 : *-----------------------------------------------------------------*/
585 :
586 15864 : nBits = st->acelp_cfg.pitch_bits[i_subfr / L_SUBFR];
587 :
588 : /*-----------------------------------------------------------------*
589 : * Find adaptive part of excitation, encode pitch period
590 : *-----------------------------------------------------------------*/
591 :
592 15864 : if ( nBits == 10 )
593 : {
594 3823 : *T0 = pitch_fr4( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, 0, limit_flag, PIT16k_FR2_EXTEND_10b, PIT16k_MAX, st->L_frame, L_SUBFR );
595 3823 : pit16k_Q_enc( hBstr, nBits, limit_flag, *T0, *T0_frac, T0_min, T0_max );
596 : }
597 12041 : else if ( nBits == 8 ) /* tc_subfr==0 && i_subfr==L_SUBFR */
598 : {
599 : /*-----------------------------------------------------------------------------*
600 : * The pitch range is encoded absolutely with 8 bits and is divided as follows:
601 : * PIT16k_MIN to PIT16k_FR2_TC0_2SUBFR-1 resolution 1/4 (frac = 0,1,2 or 3)
602 : * PIT16k_FR2_TC0_2SUBFR to 2*L_SUBFR resolution 1/2 (frac = 0 or 2)
603 : *-----------------------------------------------------------------------------*/
604 :
605 2804 : *T0 = pitch_fr4( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, 0, limit_flag, PIT16k_FR2_TC0_2SUBFR, 2 * L_SUBFR, st->L_frame, L_SUBFR );
606 :
607 2804 : if ( *T0_max > 2 * L_SUBFR )
608 : {
609 463 : *T0 = 2 * L_SUBFR;
610 463 : *T0_frac = 0;
611 : }
612 :
613 2804 : if ( *T0 < PIT16k_FR2_TC0_2SUBFR )
614 : {
615 1620 : index = ( *T0 ) * 4 + ( *T0_frac ) - ( PIT16k_MIN * 4 );
616 : }
617 : else
618 : {
619 1184 : index = ( *T0 ) * 2 + ( ( *T0_frac ) >> 1 ) - ( PIT16k_FR2_TC0_2SUBFR * 2 ) + ( ( PIT16k_FR2_TC0_2SUBFR - PIT16k_MIN ) * 4 );
620 : }
621 :
622 2804 : push_indice( hBstr, IND_PITCH, index, nBits );
623 : }
624 9237 : else if ( nBits == 6 )
625 : {
626 : /* delta search */
627 9237 : *T0 = pitch_fr4( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, L_SUBFR, limit_flag, PIT16k_FR2_EXTEND_9b, PIT16k_FR1_EXTEND_9b, st->L_frame, L_SUBFR );
628 :
629 9237 : index = delta_pit_enc( 4, *T0, *T0_frac, *T0_min );
630 :
631 9237 : push_indice( hBstr, IND_PITCH, index, nBits );
632 : }
633 15864 : if ( nBits == 6 )
634 : {
635 9237 : limit_T0( L_FRAME16k, 8, L_SUBFR, limit_flag, *T0, *T0_frac, T0_min, T0_max );
636 : }
637 :
638 : /*-----------------------------------------------------------------*
639 : * - gain clipping test to avoid unstable synthesis
640 : * - LP filtering of the adaptive excitation
641 : * - codebook target computation
642 : *-----------------------------------------------------------------*/
643 :
644 15864 : if ( ( i_subfr == L_SUBFR ) && ( *T0 == 2 * L_SUBFR ) )
645 : {
646 463 : *gain_pit = 0.0f;
647 463 : *clip_gain = 0;
648 463 : g_corr[0] = 0.01f;
649 463 : g_corr[1] = 0.01f;
650 463 : *Jopt_flag = 0;
651 :
652 463 : set_f( &exc[i_subfr], 0, L_SUBFR + 1 ); /* set excitation for current subrame to 0 */
653 :
654 463 : push_indice( hBstr, IND_LP_FILT_SELECT, 0, 1 ); /* this bit is actually not needed */
655 :
656 463 : mvr2r( xn, xn2, L_SUBFR ); /* target vector for codebook search */
657 463 : set_f( y1, 0, L_SUBFR ); /* set filtered adaptive excitation to 0 */
658 463 : set_f( &bwe_exc[i_subfr * 2], 0, L_SUBFR * 2 );
659 : }
660 : else
661 : {
662 : /* Find the adaptive codebook vector - ACELP long-term prediction */
663 15401 : pred_lt4( &exc[i_subfr], &exc[i_subfr], *T0, *T0_frac, L_SUBFR + 1, inter4_2, L_INTERPOL2, PIT_UP_SAMP );
664 :
665 1986729 : for ( i = 0; i < L_SUBFR * 2; i++ )
666 : {
667 1971328 : bwe_exc[i + i_subfr * 2] = bwe_exc[i + i_subfr * 2 - *T0 * 2 - (int16_t) ( (float) *T0_frac * 0.5f + 4 + 0.5f ) + 4];
668 : }
669 :
670 15401 : *clip_gain = gp_clip( st->element_mode, st->core_brate, st->voicing, i_subfr, TRANSITION, xn, gp_cl );
671 :
672 15401 : lp_select = lp_filt_exc_enc( MODE1, TRANSITION, i_subfr, exc, h1, xn, y1, xn2, L_SUBFR, st->L_frame, g_corr, *clip_gain, gain_pit, &lp_flag );
673 :
674 15401 : if ( lp_flag == NORMAL_OPERATION )
675 : {
676 15401 : push_indice( hBstr, IND_LP_FILT_SELECT, lp_select, 1 );
677 : }
678 :
679 15401 : *Jopt_flag = 1;
680 : }
681 :
682 15864 : **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f; /* save subframe pitch value */
683 :
684 : /*---------------------------------------------------------------------*
685 : * fill the pitch buffer - needed for post-processing
686 : *---------------------------------------------------------------------*/
687 :
688 15864 : if ( ( i_subfr - *tc_subfr == L_SUBFR ) || ( *tc_subfr == 0 && i_subfr == 2 * L_SUBFR ) )
689 : {
690 7547 : index = i_subfr / L_SUBFR;
691 7547 : ( *pt_pitch ) -= index;
692 :
693 21006 : for ( i = 0; i < index; i++ )
694 : {
695 13459 : **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
696 13459 : ( *pt_pitch )++;
697 : }
698 : }
699 : }
700 : }
701 :
702 64283 : return;
703 : }
704 :
705 :
706 : /*-------------------------------------------------------------------------------------------*
707 : * tc_enc()
708 : *
709 : * Principal function for transition coding (TC) in encoder.
710 : * Glottal codebook contribution part:
711 : *
712 : * |----| |----| xn
713 : * imp_pos->|| | imp_shape->| g1 | |
714 : * | | | | g2 | ---- exc |---| y1 ---- |
715 : * | | |-------------| |----|gain|-------| h |------|gain|----(-)---> xn2
716 : * | || | gn | ---- |---| ----
717 : * |----| |----|
718 : * codebook excitation gain_trans h_orig gain_pit
719 : *
720 : *-------------------------------------------------------------------------------------------*/
721 :
722 14366 : static void tc_enc(
723 : Encoder_State *st, /* i/o: encoder state structure */
724 : const int16_t i_subfr, /* i : subrame index */
725 : int16_t *tc_subfr, /* i/o: TC subframe index */
726 : int16_t *position, /* i/o: index of the residual signal maximum */
727 : const float *h1, /* i : weighted filter input response */
728 : const float *xn, /* i : target signal */
729 : float *exc, /* o : glottal codebook contribution */
730 : float *y1, /* o : filtered glottal codebook contribution */
731 : int16_t *T0_min, /* o : lower pitch limit */
732 : int16_t *T0_max, /* o : higher pitch limit */
733 : int16_t *T0, /* o : close loop integer pitch */
734 : int16_t *T0_frac, /* o : close loop fractional part of the pitch */
735 : float *gain_pit, /* o : pitch gain (0..GAIN_PIT_MAX) */
736 : float g_corr[], /* o : correlations <y1,y1> and -2<xn,y1> */
737 : float *bwe_exc /* i/o: excitation for SWB TBE */
738 : )
739 : {
740 : int16_t imp_shape, imp_pos, imp_sign, imp_gain, index, nBits;
741 : float gain_trans;
742 14366 : BSTR_ENC_HANDLE hBstr = st->hBstr;
743 :
744 14366 : imp_pos = *position - i_subfr;
745 :
746 : /*-----------------------------------------------------------------*
747 : * get number of bits for pitch encoding
748 : *-----------------------------------------------------------------*/
749 :
750 14366 : nBits = st->acelp_cfg.pitch_bits[i_subfr / L_SUBFR];
751 :
752 : /*--------------------------------------------------------------*
753 : * Closed loop pitch search
754 : *--------------------------------------------------------------*/
755 :
756 14366 : *T0_frac = 0;
757 14366 : if ( st->L_frame == L_FRAME )
758 : {
759 7547 : if ( ( *T0_min <= L_SUBFR ) || ( *tc_subfr == 3 * L_SUBFR ) )
760 : {
761 4622 : if ( nBits == 9 )
762 : {
763 1805 : *T0 = pitch_fr4( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, 0, 0, PIT_FR2_9b, PIT_FR1_9b, L_FRAME, L_SUBFR );
764 : }
765 2817 : else if ( nBits == 6 )
766 : {
767 2252 : *T0 = pitch_fr4( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, 0, 0, PIT_MIN, L_SUBFR, L_FRAME, L_SUBFR );
768 : }
769 : else
770 : {
771 565 : *T0 = pitch_fr4( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, 0, 0, PIT_MAX, PIT_MIN, L_FRAME, L_SUBFR );
772 : }
773 : }
774 : else
775 : {
776 2925 : *T0 = L_SUBFR;
777 : }
778 :
779 7547 : if ( ( *tc_subfr == L_SUBFR ) && ( *T0 < L_SUBFR ) )
780 : {
781 258 : *T0 = L_SUBFR;
782 : }
783 : }
784 : else /* L_frame == L_FRAME16k */
785 : {
786 6819 : if ( nBits == 10 )
787 : {
788 2996 : *T0 = pitch_fr4( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, 0, 1, PIT16k_FR2_EXTEND_10b, PIT16k_MAX, L_FRAME16k, L_SUBFR );
789 : }
790 3823 : else if ( nBits == 6 )
791 : {
792 : /* T0_frac with 1/2 sample resolution */
793 3823 : *T0 = pitch_fr4( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, 0, 0, PIT16k_MIN, L_SUBFR, L_FRAME16k, L_SUBFR );
794 :
795 3823 : if ( *T0 > L_SUBFR )
796 : {
797 3080 : *T0 = L_SUBFR;
798 3080 : *T0_frac = 0;
799 : }
800 : }
801 : }
802 :
803 : /* set tc_subfr to TC_0_0 */
804 14366 : if ( i_subfr == 0 && st->L_frame == L_FRAME && ( *T0 < L_SUBFR || *tc_subfr == 3 * L_SUBFR ) )
805 : {
806 1750 : *tc_subfr = TC_0_0;
807 : }
808 :
809 : /*--------------------------------------------------------------*
810 : * Builds glottal codebook contribution
811 : *--------------------------------------------------------------*/
812 :
813 14366 : set_impulse( xn, h1, &exc[i_subfr], y1, &imp_shape, &imp_pos, &gain_trans );
814 :
815 : /*--------------------------------------------------------------*
816 : * quantize gain_trans and scale glottal codebook contribution
817 : *--------------------------------------------------------------*/
818 :
819 14366 : gain_trans_enc( &gain_trans, &exc[i_subfr], &imp_gain, &imp_sign );
820 :
821 : /* set past excitation buffer to zeros */
822 14366 : set_f( exc - L_EXC_MEM, 0, L_EXC_MEM );
823 :
824 : /*--------------------------------------------------------------*
825 : * adapt. search of the second impulse in the same subframe
826 : * (when appears)
827 : *--------------------------------------------------------------*/
828 :
829 14366 : pred_lt4_tc( exc, *T0, *T0_frac, inter4_2, imp_pos, i_subfr );
830 :
831 14366 : if ( st->hBWE_TD != NULL )
832 : {
833 14366 : if ( st->L_frame == L_FRAME )
834 : {
835 7547 : interp_code_5over2( &exc[i_subfr], &bwe_exc[i_subfr * HIBND_ACB_L_FAC], L_SUBFR );
836 : }
837 : else
838 : {
839 6819 : interp_code_4over2( &exc[i_subfr], &bwe_exc[i_subfr * 2], L_SUBFR );
840 : }
841 : }
842 :
843 : /*--------------------------------------------------------------*
844 : * compute glottal-shape codebook excitation
845 : *--------------------------------------------------------------*/
846 :
847 : /* create filtered glottal codebook contribution */
848 14366 : conv( &exc[i_subfr], h1, y1, L_SUBFR );
849 :
850 : /* gain_pit computation */
851 14366 : *gain_pit = corr_xy1( xn, y1, g_corr, L_SUBFR, 0 );
852 :
853 : /*--------------------------------------------------------------*
854 : * Encode parameters and write indices
855 : *--------------------------------------------------------------*/
856 :
857 14366 : if ( st->L_frame == L_FRAME )
858 : {
859 7547 : if ( ( ( i_subfr != 0 ) || ( *tc_subfr == TC_0_0 ) ) && ( *tc_subfr != L_SUBFR ) )
860 : {
861 : /* write pitch index */
862 4133 : if ( ( *T0 >= L_SUBFR ) && ( *tc_subfr != 3 * L_SUBFR ) )
863 : {
864 367 : push_indice( hBstr, IND_PITCH, 0, nBits );
865 : }
866 3766 : else if ( *tc_subfr == 3 * L_SUBFR )
867 : {
868 1821 : if ( nBits == 9 )
869 : {
870 1805 : index = abs_pit_enc( 4, 0, *T0, *T0_frac );
871 : }
872 : else
873 : {
874 16 : index = abs_pit_enc( 2, 0, *T0, *T0_frac );
875 : }
876 :
877 1821 : push_indice( hBstr, IND_PITCH, index, nBits );
878 :
879 1821 : limit_T0( L_FRAME, 8, 0, 0, *T0, 0, T0_min, T0_max );
880 : }
881 : else
882 : {
883 1945 : if ( nBits == 6 )
884 : {
885 1750 : index = delta_pit_enc( 2, *T0, *T0_frac, PIT_MIN - 1 );
886 1750 : push_indice( hBstr, IND_PITCH, index, nBits );
887 : }
888 : else
889 : {
890 195 : index = delta_pit_enc( 0, *T0, *T0_frac, PIT_MIN - 1 );
891 195 : push_indice( hBstr, IND_PITCH, index, nBits );
892 : }
893 : }
894 : }
895 : }
896 : else /* L_frame == L_FRAME16k */
897 : {
898 6819 : if ( nBits == 10 )
899 : {
900 2996 : pit16k_Q_enc( hBstr, nBits, 1, *T0, *T0_frac, T0_min, T0_max );
901 : }
902 3823 : else if ( nBits == 6 )
903 : {
904 3823 : index = 2 * ( *T0 - PIT16k_MIN ) + *T0_frac / 2;
905 3823 : push_indice( hBstr, IND_PITCH, index, nBits );
906 : }
907 : }
908 :
909 14366 : push_indice( hBstr, IND_TC_IMP_SHAPE, imp_shape, 3 );
910 14366 : push_indice( hBstr, IND_TC_IMP_POS, imp_pos, 6 );
911 14366 : push_indice( hBstr, IND_TC_IMP_SIGN, imp_sign, 1 );
912 14366 : push_indice( hBstr, IND_TC_IMP_GAIN, imp_gain, 3 );
913 :
914 14366 : *position = imp_pos + i_subfr;
915 :
916 14366 : return;
917 : }
918 :
919 : /*-----------------------------------------------------------------*
920 : * gain_trans_enc()
921 : *
922 : * Quantize gain_trans of TC (gains of glottal impulses).
923 : * - Uses scalar quantization prototypes tbl_gain_trans_tc[N_GAIN_TC].
924 : * - Gains the glottal codebook contibution signal.
925 : *-----------------------------------------------------------------*/
926 :
927 14366 : static void gain_trans_enc(
928 : float *gain_trans, /* i/o: gain for TC */
929 : float exc[], /* i/o: glottal codebook contribution */
930 : int16_t *quant_index, /* o : index of quantized gain_trans */
931 : int16_t *quant_sign /* o : sign of quantized gain_trans */
932 : )
933 : {
934 : int16_t i;
935 :
936 :
937 14366 : if ( *gain_trans < 0 )
938 : {
939 7202 : *gain_trans *= -1;
940 7202 : *quant_sign = 0;
941 : }
942 : else
943 : {
944 7164 : *quant_sign = 1;
945 : }
946 :
947 14366 : *quant_index = N_GAIN_TC - 1;
948 :
949 67520 : for ( i = 0; i < N_GAIN_TC - 1; i++ )
950 : {
951 65477 : if ( *gain_trans < tbl_gain_trans_tc[i] )
952 : {
953 12323 : *quant_index = i;
954 12323 : break;
955 : }
956 : }
957 :
958 : /* restore gain_trans */
959 14366 : *gain_trans = tbl_gain_trans_tc[i];
960 :
961 14366 : if ( *quant_sign == 0 )
962 : {
963 7202 : *gain_trans *= -1;
964 : }
965 :
966 933790 : for ( i = 0; i < L_SUBFR; i++ )
967 : {
968 919424 : exc[i] *= ( *gain_trans );
969 : }
970 :
971 14366 : return;
972 : }
973 :
974 :
975 : /*-----------------------------------------------------------------*
976 : * tc_classif_enc()
977 : *
978 : * TC subframe determination
979 : *-----------------------------------------------------------------*/
980 :
981 14366 : void tc_classif_enc(
982 : const int16_t L_frame, /* i : length of the frame */
983 : int16_t *tc_subfr, /* o : TC subframe index */
984 : int16_t *position, /* o : maximum of residual signal index */
985 : const int16_t attack_flag, /* i : attack flag */
986 : const int16_t pitch, /* i : open loop pitch estimates for first halfframe */
987 : const float *res /* i : pointer to the LP residual signal frame */
988 : )
989 : {
990 : float temp;
991 : int16_t T_op;
992 :
993 14366 : T_op = pitch;
994 14366 : if ( L_frame == L_FRAME16k )
995 : {
996 6819 : T_op = (int16_t) ( pitch * 1.25f + 0.5f );
997 : }
998 :
999 14366 : *tc_subfr = -1;
1000 14366 : if ( attack_flag )
1001 : {
1002 5285 : *tc_subfr = 3 * L_SUBFR;
1003 :
1004 5285 : if ( attack_flag > 0 )
1005 : {
1006 5285 : if ( L_frame == L_FRAME )
1007 : {
1008 2515 : *tc_subfr = NB_SUBFR * ( attack_flag - 1 ) / 32 /*ATT_NSEG*/;
1009 : }
1010 : else
1011 : {
1012 2770 : *tc_subfr = NB_SUBFR16k * ( attack_flag - 1 ) / 32 /*ATT_NSEG*/;
1013 : }
1014 5285 : *tc_subfr *= L_SUBFR;
1015 : }
1016 : }
1017 :
1018 14366 : if ( attack_flag )
1019 : {
1020 : /*-----------------------------------------------------------------*
1021 : * TC: subrame determination for glottal shape search
1022 : * -------------------------------------------------------
1023 : * tc_subfr == 0 - TC in 1st subframe
1024 : * tc_subfr == TC_0_0 - TC in 1st subframe + information about T0
1025 : * tc_subfr == L_SUBFR - TC in 2nd subframe
1026 : * tc_subfr == 2*L_SUBFR - TC in 3rd subframe
1027 : * tc_subfr == 3*L_SUBFR - TC in 4th subframe
1028 : *-----------------------------------------------------------------*/
1029 :
1030 5285 : *position = emaximum( res + *tc_subfr, min( T_op + 2, L_SUBFR ), &temp ) + *tc_subfr;
1031 : }
1032 : else
1033 : {
1034 9081 : *position = emaximum( res, (int16_t) ( T_op + 2 ), &temp );
1035 :
1036 : /* correction in case of possibly wrong T_op (double-pitch values) */
1037 9081 : if ( ( L_frame == L_FRAME && T_op > 2 * PIT_MIN ) ||
1038 4049 : ( L_frame == L_FRAME16k && T_op > 2 * PIT16k_MIN ) )
1039 : {
1040 : int16_t position_tmp, len;
1041 : float aver, temp2;
1042 :
1043 4947 : len = (int16_t) ( T_op / 2 + 2 );
1044 :
1045 4947 : position_tmp = emaximum( res, len, &temp2 );
1046 4947 : aver = dotp( res, res, len ) + 0.01f;
1047 4947 : aver = sqrtf( aver / len );
1048 :
1049 4947 : temp = sqrtf( temp );
1050 4947 : temp2 = sqrtf( temp2 );
1051 :
1052 4947 : if ( temp2 > 0.8f * temp && aver < 0.25f * temp )
1053 : {
1054 353 : *position = position_tmp;
1055 : }
1056 : }
1057 :
1058 9081 : *tc_subfr = (int16_t) floor( ( *position ) / L_SUBFR ) * L_SUBFR;
1059 : }
1060 :
1061 14366 : return;
1062 : }
|