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 tc_dec( Decoder_State *st, const int16_t L_frame, float exc[], int16_t *T0, int16_t *T0_frac, const int16_t i_subfr, const int16_t tc_subfr, int16_t *position, float bwe_exc[] );
53 :
54 :
55 : /*-------------------------------------------------------------------*
56 : * transition_dec()
57 : *
58 : * Principal function for TC decoding
59 : *-------------------------------------------------------------------*/
60 :
61 1718473 : void transition_dec(
62 : Decoder_State *st, /* i/o: decoder state structure */
63 : const int16_t L_frame, /* i : length of the frame */
64 : const int16_t i_subfr, /* i : subframe index */
65 : const int16_t tc_subfr, /* i : TC subframe index */
66 : int16_t *Jopt_flag, /* i : joint optimization flag */
67 : float *exc, /* o : excitation signal */
68 : int16_t *T0, /* o : close loop integer pitch */
69 : int16_t *T0_frac, /* o : close loop fractional part of the pitch */
70 : int16_t *T0_min, /* i/o: delta search min for sf 2 & 4 */
71 : int16_t *T0_max, /* i/o: delta search max for sf 2 & 4 */
72 : float **pt_pitch, /* o : floating pitch values */
73 : int16_t *position, /* i/o: first glottal impulse position in frame */
74 : float *bwe_exc /* o : excitation for SWB TBE */
75 : )
76 : {
77 : int16_t i, pit_flag, pit_start, pit_limit, index, nBits;
78 : int16_t limit_flag;
79 : int16_t offset;
80 :
81 : /* Set limit_flag to 0 for restrained limits, and 1 for extended limits */
82 1718473 : limit_flag = 0;
83 :
84 : /*---------------------------------------------------------------------*
85 : * zero adaptive contribution (glottal shape codebook search not
86 : * in first subframe(s) )
87 : *---------------------------------------------------------------------*/
88 :
89 1718473 : if ( tc_subfr > i_subfr + TC_0_192 )
90 : {
91 541238 : set_f( &exc[i_subfr], 0, L_SUBFR );
92 :
93 541238 : if ( L_frame == L_FRAME )
94 : {
95 214421 : 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 */
96 : }
97 : else
98 : {
99 326817 : set_f( &bwe_exc[i_subfr * 2], 0, (int16_t) ( L_SUBFR * 2 ) ); /* set past excitation buffer to 0 */
100 : }
101 :
102 541238 : *T0 = L_SUBFR;
103 541238 : *T0_frac = 0;
104 541238 : **pt_pitch = (float) L_SUBFR;
105 : }
106 :
107 : /*---------------------------------------------------------------------*
108 : * glottal shape codebook search
109 : *---------------------------------------------------------------------*/
110 :
111 1177235 : else if ( ( tc_subfr - i_subfr >= 0 ) && ( tc_subfr - i_subfr <= TC_0_192 ) )
112 : {
113 380028 : set_f( exc - L_EXC_MEM, 0, L_EXC_MEM ); /* set past excitation buffer to 0 */
114 :
115 380028 : if ( L_frame == L_FRAME )
116 : {
117 181667 : set_f( bwe_exc - PIT_MAX * HIBND_ACB_L_FAC, 0, PIT_MAX * HIBND_ACB_L_FAC ); /* set past excitation buffer to 0 */
118 : }
119 : else
120 : {
121 198361 : set_f( bwe_exc - PIT16k_MAX * 2, 0, PIT16k_MAX * 2 ); /* set past excitation buffer to 0 */
122 : }
123 :
124 : /* glottal shape codebook contribution construction */
125 380028 : tc_dec( st, L_frame, exc, T0, T0_frac, i_subfr, tc_subfr, position, bwe_exc );
126 :
127 380028 : **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f; /* save subframe pitch values */
128 380028 : *Jopt_flag = 1;
129 : }
130 :
131 : /*---------------------------------------------------------------------*
132 : * Regular ACELP Decoding using GENERIC type decoder
133 : * (all subframes following subframe with glottal shape codebook seach)
134 : * - search the position of the 2nd glottal impulse in case that the first
135 : * one is in the 1st subframe (different adaptive contribution
136 : * construction and the pitch period coding is used)
137 : *---------------------------------------------------------------------*/
138 :
139 797207 : else if ( tc_subfr < i_subfr )
140 : {
141 797207 : if ( L_frame == L_FRAME )
142 : {
143 330580 : *Jopt_flag = 1;
144 :
145 330580 : if ( ( i_subfr - tc_subfr >= L_SUBFR ) && ( i_subfr - tc_subfr <= L_SUBFR + TC_0_192 ) )
146 : {
147 40428 : pit_flag = 0;
148 : }
149 : else
150 : {
151 290152 : pit_flag = L_SUBFR;
152 : }
153 :
154 330580 : if ( tc_subfr == TC_0_0 )
155 : {
156 148428 : if ( i_subfr == L_SUBFR )
157 : {
158 49476 : limit_T0( L_FRAME, 8, pit_flag, limit_flag, *T0, 0, T0_min, T0_max );
159 : }
160 :
161 148428 : pit_flag = 1;
162 : }
163 :
164 : /*-----------------------------------------------------------------*
165 : * get number of bits for pitch decoding
166 : *-----------------------------------------------------------------*/
167 :
168 330580 : nBits = st->acelp_cfg.pitch_bits[i_subfr / L_SUBFR];
169 :
170 : /*------------------------------------------------------------*
171 : * first glottal impulse is in the 1st subframe
172 : *------------------------------------------------------------*/
173 :
174 330580 : if ( ( i_subfr == L_SUBFR ) && ( tc_subfr >= TC_0_128 ) )
175 : {
176 : /*--------------------------------------------------------*
177 : * second glottal impulse is in the 3rd or 4th subframe
178 : * - build exc[] in 2nd subframe
179 : *--------------------------------------------------------*/
180 :
181 18840 : *T0 = 2 * L_SUBFR;
182 18840 : *T0_frac = 0;
183 18840 : *Jopt_flag = 0;
184 :
185 : /* set adaptive part of exciation for curent subframe to 0 */
186 18840 : set_f( &exc[i_subfr], 0, (int16_t) ( L_SUBFR + 1 ) );
187 :
188 18840 : set_f( &bwe_exc[i_subfr * HIBND_ACB_L_FAC], 0, (int16_t) ( L_SUBFR * HIBND_ACB_L_FAC ) );
189 : }
190 311740 : else if ( ( i_subfr == L_SUBFR ) && ( tc_subfr == TC_0_64 ) )
191 : {
192 : /*--------------------------------------------------------*
193 : * second glottal impulse is in the 2nd subframe,
194 : * - build exc[] in 2nd subframe
195 : *--------------------------------------------------------*/
196 :
197 19551 : if ( PIT_MIN > ( *position ) )
198 : {
199 14567 : pit_start = L_SUBFR - ( *position );
200 : }
201 : else
202 : {
203 4984 : pit_start = PIT_MIN;
204 : }
205 :
206 19551 : if ( pit_start < PIT_MIN )
207 : {
208 1118 : pit_start = PIT_MIN;
209 : }
210 :
211 19551 : pit_limit = 2 * pit_start + ( *position );
212 :
213 : /* 7 bit pitch DECODER */
214 19551 : index = get_next_indice( st, nBits );
215 :
216 19551 : *T0 = (int16_t) ( floor( pit_start + index / 2 ) );
217 19551 : *T0_frac = ( index - ( *T0 - pit_start ) * 2 ) * 2;
218 19551 : limit_T0( L_FRAME, 8, pit_flag, limit_flag, *T0, 0, T0_min, T0_max ); /* find T0_min and T0_max */
219 :
220 : /* Find the adaptive codebook vector - ACELP long-term prediction */
221 19551 : pred_lt4( &exc[i_subfr], &exc[i_subfr], *T0, *T0_frac, L_SUBFR + 1, inter4_2, L_INTERPOL2, PIT_UP_SAMP );
222 :
223 19551 : offset = tbe_celp_exc_offset( *T0, *T0_frac );
224 3147711 : for ( i = 0; i < L_SUBFR * HIBND_ACB_L_FAC; i++ )
225 : {
226 3128160 : bwe_exc[i + i_subfr * HIBND_ACB_L_FAC] = bwe_exc[i + i_subfr * HIBND_ACB_L_FAC - offset];
227 : }
228 : }
229 292189 : else if ( ( i_subfr == 2 * L_SUBFR ) && ( tc_subfr == TC_0_128 ) )
230 : {
231 : /*--------------------------------------------------------*
232 : * second glottal impulse is in the 3rd subframe
233 : * - build exc[] in 3rd subframe
234 : *--------------------------------------------------------*/
235 :
236 : /* 7bit pitch DECODER */
237 13535 : pit_start = 2 * L_SUBFR - ( *position );
238 :
239 13535 : index = get_next_indice( st, nBits );
240 :
241 13535 : *T0 = (int16_t) ( floor( pit_start + (int16_t) ( index / 2 ) ) );
242 13535 : *T0_frac = ( index - ( *T0 - pit_start ) * 2 ) * 2;
243 :
244 13535 : limit_T0( L_FRAME, 8, pit_flag, limit_flag, *T0, 0, T0_min, T0_max ); /* find T0_min and T0_max */
245 :
246 : /* Find the adaptive codebook vector. ACELP long-term prediction */
247 13535 : pred_lt4( &exc[i_subfr], &exc[i_subfr], *T0, *T0_frac, L_SUBFR + 1, inter4_2, L_INTERPOL2, PIT_UP_SAMP );
248 :
249 13535 : offset = tbe_celp_exc_offset( *T0, *T0_frac );
250 2179135 : for ( i = 0; i < L_SUBFR * HIBND_ACB_L_FAC; i++ )
251 : {
252 2165600 : bwe_exc[i + i_subfr * HIBND_ACB_L_FAC] = bwe_exc[i + i_subfr * HIBND_ACB_L_FAC - offset];
253 : }
254 : }
255 278654 : else if ( ( i_subfr == 2 * L_SUBFR ) && ( tc_subfr == TC_0_192 ) )
256 : {
257 : /*--------------------------------------------------------*
258 : * second glottal impulse is in the 4th subframe
259 : * - build exc[] in 3rd subframe
260 : *--------------------------------------------------------*/
261 :
262 5305 : *T0 = 4 * L_SUBFR;
263 5305 : *T0_frac = 0;
264 5305 : *Jopt_flag = 0;
265 :
266 : /* set adaptive part of exciation for curent subframe to 0 */
267 5305 : set_f( &exc[i_subfr], 0, (int16_t) ( L_SUBFR + 1 ) );
268 :
269 5305 : set_f( &bwe_exc[i_subfr * HIBND_ACB_L_FAC], 0, (int16_t) ( L_SUBFR * HIBND_ACB_L_FAC ) );
270 : }
271 273349 : else if ( ( i_subfr == 3 * L_SUBFR ) && ( tc_subfr == TC_0_192 ) )
272 : {
273 : /*--------------------------------------------------------*
274 : * second glottal impulse is in the 4th subframe
275 : * - build exc[] in 4th subframe
276 : *--------------------------------------------------------*/
277 :
278 5305 : pit_start = 3 * L_SUBFR - ( *position );
279 5305 : pit_limit = 2 * L_FRAME - PIT_MAX - 2 * ( *position ) - 2;
280 :
281 5305 : index = get_next_indice( st, nBits );
282 :
283 5305 : if ( index < ( pit_limit - pit_start ) * 2 )
284 : {
285 4299 : *T0 = (int16_t) ( floor( pit_start + ( index / 2 ) ) );
286 4299 : *T0_frac = ( index - ( ( *T0 ) - pit_start ) * 2 ) * 2;
287 : }
288 : else
289 : {
290 1006 : *T0 = index + pit_limit - ( pit_limit - pit_start ) * 2;
291 1006 : *T0_frac = 0;
292 : }
293 :
294 : /* biterror detection mechanism */
295 5305 : if ( ( ( *T0 << 2 ) + *T0_frac ) > ( PIT_MAX << 2 ) + 2 )
296 : {
297 0 : *T0 = L_SUBFR;
298 0 : *T0_frac = 0;
299 0 : st->BER_detect = 1;
300 : }
301 :
302 : /* Find the adaptive codebook vector. ACELP long-term prediction */
303 5305 : pred_lt4( &exc[i_subfr], &exc[i_subfr], *T0, *T0_frac, L_SUBFR + 1, inter4_2, L_INTERPOL2, PIT_UP_SAMP );
304 :
305 5305 : offset = tbe_celp_exc_offset( *T0, *T0_frac );
306 854105 : for ( i = 0; i < L_SUBFR * HIBND_ACB_L_FAC; i++ )
307 : {
308 848800 : bwe_exc[i + i_subfr * HIBND_ACB_L_FAC] = bwe_exc[i + i_subfr * HIBND_ACB_L_FAC - offset];
309 : }
310 : }
311 268044 : else if ( ( i_subfr == 3 * L_SUBFR ) && ( tc_subfr == TC_0_128 ) )
312 : {
313 : /*--------------------------------------------------------*
314 : * second glottal impulse in the 3rd subframe
315 : * build exc[] in 4th subframe
316 : *--------------------------------------------------------*/
317 :
318 13535 : index = get_next_indice( st, nBits );
319 :
320 13535 : delta_pit_dec( 2, index, T0, T0_frac, *T0_min );
321 :
322 : /* Find the adaptive codebook vector. ACELP long-term prediction */
323 13535 : pred_lt4( &exc[i_subfr], &exc[i_subfr], *T0, *T0_frac, L_SUBFR + 1, inter4_2, L_INTERPOL2, PIT_UP_SAMP );
324 :
325 13535 : offset = tbe_celp_exc_offset( *T0, *T0_frac );
326 2179135 : for ( i = 0; i < L_SUBFR * HIBND_ACB_L_FAC; i++ )
327 : {
328 2165600 : bwe_exc[i + i_subfr * HIBND_ACB_L_FAC] = bwe_exc[i + i_subfr * HIBND_ACB_L_FAC - offset];
329 : }
330 : }
331 :
332 : /*------------------------------------------------------------*
333 : * first glottal impulse is NOT in the 1st subframe,
334 : * or two impulses are in the 1st subframe
335 : *------------------------------------------------------------*/
336 : else
337 : {
338 254509 : index = get_next_indice( st, nBits );
339 :
340 254509 : pit_Q_dec( 0, index, nBits, 8, pit_flag, limit_flag, T0, T0_frac, T0_min, T0_max, &st->BER_detect );
341 :
342 : /* Find the adaptive codebook vector */
343 254509 : pred_lt4( &exc[i_subfr], &exc[i_subfr], *T0, *T0_frac, L_SUBFR + 1, inter4_2, L_INTERPOL2, PIT_UP_SAMP );
344 :
345 254509 : offset = tbe_celp_exc_offset( *T0, *T0_frac );
346 40975949 : for ( i = 0; i < L_SUBFR * HIBND_ACB_L_FAC; i++ )
347 : {
348 40721440 : bwe_exc[i + i_subfr * HIBND_ACB_L_FAC] = bwe_exc[i + i_subfr * HIBND_ACB_L_FAC - offset];
349 : }
350 : }
351 :
352 : /*-----------------------------------------------------------------*
353 : * LP filtering of the adaptive excitation (if non-zero)
354 : *-----------------------------------------------------------------*/
355 :
356 330580 : if ( *Jopt_flag )
357 : {
358 306435 : lp_filt_exc_dec( st, MODE1, i_subfr, L_SUBFR, L_frame, st->acelp_cfg.ltf_mode, exc );
359 : }
360 :
361 : /*---------------------------------------------------------------------*
362 : * fill the pitch buffer - needed for post-processing and FEC_clas_estim()
363 : *---------------------------------------------------------------------*/
364 :
365 330580 : **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f; /* save subframe pitch values */
366 330580 : if ( ( tc_subfr >= 2 * L_SUBFR ) && ( i_subfr == 3 * L_SUBFR ) )
367 : {
368 13877 : ( *pt_pitch ) -= 3;
369 13877 : **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
370 13877 : ( *pt_pitch )++;
371 13877 : **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
372 13877 : ( *pt_pitch )++;
373 13877 : **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
374 13877 : ( *pt_pitch )++;
375 : }
376 316703 : else if ( ( tc_subfr == L_SUBFR ) && ( i_subfr == 2 * L_SUBFR ) )
377 : {
378 26551 : ( *pt_pitch ) -= 2;
379 26551 : **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
380 26551 : ( *pt_pitch )++;
381 26551 : **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
382 26551 : ( *pt_pitch )++;
383 : }
384 290152 : else if ( ( tc_subfr == TC_0_64 ) && ( i_subfr == L_SUBFR ) )
385 : {
386 19551 : ( *pt_pitch ) -= 1;
387 19551 : **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
388 19551 : ( *pt_pitch )++;
389 : }
390 270601 : else if ( ( tc_subfr == TC_0_128 ) && ( i_subfr == 2 * L_SUBFR ) )
391 : {
392 13535 : ( *pt_pitch ) -= 2;
393 13535 : **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
394 13535 : ( *pt_pitch )++;
395 13535 : **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
396 13535 : ( *pt_pitch )++;
397 : }
398 257066 : else if ( ( tc_subfr == TC_0_192 ) && ( i_subfr == 3 * L_SUBFR ) )
399 : {
400 5305 : ( *pt_pitch ) -= 3;
401 5305 : **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
402 5305 : ( *pt_pitch )++;
403 5305 : **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
404 5305 : ( *pt_pitch )++;
405 5305 : **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
406 5305 : ( *pt_pitch )++;
407 : }
408 : }
409 : else /* L_frame == L_FRAME16k */
410 : {
411 466627 : if ( i_subfr >= 2 * L_SUBFR )
412 : {
413 384678 : limit_flag = 1;
414 : }
415 :
416 466627 : if ( i_subfr - tc_subfr == L_SUBFR )
417 : {
418 144474 : limit_T0( L_FRAME16k, 8, 0, limit_flag, *T0, *T0_frac, T0_min, T0_max ); /* find T0_min and T0_max */
419 : }
420 :
421 : /*-----------------------------------------------------------------*
422 : * get number of bits and index for pitch decoding
423 : *-----------------------------------------------------------------*/
424 :
425 466627 : nBits = st->acelp_cfg.pitch_bits[i_subfr / L_SUBFR];
426 :
427 466627 : index = get_next_indice( st, nBits );
428 :
429 : /*-----------------------------------------------------------------*
430 : * Find adaptive part of excitation, encode pitch period
431 : *-----------------------------------------------------------------*/
432 :
433 466627 : if ( nBits == 10 )
434 : {
435 105111 : pit16k_Q_dec( index, nBits, limit_flag, T0, T0_frac, T0_min, T0_max, &st->BER_detect );
436 : }
437 361516 : else if ( nBits == 8 ) /* tc_subfr==0 && i_subfr==L_SUBFR */
438 : {
439 : /*-----------------------------------------------------------------------------*
440 : * The pitch range is encoded absolutely with 8 bits and is divided as follows:
441 : * PIT16k_MIN to PIT16k_FR2_TC0_2SUBFR-1 resolution 1/4 (frac = 0,1,2 or 3)
442 : * PIT16k_FR2_TC0_2SUBFR to 2*L_SUBFR resolution 1/2 (frac = 0 or 2)
443 : *-----------------------------------------------------------------------------*/
444 :
445 81949 : if ( index < ( PIT16k_FR2_TC0_2SUBFR - PIT16k_MIN ) * 4 )
446 : {
447 49534 : *T0 = PIT16k_MIN + ( index / 4 );
448 49534 : *T0_frac = index - ( *T0 - PIT16k_MIN ) * 4;
449 : }
450 : else
451 : {
452 32415 : index -= ( PIT16k_FR2_TC0_2SUBFR - PIT16k_MIN ) * 4;
453 32415 : *T0 = PIT16k_FR2_TC0_2SUBFR + ( index / 2 );
454 32415 : *T0_frac = index - ( *T0 - PIT16k_FR2_TC0_2SUBFR ) * 2;
455 32415 : ( *T0_frac ) *= 2;
456 : }
457 :
458 : /* biterror detection mechanism */
459 81949 : if ( ( ( *T0 << 2 ) + *T0_frac ) > ( ( 2 * L_SUBFR ) << 2 ) )
460 : {
461 0 : *T0 = L_SUBFR;
462 0 : *T0_frac = 0;
463 0 : st->BER_detect = 1;
464 : }
465 : }
466 279567 : else if ( nBits == 6 )
467 : {
468 279567 : delta_pit_dec( 4, index, T0, T0_frac, *T0_min );
469 : }
470 466627 : if ( nBits == 6 )
471 : {
472 279567 : limit_T0( L_FRAME16k, 8, L_SUBFR, limit_flag, *T0, *T0_frac, T0_min, T0_max ); /* find T0_min and T0_max */
473 : }
474 :
475 : /*-----------------------------------------------------------------*
476 : * - find the adaptive codebook vector
477 : * - LP filtering of the adaptive excitation (if non-zero)
478 : *-----------------------------------------------------------------*/
479 :
480 466627 : if ( ( i_subfr == L_SUBFR ) && ( *T0 == 2 * L_SUBFR ) )
481 : {
482 : /* no adaptive excitation in the second subframe */
483 15358 : set_f( &exc[i_subfr], 0, L_SUBFR + 1 );
484 :
485 15358 : get_next_indice( st, 1 ); /* this bit is actually not needed */
486 :
487 15358 : set_f( &bwe_exc[i_subfr * 2], 0, L_SUBFR * 2 );
488 : }
489 : else
490 : {
491 : /* Find the adaptive codebook vector - ACELP long-term prediction */
492 451269 : pred_lt4( &exc[i_subfr], &exc[i_subfr], *T0, *T0_frac, L_SUBFR + 1, inter4_2, L_INTERPOL2, PIT_UP_SAMP );
493 :
494 58213701 : for ( i = 0; i < L_SUBFR * 2; i++ )
495 : {
496 57762432 : 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];
497 : }
498 :
499 451269 : lp_filt_exc_dec( st, MODE1, i_subfr, L_SUBFR, L_frame, st->acelp_cfg.ltf_mode, exc );
500 :
501 451269 : *Jopt_flag = 1;
502 : }
503 :
504 466627 : **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f; /* save subframe pitch values */
505 :
506 : /*---------------------------------------------------------------------*
507 : * fill the pitch buffer - needed for post-processing and FEC_clas_estim()
508 : *---------------------------------------------------------------------*/
509 :
510 466627 : if ( ( i_subfr - tc_subfr == L_SUBFR ) || ( tc_subfr == 0 && i_subfr == 2 * L_SUBFR ) )
511 : {
512 226423 : index = i_subfr / L_SUBFR;
513 226423 : ( *pt_pitch ) -= index;
514 :
515 646064 : for ( i = 0; i < index; i++ )
516 : {
517 419641 : **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
518 419641 : ( *pt_pitch )++;
519 : }
520 : }
521 : }
522 : }
523 :
524 1718473 : return;
525 : }
526 :
527 : /*---------------------------------------------------------------------*
528 : * tc_dec()
529 : *
530 : * Principal function for TC decoding.
531 : * - constructs glottal codebook contribution
532 : * - uses pitch sharpening
533 : * - uses gain_trans
534 : *---------------------------------------------------------------------*/
535 :
536 380028 : static void tc_dec(
537 : Decoder_State *st, /* i/o: decoder state structure */
538 : const int16_t L_frame, /* i : length of the frame */
539 : float exc[], /* o : glottal codebook contribution */
540 : int16_t *T0, /* o : close-loop pitch period */
541 : int16_t *T0_frac, /* o : close-loop pitch period - fractional part */
542 : const int16_t i_subfr, /* i : subframe index */
543 : const int16_t tc_subfr, /* i : TC subframe index */
544 : int16_t *position, /* o : first glottal impulse position in frame */
545 : float bwe_exc[] /* o : excitation for SWB TBE */
546 : )
547 : {
548 : int16_t i, imp_shape, imp_pos, imp_sign, imp_gain, nBits;
549 : float gain_trans;
550 : int16_t index;
551 :
552 : /*----------------------------------------------------------------*
553 : * find the number of bits
554 : *----------------------------------------------------------------*/
555 :
556 380028 : nBits = st->acelp_cfg.pitch_bits[i_subfr / L_SUBFR];
557 :
558 : /*----------------------------------------------------------------*
559 : * decode parameter T0 (pitch period)
560 : *----------------------------------------------------------------*/
561 :
562 380028 : if ( L_frame == L_FRAME )
563 : {
564 181667 : if ( ( ( i_subfr == 0 ) && ( ( tc_subfr == 0 ) || ( tc_subfr == TC_0_64 ) || ( tc_subfr == TC_0_128 ) || ( tc_subfr == TC_0_192 ) ) ) || ( tc_subfr == L_SUBFR ) )
565 : {
566 64942 : *T0 = L_SUBFR;
567 64942 : *T0_frac = 0;
568 : }
569 116725 : else if ( ( tc_subfr == 3 * L_SUBFR ) )
570 : {
571 53372 : i = get_next_indice( st, nBits );
572 :
573 53372 : if ( nBits == 9 )
574 : {
575 53163 : abs_pit_dec( 4, i, 0, T0, T0_frac );
576 : }
577 : else
578 : {
579 209 : abs_pit_dec( 2, i, 0, T0, T0_frac );
580 : }
581 : }
582 : else
583 : {
584 63353 : i = get_next_indice( st, nBits );
585 :
586 63353 : if ( i == 0 )
587 : {
588 7625 : *T0 = L_SUBFR;
589 7625 : *T0_frac = 0;
590 : }
591 : else
592 : {
593 55728 : if ( tc_subfr == TC_0_0 )
594 : {
595 49476 : delta_pit_dec( 2, i, T0, T0_frac, PIT_MIN - 1 );
596 : }
597 : else
598 : {
599 6252 : delta_pit_dec( 0, i, T0, T0_frac, PIT_MIN - 1 );
600 : }
601 : }
602 : }
603 : }
604 : else /* L_frame == L_FRAME16k */
605 : {
606 198361 : i = get_next_indice( st, nBits );
607 :
608 198361 : if ( nBits == 10 )
609 : {
610 93250 : if ( i < ( PIT16k_FR2_EXTEND_10b - PIT16k_MIN_EXTEND ) * 4 )
611 : {
612 90826 : *T0 = PIT16k_MIN_EXTEND + ( i / 4 );
613 90826 : *T0_frac = i - ( ( *T0 - PIT16k_MIN_EXTEND ) * 4 );
614 : }
615 : else
616 : {
617 2424 : index = i - ( PIT16k_FR2_EXTEND_10b - PIT16k_MIN_EXTEND ) * 4;
618 2424 : *T0 = PIT16k_FR2_EXTEND_10b + ( index / 2 );
619 2424 : *T0_frac = index - ( *T0 - PIT16k_FR2_EXTEND_10b ) * 2;
620 2424 : ( *T0_frac ) *= 2;
621 : }
622 : }
623 105111 : else if ( nBits == 6 )
624 : {
625 105111 : *T0 = PIT16k_MIN + ( i / 2 );
626 105111 : *T0_frac = i - ( *T0 - PIT16k_MIN ) * 2;
627 105111 : *T0_frac *= 2;
628 : }
629 : }
630 :
631 : /*----------------------------------------------------------------*
632 : * decode other TC parameters
633 : *----------------------------------------------------------------*/
634 :
635 380028 : imp_shape = get_next_indice( st, 3 );
636 380028 : imp_pos = get_next_indice( st, 6 );
637 380028 : imp_sign = get_next_indice( st, 1 );
638 380028 : imp_gain = get_next_indice( st, 3 );
639 :
640 : /*----------------------------------------------------------------*
641 : * restore gain_trans
642 : * build glottal codebook contribution
643 : *----------------------------------------------------------------*/
644 :
645 380028 : gain_trans = tbl_gain_trans_tc[imp_gain];
646 :
647 380028 : if ( imp_sign == 0 )
648 : {
649 173715 : gain_trans *= -1;
650 : }
651 :
652 : /* build glottal codebook contribution */
653 380028 : set_f( &exc[i_subfr], 0, L_SUBFR );
654 :
655 6840504 : for ( i = ( imp_pos - L_IMPULSE2 ); i <= ( imp_pos + L_IMPULSE2 ); i++ )
656 : {
657 6460476 : if ( ( i >= 0 ) && ( i < L_SUBFR ) )
658 : {
659 6050568 : exc[i + i_subfr] = glottal_cdbk[(imp_shape) *L_IMPULSE + i - imp_pos + L_IMPULSE2] * gain_trans;
660 : }
661 : }
662 :
663 : /*--------------------------------------------------------------*
664 : * adapt. search of the second impulse in the same subframe
665 : * (when appears)
666 : *--------------------------------------------------------------*/
667 :
668 380028 : pred_lt4_tc( exc, *T0, *T0_frac, inter4_2, imp_pos, i_subfr );
669 :
670 380028 : if ( st->hBWE_TD != NULL )
671 : {
672 380028 : if ( L_frame == L_FRAME )
673 : {
674 181667 : interp_code_5over2( &exc[i_subfr], &bwe_exc[i_subfr * HIBND_ACB_L_FAC], L_SUBFR );
675 : }
676 : else
677 : {
678 198361 : interp_code_4over2( &exc[i_subfr], &bwe_exc[i_subfr * 2], L_SUBFR );
679 : }
680 : }
681 :
682 380028 : *position = imp_pos + i_subfr;
683 :
684 380028 : return;
685 : }
686 :
687 : /*-------------------------------------------------------------------*
688 : * tc_classif()
689 : *
690 : * TC subframe classification decoding
691 : *-------------------------------------------------------------------*/
692 :
693 380028 : int16_t tc_classif(
694 : Decoder_State *st /* i/o: decoder state structure */
695 : )
696 : {
697 : int16_t tc_subfr, indice;
698 :
699 380028 : if ( st->L_frame == L_FRAME )
700 : {
701 181667 : if ( get_next_indice( st, 1 ) )
702 : {
703 49476 : tc_subfr = TC_0_0;
704 : }
705 : else
706 : {
707 132191 : if ( get_next_indice( st, 1 ) )
708 : {
709 38391 : tc_subfr = 0;
710 :
711 38391 : if ( get_next_indice( st, 1 ) )
712 : {
713 5305 : tc_subfr = TC_0_192;
714 : }
715 : else
716 : {
717 33086 : if ( get_next_indice( st, 1 ) )
718 : {
719 19551 : tc_subfr = TC_0_64;
720 : }
721 : else
722 : {
723 13535 : tc_subfr = TC_0_128;
724 : }
725 : }
726 : }
727 : else
728 : {
729 93800 : if ( get_next_indice( st, 1 ) )
730 : {
731 26551 : tc_subfr = L_SUBFR;
732 : }
733 : else
734 : {
735 67249 : if ( get_next_indice( st, 1 ) )
736 : {
737 13877 : tc_subfr = 2 * L_SUBFR;
738 : }
739 : else
740 : {
741 53372 : tc_subfr = 3 * L_SUBFR;
742 : }
743 : }
744 : }
745 : }
746 : }
747 : else /* L_frame == L_FRAME16k */
748 : {
749 198361 : indice = get_next_indice( st, 2 );
750 :
751 198361 : if ( indice < 3 )
752 : {
753 135093 : tc_subfr = indice * L_SUBFR;
754 : }
755 : else
756 : {
757 63268 : if ( get_next_indice( st, 1 ) == 0 )
758 : {
759 9381 : tc_subfr = 3 * L_SUBFR;
760 : }
761 : else
762 : {
763 53887 : tc_subfr = 4 * L_SUBFR;
764 : }
765 : }
766 : }
767 :
768 380028 : return ( tc_subfr );
769 : }
|