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 1308333 : 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 1308333 : limit_flag = 0;
83 :
84 : /*---------------------------------------------------------------------*
85 : * zero adaptive contribution (glottal shape codebook search not
86 : * in first subframe(s) )
87 : *---------------------------------------------------------------------*/
88 :
89 1308333 : if ( tc_subfr > i_subfr + TC_0_192 )
90 : {
91 389173 : set_f( &exc[i_subfr], 0, L_SUBFR );
92 :
93 389173 : if ( L_frame == L_FRAME )
94 : {
95 144086 : 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 245087 : set_f( &bwe_exc[i_subfr * 2], 0, (int16_t) ( L_SUBFR * 2 ) ); /* set past excitation buffer to 0 */
100 : }
101 :
102 389173 : *T0 = L_SUBFR;
103 389173 : *T0_frac = 0;
104 389173 : **pt_pitch = (float) L_SUBFR;
105 : }
106 :
107 : /*---------------------------------------------------------------------*
108 : * glottal shape codebook search
109 : *---------------------------------------------------------------------*/
110 :
111 919160 : else if ( ( tc_subfr - i_subfr >= 0 ) && ( tc_subfr - i_subfr <= TC_0_192 ) )
112 : {
113 289393 : set_f( exc - L_EXC_MEM, 0, L_EXC_MEM ); /* set past excitation buffer to 0 */
114 :
115 289393 : if ( L_frame == L_FRAME )
116 : {
117 138632 : 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 150761 : 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 289393 : tc_dec( st, L_frame, exc, T0, T0_frac, i_subfr, tc_subfr, position, bwe_exc );
126 :
127 289393 : **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f; /* save subframe pitch values */
128 289393 : *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 629767 : else if ( tc_subfr < i_subfr )
140 : {
141 629767 : if ( L_frame == L_FRAME )
142 : {
143 271810 : *Jopt_flag = 1;
144 :
145 271810 : if ( ( i_subfr - tc_subfr >= L_SUBFR ) && ( i_subfr - tc_subfr <= L_SUBFR + TC_0_192 ) )
146 : {
147 33376 : pit_flag = 0;
148 : }
149 : else
150 : {
151 238434 : pit_flag = L_SUBFR;
152 : }
153 :
154 271810 : if ( tc_subfr == TC_0_0 )
155 : {
156 91368 : if ( i_subfr == L_SUBFR )
157 : {
158 30456 : limit_T0( L_FRAME, 8, pit_flag, limit_flag, *T0, 0, T0_min, T0_max );
159 : }
160 :
161 91368 : pit_flag = 1;
162 : }
163 :
164 : /*-----------------------------------------------------------------*
165 : * get number of bits for pitch decoding
166 : *-----------------------------------------------------------------*/
167 :
168 271810 : nBits = st->acelp_cfg.pitch_bits[i_subfr / L_SUBFR];
169 :
170 : /*------------------------------------------------------------*
171 : * first glottal impulse is in the 1st subframe
172 : *------------------------------------------------------------*/
173 :
174 271810 : 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 11141 : *T0 = 2 * L_SUBFR;
182 11141 : *T0_frac = 0;
183 11141 : *Jopt_flag = 0;
184 :
185 : /* set adaptive part of exciation for curent subframe to 0 */
186 11141 : set_f( &exc[i_subfr], 0, (int16_t) ( L_SUBFR + 1 ) );
187 :
188 11141 : set_f( &bwe_exc[i_subfr * HIBND_ACB_L_FAC], 0, (int16_t) ( L_SUBFR * HIBND_ACB_L_FAC ) );
189 : }
190 260669 : 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 32058 : if ( PIT_MIN > ( *position ) )
198 : {
199 28261 : pit_start = L_SUBFR - ( *position );
200 : }
201 : else
202 : {
203 3797 : pit_start = PIT_MIN;
204 : }
205 :
206 32058 : if ( pit_start < PIT_MIN )
207 : {
208 1721 : pit_start = PIT_MIN;
209 : }
210 :
211 32058 : pit_limit = 2 * pit_start + ( *position );
212 :
213 : /* 7 bit pitch DECODER */
214 32058 : index = get_next_indice( st, nBits );
215 :
216 32058 : *T0 = (int16_t) ( floor( pit_start + index / 2 ) );
217 32058 : *T0_frac = ( index - ( *T0 - pit_start ) * 2 ) * 2;
218 32058 : 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 32058 : pred_lt4( &exc[i_subfr], &exc[i_subfr], *T0, *T0_frac, L_SUBFR + 1, inter4_2, L_INTERPOL2, PIT_UP_SAMP );
222 :
223 32058 : offset = tbe_celp_exc_offset( *T0, *T0_frac );
224 5161338 : for ( i = 0; i < L_SUBFR * HIBND_ACB_L_FAC; i++ )
225 : {
226 5129280 : bwe_exc[i + i_subfr * HIBND_ACB_L_FAC] = bwe_exc[i + i_subfr * HIBND_ACB_L_FAC - offset];
227 : }
228 : }
229 228611 : 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 8632 : pit_start = 2 * L_SUBFR - ( *position );
238 :
239 8632 : index = get_next_indice( st, nBits );
240 :
241 8632 : *T0 = (int16_t) ( floor( pit_start + (int16_t) ( index / 2 ) ) );
242 8632 : *T0_frac = ( index - ( *T0 - pit_start ) * 2 ) * 2;
243 :
244 8632 : 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 8632 : pred_lt4( &exc[i_subfr], &exc[i_subfr], *T0, *T0_frac, L_SUBFR + 1, inter4_2, L_INTERPOL2, PIT_UP_SAMP );
248 :
249 8632 : offset = tbe_celp_exc_offset( *T0, *T0_frac );
250 1389752 : for ( i = 0; i < L_SUBFR * HIBND_ACB_L_FAC; i++ )
251 : {
252 1381120 : bwe_exc[i + i_subfr * HIBND_ACB_L_FAC] = bwe_exc[i + i_subfr * HIBND_ACB_L_FAC - offset];
253 : }
254 : }
255 219979 : 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 2509 : *T0 = 4 * L_SUBFR;
263 2509 : *T0_frac = 0;
264 2509 : *Jopt_flag = 0;
265 :
266 : /* set adaptive part of exciation for curent subframe to 0 */
267 2509 : set_f( &exc[i_subfr], 0, (int16_t) ( L_SUBFR + 1 ) );
268 :
269 2509 : set_f( &bwe_exc[i_subfr * HIBND_ACB_L_FAC], 0, (int16_t) ( L_SUBFR * HIBND_ACB_L_FAC ) );
270 : }
271 217470 : 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 2509 : pit_start = 3 * L_SUBFR - ( *position );
279 2509 : pit_limit = 2 * L_FRAME - PIT_MAX - 2 * ( *position ) - 2;
280 :
281 2509 : index = get_next_indice( st, nBits );
282 :
283 2509 : if ( index < ( pit_limit - pit_start ) * 2 )
284 : {
285 1996 : *T0 = (int16_t) ( floor( pit_start + ( index / 2 ) ) );
286 1996 : *T0_frac = ( index - ( ( *T0 ) - pit_start ) * 2 ) * 2;
287 : }
288 : else
289 : {
290 513 : *T0 = index + pit_limit - ( pit_limit - pit_start ) * 2;
291 513 : *T0_frac = 0;
292 : }
293 :
294 : /* biterror detection mechanism */
295 2509 : 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 2509 : pred_lt4( &exc[i_subfr], &exc[i_subfr], *T0, *T0_frac, L_SUBFR + 1, inter4_2, L_INTERPOL2, PIT_UP_SAMP );
304 :
305 2509 : offset = tbe_celp_exc_offset( *T0, *T0_frac );
306 403949 : for ( i = 0; i < L_SUBFR * HIBND_ACB_L_FAC; i++ )
307 : {
308 401440 : bwe_exc[i + i_subfr * HIBND_ACB_L_FAC] = bwe_exc[i + i_subfr * HIBND_ACB_L_FAC - offset];
309 : }
310 : }
311 214961 : 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 8632 : index = get_next_indice( st, nBits );
319 :
320 8632 : delta_pit_dec( 2, index, T0, T0_frac, *T0_min );
321 :
322 : /* Find the adaptive codebook vector. ACELP long-term prediction */
323 8632 : pred_lt4( &exc[i_subfr], &exc[i_subfr], *T0, *T0_frac, L_SUBFR + 1, inter4_2, L_INTERPOL2, PIT_UP_SAMP );
324 :
325 8632 : offset = tbe_celp_exc_offset( *T0, *T0_frac );
326 1389752 : for ( i = 0; i < L_SUBFR * HIBND_ACB_L_FAC; i++ )
327 : {
328 1381120 : 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 206329 : index = get_next_indice( st, nBits );
339 :
340 206329 : 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 206329 : pred_lt4( &exc[i_subfr], &exc[i_subfr], *T0, *T0_frac, L_SUBFR + 1, inter4_2, L_INTERPOL2, PIT_UP_SAMP );
344 :
345 206329 : offset = tbe_celp_exc_offset( *T0, *T0_frac );
346 33218969 : for ( i = 0; i < L_SUBFR * HIBND_ACB_L_FAC; i++ )
347 : {
348 33012640 : 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 271810 : if ( *Jopt_flag )
357 : {
358 258160 : 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 271810 : **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f; /* save subframe pitch values */
366 271810 : if ( ( tc_subfr >= 2 * L_SUBFR ) && ( i_subfr == 3 * L_SUBFR ) )
367 : {
368 15907 : ( *pt_pitch ) -= 3;
369 15907 : **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
370 15907 : ( *pt_pitch )++;
371 15907 : **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
372 15907 : ( *pt_pitch )++;
373 15907 : **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
374 15907 : ( *pt_pitch )++;
375 : }
376 255903 : else if ( ( tc_subfr == L_SUBFR ) && ( i_subfr == 2 * L_SUBFR ) )
377 : {
378 17469 : ( *pt_pitch ) -= 2;
379 17469 : **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
380 17469 : ( *pt_pitch )++;
381 17469 : **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
382 17469 : ( *pt_pitch )++;
383 : }
384 238434 : else if ( ( tc_subfr == TC_0_64 ) && ( i_subfr == L_SUBFR ) )
385 : {
386 32058 : ( *pt_pitch ) -= 1;
387 32058 : **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
388 32058 : ( *pt_pitch )++;
389 : }
390 206376 : else if ( ( tc_subfr == TC_0_128 ) && ( i_subfr == 2 * L_SUBFR ) )
391 : {
392 8632 : ( *pt_pitch ) -= 2;
393 8632 : **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
394 8632 : ( *pt_pitch )++;
395 8632 : **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
396 8632 : ( *pt_pitch )++;
397 : }
398 197744 : else if ( ( tc_subfr == TC_0_192 ) && ( i_subfr == 3 * L_SUBFR ) )
399 : {
400 2509 : ( *pt_pitch ) -= 3;
401 2509 : **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
402 2509 : ( *pt_pitch )++;
403 2509 : **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
404 2509 : ( *pt_pitch )++;
405 2509 : **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
406 2509 : ( *pt_pitch )++;
407 : }
408 : }
409 : else /* L_frame == L_FRAME16k */
410 : {
411 357957 : if ( i_subfr >= 2 * L_SUBFR )
412 : {
413 291922 : limit_flag = 1;
414 : }
415 :
416 357957 : if ( i_subfr - tc_subfr == L_SUBFR )
417 : {
418 110660 : 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 357957 : nBits = st->acelp_cfg.pitch_bits[i_subfr / L_SUBFR];
426 :
427 357957 : index = get_next_indice( st, nBits );
428 :
429 : /*-----------------------------------------------------------------*
430 : * Find adaptive part of excitation, encode pitch period
431 : *-----------------------------------------------------------------*/
432 :
433 357957 : if ( nBits == 10 )
434 : {
435 80712 : pit16k_Q_dec( index, nBits, limit_flag, T0, T0_frac, T0_min, T0_max, &st->BER_detect );
436 : }
437 277245 : 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 66035 : if ( index < ( PIT16k_FR2_TC0_2SUBFR - PIT16k_MIN ) * 4 )
446 : {
447 33387 : *T0 = PIT16k_MIN + ( index / 4 );
448 33387 : *T0_frac = index - ( *T0 - PIT16k_MIN ) * 4;
449 : }
450 : else
451 : {
452 32648 : index -= ( PIT16k_FR2_TC0_2SUBFR - PIT16k_MIN ) * 4;
453 32648 : *T0 = PIT16k_FR2_TC0_2SUBFR + ( index / 2 );
454 32648 : *T0_frac = index - ( *T0 - PIT16k_FR2_TC0_2SUBFR ) * 2;
455 32648 : ( *T0_frac ) *= 2;
456 : }
457 :
458 : /* biterror detection mechanism */
459 66035 : 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 211210 : else if ( nBits == 6 )
467 : {
468 211210 : delta_pit_dec( 4, index, T0, T0_frac, *T0_min );
469 : }
470 357957 : if ( nBits == 6 )
471 : {
472 211210 : 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 357957 : if ( ( i_subfr == L_SUBFR ) && ( *T0 == 2 * L_SUBFR ) )
481 : {
482 : /* no adaptive excitation in the second subframe */
483 13941 : set_f( &exc[i_subfr], 0, L_SUBFR + 1 );
484 :
485 13941 : get_next_indice( st, 1 ); /* this bit is actually not needed */
486 :
487 13941 : 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 344016 : pred_lt4( &exc[i_subfr], &exc[i_subfr], *T0, *T0_frac, L_SUBFR + 1, inter4_2, L_INTERPOL2, PIT_UP_SAMP );
493 :
494 44378064 : for ( i = 0; i < L_SUBFR * 2; i++ )
495 : {
496 44034048 : 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 344016 : lp_filt_exc_dec( st, MODE1, i_subfr, L_SUBFR, L_frame, st->acelp_cfg.ltf_mode, exc );
500 :
501 344016 : *Jopt_flag = 1;
502 : }
503 :
504 357957 : **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 357957 : if ( ( i_subfr - tc_subfr == L_SUBFR ) || ( tc_subfr == 0 && i_subfr == 2 * L_SUBFR ) )
511 : {
512 176695 : index = i_subfr / L_SUBFR;
513 176695 : ( *pt_pitch ) -= index;
514 :
515 504108 : for ( i = 0; i < index; i++ )
516 : {
517 327413 : **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
518 327413 : ( *pt_pitch )++;
519 : }
520 : }
521 : }
522 : }
523 :
524 1308333 : 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 289393 : 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 289393 : nBits = st->acelp_cfg.pitch_bits[i_subfr / L_SUBFR];
557 :
558 : /*----------------------------------------------------------------*
559 : * decode parameter T0 (pitch period)
560 : *----------------------------------------------------------------*/
561 :
562 289393 : if ( L_frame == L_FRAME )
563 : {
564 138632 : 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 60668 : *T0 = L_SUBFR;
567 60668 : *T0_frac = 0;
568 : }
569 77964 : else if ( ( tc_subfr == 3 * L_SUBFR ) )
570 : {
571 31601 : i = get_next_indice( st, nBits );
572 :
573 31601 : if ( nBits == 9 )
574 : {
575 31418 : abs_pit_dec( 4, i, 0, T0, T0_frac );
576 : }
577 : else
578 : {
579 183 : abs_pit_dec( 2, i, 0, T0, T0_frac );
580 : }
581 : }
582 : else
583 : {
584 46363 : i = get_next_indice( st, nBits );
585 :
586 46363 : if ( i == 0 )
587 : {
588 9812 : *T0 = L_SUBFR;
589 9812 : *T0_frac = 0;
590 : }
591 : else
592 : {
593 36551 : if ( tc_subfr == TC_0_0 )
594 : {
595 30456 : delta_pit_dec( 2, i, T0, T0_frac, PIT_MIN - 1 );
596 : }
597 : else
598 : {
599 6095 : delta_pit_dec( 0, i, T0, T0_frac, PIT_MIN - 1 );
600 : }
601 : }
602 : }
603 : }
604 : else /* L_frame == L_FRAME16k */
605 : {
606 150761 : i = get_next_indice( st, nBits );
607 :
608 150761 : if ( nBits == 10 )
609 : {
610 70049 : if ( i < ( PIT16k_FR2_EXTEND_10b - PIT16k_MIN_EXTEND ) * 4 )
611 : {
612 68114 : *T0 = PIT16k_MIN_EXTEND + ( i / 4 );
613 68114 : *T0_frac = i - ( ( *T0 - PIT16k_MIN_EXTEND ) * 4 );
614 : }
615 : else
616 : {
617 1935 : index = i - ( PIT16k_FR2_EXTEND_10b - PIT16k_MIN_EXTEND ) * 4;
618 1935 : *T0 = PIT16k_FR2_EXTEND_10b + ( index / 2 );
619 1935 : *T0_frac = index - ( *T0 - PIT16k_FR2_EXTEND_10b ) * 2;
620 1935 : ( *T0_frac ) *= 2;
621 : }
622 : }
623 80712 : else if ( nBits == 6 )
624 : {
625 80712 : *T0 = PIT16k_MIN + ( i / 2 );
626 80712 : *T0_frac = i - ( *T0 - PIT16k_MIN ) * 2;
627 80712 : *T0_frac *= 2;
628 : }
629 : }
630 :
631 : /*----------------------------------------------------------------*
632 : * decode other TC parameters
633 : *----------------------------------------------------------------*/
634 :
635 289393 : imp_shape = get_next_indice( st, 3 );
636 289393 : imp_pos = get_next_indice( st, 6 );
637 289393 : imp_sign = get_next_indice( st, 1 );
638 289393 : imp_gain = get_next_indice( st, 3 );
639 :
640 : /*----------------------------------------------------------------*
641 : * restore gain_trans
642 : * build glottal codebook contribution
643 : *----------------------------------------------------------------*/
644 :
645 289393 : gain_trans = tbl_gain_trans_tc[imp_gain];
646 :
647 289393 : if ( imp_sign == 0 )
648 : {
649 140919 : gain_trans *= -1;
650 : }
651 :
652 : /* build glottal codebook contribution */
653 289393 : set_f( &exc[i_subfr], 0, L_SUBFR );
654 :
655 5209074 : for ( i = ( imp_pos - L_IMPULSE2 ); i <= ( imp_pos + L_IMPULSE2 ); i++ )
656 : {
657 4919681 : if ( ( i >= 0 ) && ( i < L_SUBFR ) )
658 : {
659 4643435 : 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 289393 : pred_lt4_tc( exc, *T0, *T0_frac, inter4_2, imp_pos, i_subfr );
669 :
670 289393 : if ( st->hBWE_TD != NULL )
671 : {
672 289393 : if ( L_frame == L_FRAME )
673 : {
674 138632 : interp_code_5over2( &exc[i_subfr], &bwe_exc[i_subfr * HIBND_ACB_L_FAC], L_SUBFR );
675 : }
676 : else
677 : {
678 150761 : interp_code_4over2( &exc[i_subfr], &bwe_exc[i_subfr * 2], L_SUBFR );
679 : }
680 : }
681 :
682 289393 : *position = imp_pos + i_subfr;
683 :
684 289393 : return;
685 : }
686 :
687 : /*-------------------------------------------------------------------*
688 : * tc_classif()
689 : *
690 : * TC subframe classification decoding
691 : *-------------------------------------------------------------------*/
692 :
693 289393 : int16_t tc_classif(
694 : Decoder_State *st /* i/o: decoder state structure */
695 : )
696 : {
697 : int16_t tc_subfr, indice;
698 :
699 289393 : if ( st->L_frame == L_FRAME )
700 : {
701 138632 : if ( get_next_indice( st, 1 ) )
702 : {
703 30456 : tc_subfr = TC_0_0;
704 : }
705 : else
706 : {
707 108176 : if ( get_next_indice( st, 1 ) )
708 : {
709 43199 : tc_subfr = 0;
710 :
711 43199 : if ( get_next_indice( st, 1 ) )
712 : {
713 2509 : tc_subfr = TC_0_192;
714 : }
715 : else
716 : {
717 40690 : if ( get_next_indice( st, 1 ) )
718 : {
719 32058 : tc_subfr = TC_0_64;
720 : }
721 : else
722 : {
723 8632 : tc_subfr = TC_0_128;
724 : }
725 : }
726 : }
727 : else
728 : {
729 64977 : if ( get_next_indice( st, 1 ) )
730 : {
731 17469 : tc_subfr = L_SUBFR;
732 : }
733 : else
734 : {
735 47508 : if ( get_next_indice( st, 1 ) )
736 : {
737 15907 : tc_subfr = 2 * L_SUBFR;
738 : }
739 : else
740 : {
741 31601 : tc_subfr = 3 * L_SUBFR;
742 : }
743 : }
744 : }
745 : }
746 : }
747 : else /* L_frame == L_FRAME16k */
748 : {
749 150761 : indice = get_next_indice( st, 2 );
750 :
751 150761 : if ( indice < 3 )
752 : {
753 100550 : tc_subfr = indice * L_SUBFR;
754 : }
755 : else
756 : {
757 50211 : if ( get_next_indice( st, 1 ) == 0 )
758 : {
759 10110 : tc_subfr = 3 * L_SUBFR;
760 : }
761 : else
762 : {
763 40101 : tc_subfr = 4 * L_SUBFR;
764 : }
765 : }
766 : }
767 :
768 289393 : return ( tc_subfr );
769 : }
|