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