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 68367 : 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 68367 : 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 68367 : limit_flag = 0;
96 :
97 68367 : pit_start = PIT_MIN;
98 :
99 68367 : if ( st->L_frame == L_FRAME16k )
100 : {
101 35075 : T_op[0] = (int16_t) ( st->pitch[0] * 1.25f + 0.5f );
102 35075 : T_op[1] = (int16_t) ( st->pitch[1] * 1.25f + 0.5f );
103 : }
104 : else
105 : {
106 33292 : T_op[0] = st->pitch[0];
107 33292 : T_op[1] = st->pitch[1];
108 : }
109 :
110 68367 : lp_flag = st->acelp_cfg.ltf_mode;
111 :
112 68367 : if ( i_subfr == 0 )
113 : {
114 15338 : mult_Top = 1;
115 15338 : if ( limit_flag == 0 )
116 : {
117 15338 : if ( st->L_frame == L_FRAME && T_op[1] < PIT_MIN )
118 : {
119 1211 : mult_Top = 2;
120 : }
121 :
122 15338 : if ( st->L_frame == L_FRAME16k && T_op[1] < PIT16k_MIN )
123 : {
124 816 : mult_Top = 2;
125 : }
126 : }
127 :
128 15338 : 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 68367 : if ( *tc_subfr > i_subfr )
136 : {
137 19261 : *gain_pit = 0.0f;
138 19261 : *clip_gain = 0;
139 19261 : g_corr[0] = 1.0f;
140 19261 : g_corr[1] = 1.0f;
141 :
142 19261 : set_f( &exc[i_subfr], 0, L_SUBFR ); /* set excitation for current subrame to 0 */
143 :
144 19261 : if ( st->L_frame == L_FRAME )
145 : {
146 8245 : 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 11016 : set_f( &bwe_exc[i_subfr * 2], 0, (int16_t) ( L_SUBFR * 2 ) ); /* set past excitation buffer to 0 */
151 : }
152 :
153 19261 : set_f( y1, 0, L_SUBFR ); /* set filtered adaptive excitation to 0 */
154 19261 : mvr2r( xn, xn2, L_SUBFR ); /* target vector for codebook search */
155 19261 : *T0 = L_SUBFR;
156 19261 : *T0_frac = 0;
157 :
158 19261 : **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f; /* save subframe pitch values */
159 : }
160 :
161 : /*-----------------------------------------------------------------*
162 : * glottal codebook contribution construction
163 : *-----------------------------------------------------------------*/
164 :
165 49106 : else if ( *tc_subfr == i_subfr )
166 : {
167 15338 : if ( st->L_frame == L_FRAME )
168 : {
169 8323 : 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 7015 : set_f( bwe_exc - PIT16k_MAX * 2, 0, PIT16k_MAX * 2 ); /* set past excitation buffer to 0 */
174 : }
175 :
176 15338 : 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 15338 : if ( *tc_subfr == TC_0_0 )
179 : {
180 : /* this is called only to compute unused bits */
181 2378 : 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, 0 /*tdm_Pitch_reuse_flag*/, st->tdm_LRTD_flag, 0 /*GSC_IVAS_mode*/ );
182 : }
183 :
184 15338 : *clip_gain = gp_clip( st->element_mode, st->core_brate, st->voicing, i_subfr, TRANSITION, xn, gp_cl );
185 :
186 15338 : updt_tar( xn, xn2, y1, *gain_pit, L_SUBFR );
187 :
188 15338 : **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f; /* save subframe pitch values */
189 15338 : *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 33768 : else if ( *tc_subfr < i_subfr )
201 : {
202 33768 : if ( st->L_frame == L_FRAME )
203 : {
204 16724 : *Jopt_flag = 1;
205 :
206 : /* pit_flag for T0 bits number coding determination */
207 16724 : if ( ( ( i_subfr - *tc_subfr ) == L_SUBFR ) || ( ( i_subfr - *tc_subfr ) == L_SUBFR - TC_0_0 ) )
208 : {
209 6412 : pit_flag = 0;
210 : }
211 : else
212 : {
213 10312 : pit_flag = L_SUBFR;
214 : }
215 16724 : if ( *tc_subfr == TC_0_0 )
216 : {
217 7134 : if ( i_subfr == L_SUBFR )
218 : {
219 2378 : limit_T0( L_FRAME, 8, pit_flag, limit_flag, *T0, 0, T0_min, T0_max );
220 : }
221 7134 : 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 16724 : if ( ( *tc_subfr == 0 ) && ( i_subfr == L_SUBFR ) )
230 : {
231 2163 : if ( PIT_MIN > ( *position ) )
232 : {
233 1382 : pit_start = L_SUBFR - ( *position );
234 : }
235 : else
236 : {
237 781 : pit_start = PIT_MIN;
238 : }
239 2163 : if ( pit_start < PIT_MIN )
240 : {
241 88 : pit_start = PIT_MIN;
242 : }
243 :
244 2163 : pit_limit = 2 * pit_start + ( *position );
245 :
246 : /* Find the closed loop pitch period */
247 2163 : *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 2163 : offset = tbe_celp_exc_offset( *T0, *T0_frac );
250 348243 : for ( i = 0; i < L_SUBFR * HIBND_ACB_L_FAC; i++ )
251 : {
252 346080 : bwe_exc[i + i_subfr * HIBND_ACB_L_FAC] = bwe_exc[i + i_subfr * HIBND_ACB_L_FAC - offset];
253 : }
254 :
255 2163 : if ( ( *T0 ) > ( 2 * L_SUBFR - ( *position ) ) )
256 : {
257 1020 : if ( ( *T0 ) + ( *position ) >= 3 * L_SUBFR )
258 : {
259 : /* second glottal impulse is in the 4th subframe */
260 315 : *tc_subfr = TC_0_192;
261 : }
262 : else
263 : {
264 : /* second glottal impulse is in the 3rd subframe */
265 705 : *tc_subfr = TC_0_128;
266 : }
267 : }
268 1143 : else if ( ( *tc_subfr == 0 ) && ( i_subfr == L_SUBFR ) )
269 : {
270 : /* second glottal impulse is in the 2nd subframe */
271 1143 : *tc_subfr = TC_0_64;
272 : }
273 : }
274 :
275 16724 : if ( i_subfr - *tc_subfr <= L_SUBFR )
276 : {
277 6412 : 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, 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 16724 : 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 16724 : 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 1020 : *T0 = 2 * L_SUBFR;
299 1020 : *T0_frac = 0;
300 1020 : *Jopt_flag = 0;
301 :
302 1020 : set_f( &exc[i_subfr], 0, (int16_t) ( L_SUBFR + 1 ) );
303 1020 : set_f( &bwe_exc[i_subfr * HIBND_ACB_L_FAC], 0, (int16_t) ( L_SUBFR * HIBND_ACB_L_FAC ) );
304 : }
305 15704 : 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 1143 : if ( ( *T0 ) + ( *position ) < L_SUBFR )
313 : {
314 : /* impulse must be in the 2nd subframe (not in 1st) */
315 12 : *T0 = L_SUBFR - ( *position );
316 12 : *T0_frac = 0;
317 : }
318 :
319 1143 : if ( ( *T0 ) + ( *position ) >= 2 * L_SUBFR )
320 : {
321 : /* impulse must be in the 2nd subframe (not in 3rd) */
322 19 : *T0 = 2 * L_SUBFR - 1 - ( *position );
323 19 : *T0_frac = 2;
324 : }
325 :
326 1143 : 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 1143 : index = ( *T0 - pit_start ) * 2 + *T0_frac / 2;
330 1143 : push_indice( hBstr, IND_PITCH, index, nBits );
331 :
332 : /* Find the adaptive codebook vector - ACELP long-term prediction */
333 1143 : pred_lt4( &exc[i_subfr], &exc[i_subfr], *T0, *T0_frac, L_SUBFR + 1, inter4_2, L_INTERPOL2, PIT_UP_SAMP );
334 :
335 1143 : offset = tbe_celp_exc_offset( *T0, *T0_frac );
336 184023 : for ( i = 0; i < L_SUBFR * HIBND_ACB_L_FAC; i++ )
337 : {
338 182880 : bwe_exc[i + i_subfr * HIBND_ACB_L_FAC] = bwe_exc[i + i_subfr * HIBND_ACB_L_FAC - offset];
339 : }
340 : }
341 14561 : 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 705 : pit_start = 2 * L_SUBFR - ( *position );
349 :
350 705 : pit_flag = 0;
351 :
352 705 : *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 705 : if ( ( *T0 ) + ( *position ) < 2 * L_SUBFR )
355 : {
356 : /* impulse must be in the 3rd subframe (not in 2nd) */
357 20 : *T0 = 2 * L_SUBFR - ( *position );
358 20 : *T0_frac = 0;
359 : }
360 705 : 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 705 : 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 705 : index = ( *T0 - pit_start ) * 2 + *T0_frac / 2;
370 705 : push_indice( hBstr, IND_PITCH, index, nBits );
371 :
372 : /* Find the adaptive codebook vector - ACELP long-term prediction */
373 705 : pred_lt4( &exc[i_subfr], &exc[i_subfr], *T0, *T0_frac, L_SUBFR + 1, inter4_2, L_INTERPOL2, PIT_UP_SAMP );
374 :
375 705 : offset = tbe_celp_exc_offset( *T0, *T0_frac );
376 113505 : for ( i = 0; i < L_SUBFR * HIBND_ACB_L_FAC; i++ )
377 : {
378 112800 : bwe_exc[i + i_subfr * HIBND_ACB_L_FAC] = bwe_exc[i + i_subfr * HIBND_ACB_L_FAC - offset];
379 : }
380 : }
381 13856 : 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 315 : *T0 = 4 * L_SUBFR;
389 315 : *T0_frac = 0;
390 315 : *Jopt_flag = 0;
391 :
392 315 : set_f( &exc[i_subfr], 0, (int16_t) ( L_SUBFR + 1 ) );
393 315 : set_f( &bwe_exc[i_subfr * HIBND_ACB_L_FAC], 0, (int16_t) ( L_SUBFR * HIBND_ACB_L_FAC ) );
394 : }
395 13541 : 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 315 : pit_flag = 0;
403 :
404 315 : *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 315 : if ( ( *T0 ) + ( *position ) < 3 * L_SUBFR )
407 : {
408 : /* impulse must be in the 4th subframe (not in 3rd) */
409 33 : *T0 = 3 * L_SUBFR - ( *position );
410 33 : *T0_frac = 0;
411 : }
412 :
413 315 : pit_start = 3 * L_SUBFR - ( *position );
414 315 : pit_limit = 2 * L_FRAME - PIT_MAX - 2 - 2 * ( *position );
415 :
416 315 : if ( *T0 < pit_limit )
417 : {
418 242 : index = ( *T0 - pit_start ) * 2 + *T0_frac / 2;
419 : }
420 : else
421 : {
422 73 : index = *T0 - pit_limit + ( pit_limit - pit_start ) * 2;
423 73 : *T0_frac = 0;
424 : }
425 :
426 315 : push_indice( hBstr, IND_PITCH, index, nBits );
427 :
428 : /* Find the adaptive codebook vector - ACELP long-term prediction */
429 315 : pred_lt4( &exc[i_subfr], &exc[i_subfr], *T0, *T0_frac, L_SUBFR + 1, inter4_2, L_INTERPOL2, PIT_UP_SAMP );
430 :
431 315 : offset = tbe_celp_exc_offset( *T0, *T0_frac );
432 50715 : for ( i = 0; i < L_SUBFR * HIBND_ACB_L_FAC; i++ )
433 : {
434 50400 : bwe_exc[i + i_subfr * HIBND_ACB_L_FAC] = bwe_exc[i + i_subfr * HIBND_ACB_L_FAC - offset];
435 : }
436 : }
437 13226 : 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 705 : pit_flag = L_SUBFR;
445 705 : *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 705 : index = delta_pit_enc( 2, *T0, *T0_frac, *T0_min );
447 705 : push_indice( hBstr, IND_PITCH, index, nBits );
448 :
449 : /* Find the adaptive codebook vector - ACELP long-term prediction */
450 705 : pred_lt4( &exc[i_subfr], &exc[i_subfr], *T0, *T0_frac, L_SUBFR + 1, inter4_2, L_INTERPOL2, PIT_UP_SAMP );
451 :
452 705 : offset = tbe_celp_exc_offset( *T0, *T0_frac );
453 113505 : for ( i = 0; i < L_SUBFR * HIBND_ACB_L_FAC; i++ )
454 : {
455 112800 : 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 12521 : if ( nBits == 8 || nBits == 5 )
466 : {
467 1370 : if ( !( ( *tc_subfr == 0 ) && ( i_subfr == L_SUBFR ) ) )
468 : {
469 1370 : *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 11151 : *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 12521 : 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 12521 : pred_lt4( &exc[i_subfr], &exc[i_subfr], *T0, *T0_frac, L_SUBFR + 1, inter4_2, L_INTERPOL2, PIT_UP_SAMP );
481 :
482 12521 : offset = tbe_celp_exc_offset( *T0, *T0_frac );
483 2015881 : for ( i = 0; i < L_SUBFR * HIBND_ACB_L_FAC; i++ )
484 : {
485 2003360 : bwe_exc[i + i_subfr * HIBND_ACB_L_FAC] = bwe_exc[i + i_subfr * HIBND_ACB_L_FAC - offset];
486 : }
487 : }
488 :
489 16724 : if ( *Jopt_flag == 0 )
490 : {
491 : /* adaptive/TC excitation is zero */
492 1335 : mvr2r( xn, xn2, L_SUBFR );
493 1335 : g_corr[0] = 0.0f;
494 1335 : g_corr[1] = 0.0f;
495 1335 : *clip_gain = 0;
496 : }
497 : else
498 : {
499 15389 : *clip_gain = gp_clip( st->element_mode, st->core_brate, st->voicing, i_subfr, TRANSITION, xn, gp_cl );
500 :
501 15389 : 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 15389 : if ( lp_flag == NORMAL_OPERATION )
504 : {
505 12530 : push_indice( hBstr, IND_LP_FILT_SELECT, lp_select, 1 );
506 : }
507 : }
508 :
509 16724 : **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 16724 : if ( ( *tc_subfr >= 2 * L_SUBFR ) && ( i_subfr == 3 * L_SUBFR ) )
516 : {
517 641 : ( *pt_pitch ) -= 3;
518 641 : **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
519 641 : ( *pt_pitch )++;
520 641 : **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
521 641 : ( *pt_pitch )++;
522 641 : **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
523 641 : ( *pt_pitch )++;
524 : }
525 16083 : else if ( ( *tc_subfr == L_SUBFR ) && ( i_subfr == 2 * L_SUBFR ) )
526 : {
527 1230 : ( *pt_pitch ) -= 2;
528 1230 : **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
529 1230 : ( *pt_pitch )++;
530 1230 : **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
531 1230 : ( *pt_pitch )++;
532 : }
533 14853 : else if ( ( *tc_subfr == TC_0_64 ) && ( i_subfr == L_SUBFR ) )
534 : {
535 1143 : ( *pt_pitch ) -= 1;
536 1143 : **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
537 1143 : ( *pt_pitch )++;
538 : }
539 13710 : else if ( ( *tc_subfr == TC_0_128 ) && ( i_subfr == 2 * L_SUBFR ) )
540 : {
541 705 : ( *pt_pitch ) -= 2;
542 705 : **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
543 705 : ( *pt_pitch )++;
544 705 : **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
545 705 : ( *pt_pitch )++;
546 : }
547 13005 : else if ( ( *tc_subfr == TC_0_192 ) && ( i_subfr == 3 * L_SUBFR ) )
548 : {
549 315 : ( *pt_pitch ) -= 3;
550 315 : **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
551 315 : ( *pt_pitch )++;
552 315 : **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
553 315 : ( *pt_pitch )++;
554 315 : **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
555 315 : ( *pt_pitch )++;
556 : }
557 : }
558 : else /* L_frame == L_FRAME16k */
559 : {
560 17044 : if ( i_subfr >= 2 * L_SUBFR )
561 : {
562 13969 : limit_flag = 1;
563 : }
564 :
565 17044 : if ( i_subfr <= 2 * L_SUBFR )
566 : {
567 7009 : if ( i_subfr < 2 * L_SUBFR )
568 : {
569 3075 : mult_Top = 1;
570 3075 : if ( T_op[0] < PIT16k_MIN )
571 : {
572 269 : mult_Top = 2;
573 : }
574 3075 : 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 3934 : 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 17044 : nBits = st->acelp_cfg.pitch_bits[i_subfr / L_SUBFR];
587 :
588 : /*-----------------------------------------------------------------*
589 : * Find adaptive part of excitation, encode pitch period
590 : *-----------------------------------------------------------------*/
591 :
592 17044 : if ( nBits == 10 )
593 : {
594 3934 : *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 3934 : pit16k_Q_enc( hBstr, nBits, limit_flag, *T0, *T0_frac, T0_min, T0_max );
596 : }
597 13110 : 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 3075 : *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 3075 : if ( *T0_max > 2 * L_SUBFR )
608 : {
609 490 : *T0 = 2 * L_SUBFR;
610 490 : *T0_frac = 0;
611 : }
612 :
613 3075 : if ( *T0 < PIT16k_FR2_TC0_2SUBFR )
614 : {
615 1942 : index = ( *T0 ) * 4 + ( *T0_frac ) - ( PIT16k_MIN * 4 );
616 : }
617 : else
618 : {
619 1133 : index = ( *T0 ) * 2 + ( ( *T0_frac ) >> 1 ) - ( PIT16k_FR2_TC0_2SUBFR * 2 ) + ( ( PIT16k_FR2_TC0_2SUBFR - PIT16k_MIN ) * 4 );
620 : }
621 :
622 3075 : push_indice( hBstr, IND_PITCH, index, nBits );
623 : }
624 10035 : else if ( nBits == 6 )
625 : {
626 : /* delta search */
627 10035 : *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 10035 : index = delta_pit_enc( 4, *T0, *T0_frac, *T0_min );
630 :
631 10035 : push_indice( hBstr, IND_PITCH, index, nBits );
632 : }
633 17044 : if ( nBits == 6 )
634 : {
635 10035 : 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 17044 : if ( ( i_subfr == L_SUBFR ) && ( *T0 == 2 * L_SUBFR ) )
645 : {
646 490 : *gain_pit = 0.0f;
647 490 : *clip_gain = 0;
648 490 : g_corr[0] = 0.01f;
649 490 : g_corr[1] = 0.01f;
650 490 : *Jopt_flag = 0;
651 :
652 490 : set_f( &exc[i_subfr], 0, L_SUBFR + 1 ); /* set excitation for current subrame to 0 */
653 :
654 490 : push_indice( hBstr, IND_LP_FILT_SELECT, 0, 1 ); /* this bit is actually not needed */
655 :
656 490 : mvr2r( xn, xn2, L_SUBFR ); /* target vector for codebook search */
657 490 : set_f( y1, 0, L_SUBFR ); /* set filtered adaptive excitation to 0 */
658 490 : 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 16554 : pred_lt4( &exc[i_subfr], &exc[i_subfr], *T0, *T0_frac, L_SUBFR + 1, inter4_2, L_INTERPOL2, PIT_UP_SAMP );
664 :
665 2135466 : for ( i = 0; i < L_SUBFR * 2; i++ )
666 : {
667 2118912 : 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 16554 : *clip_gain = gp_clip( st->element_mode, st->core_brate, st->voicing, i_subfr, TRANSITION, xn, gp_cl );
671 :
672 16554 : 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 16554 : if ( lp_flag == NORMAL_OPERATION )
675 : {
676 16554 : push_indice( hBstr, IND_LP_FILT_SELECT, lp_select, 1 );
677 : }
678 :
679 16554 : *Jopt_flag = 1;
680 : }
681 :
682 17044 : **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 17044 : if ( ( i_subfr - *tc_subfr == L_SUBFR ) || ( *tc_subfr == 0 && i_subfr == 2 * L_SUBFR ) )
689 : {
690 8227 : index = i_subfr / L_SUBFR;
691 8227 : ( *pt_pitch ) -= index;
692 :
693 23093 : for ( i = 0; i < index; i++ )
694 : {
695 14866 : **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
696 14866 : ( *pt_pitch )++;
697 : }
698 : }
699 : }
700 : }
701 :
702 68367 : 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 15338 : 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 15338 : BSTR_ENC_HANDLE hBstr = st->hBstr;
743 :
744 15338 : imp_pos = *position - i_subfr;
745 :
746 : /*-----------------------------------------------------------------*
747 : * get number of bits for pitch encoding
748 : *-----------------------------------------------------------------*/
749 :
750 15338 : nBits = st->acelp_cfg.pitch_bits[i_subfr / L_SUBFR];
751 :
752 : /*--------------------------------------------------------------*
753 : * Closed loop pitch search
754 : *--------------------------------------------------------------*/
755 :
756 15338 : *T0_frac = 0;
757 15338 : if ( st->L_frame == L_FRAME )
758 : {
759 8323 : if ( ( *T0_min <= L_SUBFR ) || ( *tc_subfr == 3 * L_SUBFR ) )
760 : {
761 5584 : if ( nBits == 9 )
762 : {
763 1903 : *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 3681 : else if ( nBits == 6 )
766 : {
767 2857 : *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 824 : *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 2739 : *T0 = L_SUBFR;
777 : }
778 :
779 8323 : if ( ( *tc_subfr == L_SUBFR ) && ( *T0 < L_SUBFR ) )
780 : {
781 380 : *T0 = L_SUBFR;
782 : }
783 : }
784 : else /* L_frame == L_FRAME16k */
785 : {
786 7015 : if ( nBits == 10 )
787 : {
788 3081 : *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 3934 : else if ( nBits == 6 )
791 : {
792 : /* T0_frac with 1/2 sample resolution */
793 3934 : *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 3934 : if ( *T0 > L_SUBFR )
796 : {
797 2926 : *T0 = L_SUBFR;
798 2926 : *T0_frac = 0;
799 : }
800 : }
801 : }
802 :
803 : /* set tc_subfr to TC_0_0 */
804 15338 : if ( i_subfr == 0 && st->L_frame == L_FRAME && ( *T0 < L_SUBFR || *tc_subfr == 3 * L_SUBFR ) )
805 : {
806 2378 : *tc_subfr = TC_0_0;
807 : }
808 :
809 : /*--------------------------------------------------------------*
810 : * Builds glottal codebook contribution
811 : *--------------------------------------------------------------*/
812 :
813 15338 : 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 15338 : gain_trans_enc( &gain_trans, &exc[i_subfr], &imp_gain, &imp_sign );
820 :
821 : /* set past excitation buffer to zeros */
822 15338 : 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 15338 : pred_lt4_tc( exc, *T0, *T0_frac, inter4_2, imp_pos, i_subfr );
830 :
831 15338 : if ( st->hBWE_TD != NULL )
832 : {
833 15338 : if ( st->L_frame == L_FRAME )
834 : {
835 8323 : interp_code_5over2( &exc[i_subfr], &bwe_exc[i_subfr * HIBND_ACB_L_FAC], L_SUBFR );
836 : }
837 : else
838 : {
839 7015 : 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 15338 : conv( &exc[i_subfr], h1, y1, L_SUBFR );
849 :
850 : /* gain_pit computation */
851 15338 : *gain_pit = corr_xy1( xn, y1, g_corr, L_SUBFR, 0 );
852 :
853 : /*--------------------------------------------------------------*
854 : * Encode parameters and write indices
855 : *--------------------------------------------------------------*/
856 :
857 15338 : if ( st->L_frame == L_FRAME )
858 : {
859 8323 : if ( ( ( i_subfr != 0 ) || ( *tc_subfr == TC_0_0 ) ) && ( *tc_subfr != L_SUBFR ) )
860 : {
861 : /* write pitch index */
862 4930 : if ( ( *T0 >= L_SUBFR ) && ( *tc_subfr != 3 * L_SUBFR ) )
863 : {
864 365 : push_indice( hBstr, IND_PITCH, 0, nBits );
865 : }
866 4565 : else if ( *tc_subfr == 3 * L_SUBFR )
867 : {
868 1911 : if ( nBits == 9 )
869 : {
870 1903 : index = abs_pit_enc( 4, 0, *T0, *T0_frac );
871 : }
872 : else
873 : {
874 8 : index = abs_pit_enc( 2, 0, *T0, *T0_frac );
875 : }
876 :
877 1911 : push_indice( hBstr, IND_PITCH, index, nBits );
878 :
879 1911 : limit_T0( L_FRAME, 8, 0, 0, *T0, 0, T0_min, T0_max );
880 : }
881 : else
882 : {
883 2654 : if ( nBits == 6 )
884 : {
885 2378 : index = delta_pit_enc( 2, *T0, *T0_frac, PIT_MIN - 1 );
886 2378 : push_indice( hBstr, IND_PITCH, index, nBits );
887 : }
888 : else
889 : {
890 276 : index = delta_pit_enc( 0, *T0, *T0_frac, PIT_MIN - 1 );
891 276 : push_indice( hBstr, IND_PITCH, index, nBits );
892 : }
893 : }
894 : }
895 : }
896 : else /* L_frame == L_FRAME16k */
897 : {
898 7015 : if ( nBits == 10 )
899 : {
900 3081 : pit16k_Q_enc( hBstr, nBits, 1, *T0, *T0_frac, T0_min, T0_max );
901 : }
902 3934 : else if ( nBits == 6 )
903 : {
904 3934 : index = 2 * ( *T0 - PIT16k_MIN ) + *T0_frac / 2;
905 3934 : push_indice( hBstr, IND_PITCH, index, nBits );
906 : }
907 : }
908 :
909 15338 : push_indice( hBstr, IND_TC_IMP_SHAPE, imp_shape, 3 );
910 15338 : push_indice( hBstr, IND_TC_IMP_POS, imp_pos, 6 );
911 15338 : push_indice( hBstr, IND_TC_IMP_SIGN, imp_sign, 1 );
912 15338 : push_indice( hBstr, IND_TC_IMP_GAIN, imp_gain, 3 );
913 :
914 15338 : *position = imp_pos + i_subfr;
915 :
916 15338 : 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 15338 : 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 15338 : if ( *gain_trans < 0 )
938 : {
939 6984 : *gain_trans *= -1;
940 6984 : *quant_sign = 0;
941 : }
942 : else
943 : {
944 8354 : *quant_sign = 1;
945 : }
946 :
947 15338 : *quant_index = N_GAIN_TC - 1;
948 :
949 76941 : for ( i = 0; i < N_GAIN_TC - 1; i++ )
950 : {
951 73509 : if ( *gain_trans < tbl_gain_trans_tc[i] )
952 : {
953 11906 : *quant_index = i;
954 11906 : break;
955 : }
956 : }
957 :
958 : /* restore gain_trans */
959 15338 : *gain_trans = tbl_gain_trans_tc[i];
960 :
961 15338 : if ( *quant_sign == 0 )
962 : {
963 6984 : *gain_trans *= -1;
964 : }
965 :
966 996970 : for ( i = 0; i < L_SUBFR; i++ )
967 : {
968 981632 : exc[i] *= ( *gain_trans );
969 : }
970 :
971 15338 : return;
972 : }
973 :
974 :
975 : /*-----------------------------------------------------------------*
976 : * tc_classif_enc()
977 : *
978 : * TC subframe determination
979 : *-----------------------------------------------------------------*/
980 :
981 15338 : 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 15338 : T_op = pitch;
994 15338 : if ( L_frame == L_FRAME16k )
995 : {
996 7015 : T_op = (int16_t) ( pitch * 1.25f + 0.5f );
997 : }
998 :
999 15338 : *tc_subfr = -1;
1000 15338 : if ( attack_flag )
1001 : {
1002 5607 : *tc_subfr = 3 * L_SUBFR;
1003 :
1004 5607 : if ( attack_flag > 0 )
1005 : {
1006 5607 : if ( L_frame == L_FRAME )
1007 : {
1008 2754 : *tc_subfr = NB_SUBFR * ( attack_flag - 1 ) / 32 /*ATT_NSEG*/;
1009 : }
1010 : else
1011 : {
1012 2853 : *tc_subfr = NB_SUBFR16k * ( attack_flag - 1 ) / 32 /*ATT_NSEG*/;
1013 : }
1014 5607 : *tc_subfr *= L_SUBFR;
1015 : }
1016 : }
1017 :
1018 15338 : 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 5607 : *position = emaximum( res + *tc_subfr, min( T_op + 2, L_SUBFR ), &temp ) + *tc_subfr;
1031 : }
1032 : else
1033 : {
1034 9731 : *position = emaximum( res, (int16_t) ( T_op + 2 ), &temp );
1035 :
1036 : /* correction in case of possibly wrong T_op (double-pitch values) */
1037 9731 : if ( ( L_frame == L_FRAME && T_op > 2 * PIT_MIN ) ||
1038 4162 : ( L_frame == L_FRAME16k && T_op > 2 * PIT16k_MIN ) )
1039 : {
1040 : int16_t position_tmp, len;
1041 : float aver, temp2;
1042 :
1043 4594 : len = (int16_t) ( T_op / 2 + 2 );
1044 :
1045 4594 : position_tmp = emaximum( res, len, &temp2 );
1046 4594 : aver = dotp( res, res, len ) + 0.01f;
1047 4594 : aver = sqrtf( aver / len );
1048 :
1049 4594 : temp = sqrtf( temp );
1050 4594 : temp2 = sqrtf( temp2 );
1051 :
1052 4594 : if ( temp2 > 0.8f * temp && aver < 0.25f * temp )
1053 : {
1054 354 : *position = position_tmp;
1055 : }
1056 : }
1057 :
1058 9731 : *tc_subfr = (int16_t) floor( ( *position ) / L_SUBFR ) * L_SUBFR;
1059 : }
1060 :
1061 15338 : return;
1062 : }
|