Line data Source code
1 : /*====================================================================================
2 : EVS Codec 3GPP TS26.452 Aug 12, 2021. Version 16.3.0
3 : ====================================================================================*/
4 : #include <stdint.h>
5 : #include "options.h" /* Compilation switches */
6 : #include "cnst.h" /* Common constants */
7 : #include "rom_com.h" /* Static table prototypes */
8 : #include "prot_fx.h" /* Function prototypes */
9 : #include "stl.h"
10 :
11 : /*----------------------------------------------------------------------*
12 : * Local function prototypes
13 : *----------------------------------------------------------------------*/
14 : static void tc_dec_fx( Decoder_State *st_fx, const Word16 L_frame, Word16 exc[], Word16 *T0, Word16 *T0_frac, const Word16 i_subfr, const Word16 tc_subfr, Word16 *position, Word16 bwe_exc[], Word16 *Q_exc );
15 : /*======================================================================*/
16 : /* FUNCTION : transition_dec_fx() */
17 : /*----------------------------------------------------------------------*/
18 : /* PURPOSE : Principal function for TC decoding */
19 : /* */
20 : /*----------------------------------------------------------------------*/
21 : /* GLOBAL INPUT ARGUMENTS : */
22 : /* _ (Word16) L_frame : length of the frame Q0 */
23 : /* _ (Word16[]) pitch_buf_fx : floating pitch values for each subframe Q6*/
24 : /* _ (Word16[]) voice_factors_fx: frame error rate Q15 */
25 : /*-----------------------------------------------------------------------*/
26 : /* OUTPUT ARGUMENTS : */
27 : /* _ (Word16[]) exc_fx : adapt. excitation exc (Q0) */
28 : /* _ (Word16[]) exc2_fx : adapt. excitation/total exc (Q0) */
29 : /*-----------------------------------------------------------------------*/
30 :
31 :
32 : /*-----------------------------------------------------------------------*/
33 : /* RETURN ARGUMENTS : */
34 : /* _ None */
35 : /*=======================================================================*/
36 :
37 56861 : void transition_dec_fx(
38 : Decoder_State *st_fx, /* i/o: decoder state structure */
39 : const Word16 Opt_AMR_WB, /* i : flag indicating AMR-WB IO mode Q0 */
40 : const Word16 L_frame, /* i : length of the frame Q0 */
41 : const Word16 i_subfr, /* i : subframe index Q0 */
42 : const Word16 tc_subfr, /* i : TC subframe index Q0 */
43 : Word16 *Jopt_flag, /* i : joint optimization flag Q0*/
44 : Word16 *exc, /* o : excitation signal Q0 */
45 : Word16 *T0, /* o : close loop integer pitch Q0 */
46 : Word16 *T0_frac, /* o : close loop fractional part of the pitch Q0*/
47 : Word16 *T0_min, /* i/o: delta search min for sf 2 & 4 Q0 */
48 : Word16 *T0_max, /* i/o: delta search max for sf 2 & 4 Q0 */
49 : Word16 **pt_pitch, /* o : floating pitch values Q6 */
50 : Word16 *position, /* i/o: first glottal impulse position in frame Q0*/
51 : Word16 *bwe_exc, /* o : excitation for SWB TBE Q_exc */
52 : Word16 *Q_exc /*i/o : scaling of excitation */
53 : )
54 : {
55 : Word16 pit_flag, pit_start, pit_limit, index, nBits;
56 : Word16 i, offset, temp, tmp;
57 : Word16 limit_flag;
58 : Word16 i_subfridx;
59 : (void) ( Opt_AMR_WB );
60 :
61 56861 : i_subfridx = i_subfr / L_SUBFR; /*i_subfr / L_SUBFR*/
62 : /* Set limit_flag to 0 for restrained limits, and 1 for extended limits */
63 56861 : limit_flag = 0;
64 56861 : move16();
65 : /*---------------------------------------------------------------------*
66 : * zero adaptive contribution (glottal shape codebook search not
67 : * in first subframe(s) )
68 : *---------------------------------------------------------------------*/
69 56861 : test();
70 56861 : IF( GT_16( tc_subfr, add( i_subfr, TC_0_192 ) ) )
71 : {
72 16597 : set16_fx( &exc[i_subfr], 0, L_SUBFR );
73 :
74 16597 : IF( EQ_16( L_frame, L_FRAME ) )
75 : {
76 6425 : set16_fx( &bwe_exc[i_subfr * HIBND_ACB_L_FAC], 0, (Word16) ( L_SUBFR * HIBND_ACB_L_FAC ) ); /* set past excitation buffer to 0 */
77 : }
78 : ELSE
79 : {
80 10172 : set16_fx( &bwe_exc[i_subfr * 2], 0, (Word16) ( L_SUBFR * 2 ) ); /* set past excitation buffer to 0 */
81 : }
82 :
83 16597 : *T0 = L_SUBFR;
84 16597 : move16();
85 16597 : *T0_frac = 0;
86 16597 : move16();
87 16597 : **pt_pitch = L_SUBFR_Q6;
88 16597 : move16();
89 : }
90 :
91 : /*---------------------------------------------------------------------*
92 : * glottal shape codebook search
93 : *---------------------------------------------------------------------*/
94 :
95 40264 : ELSE IF( ( ( GE_16( tc_subfr, i_subfr ) ) && ( LE_16( sub( tc_subfr, i_subfr ), TC_0_192 ) ) ) )
96 : {
97 12668 : set16_fx( exc - L_EXC_MEM, 0, L_EXC_MEM ); /* set past excitation buffer to 0 */
98 :
99 12668 : IF( EQ_16( L_frame, L_FRAME ) )
100 : {
101 6479 : set16_fx( bwe_exc - PIT_MAX * HIBND_ACB_L_FAC, 0, PIT_MAX * HIBND_ACB_L_FAC ); /* set past excitation buffer to 0 */
102 : }
103 : ELSE
104 : {
105 6189 : set16_fx( bwe_exc - PIT16k_MAX * 2, 0, PIT16k_MAX * 2 ); /* set past excitation buffer to 0 */
106 : }
107 :
108 : /* glottal shape codebook contribution construction */
109 12668 : tc_dec_fx( st_fx, L_frame, exc, T0, T0_frac, i_subfr, tc_subfr, position, bwe_exc, Q_exc );
110 12668 : **pt_pitch = shl( add( shl( *T0, 2 ), *T0_frac ), 4 ); // Q6
111 12668 : move16(); /* save subframe pitch values Q6 */
112 :
113 12668 : *Jopt_flag = 1;
114 12668 : move16();
115 : }
116 :
117 : /*---------------------------------------------------------------------*
118 : * Regular ACELP Decoding using GENERIC type decoder
119 : * (all subframes following subframe with glottal shape codebook seach)
120 : * - search the position of the 2nd glottal impulse in case that the first
121 : * one is in the 1st subframe (different adaptive contribution
122 : * construction and the pitch period coding is used)
123 : *---------------------------------------------------------------------*/
124 :
125 27596 : ELSE IF( LT_16( tc_subfr, i_subfr ) )
126 : {
127 27596 : IF( EQ_16( L_frame, L_FRAME ) )
128 : {
129 13012 : *Jopt_flag = 1;
130 13012 : move16();
131 13012 : test();
132 13012 : IF( ( GE_16( sub( i_subfr, tc_subfr ), L_SUBFR ) ) && ( LE_16( sub( i_subfr, tc_subfr ), L_SUBFR + TC_0_192 ) ) )
133 : {
134 1444 : pit_flag = 0;
135 1444 : move16();
136 : }
137 : ELSE
138 : {
139 11568 : pit_flag = L_SUBFR;
140 11568 : move16();
141 : }
142 13012 : IF( EQ_16( tc_subfr, TC_0_0 ) )
143 : {
144 4596 : IF( EQ_16( i_subfr, L_SUBFR ) )
145 : {
146 1532 : limit_T0_fx( L_FRAME, 8, pit_flag, limit_flag, *T0, 0, T0_min, T0_max );
147 : }
148 4596 : pit_flag = 1;
149 4596 : move16();
150 : }
151 :
152 : /*-----------------------------------------------------------------*
153 : * get number of bits for pitch decoding
154 : *-----------------------------------------------------------------*/
155 13012 : nBits = st_fx->acelp_cfg.pitch_bits[i_subfridx];
156 13012 : move16();
157 :
158 : /*------------------------------------------------------------*
159 : * first glottal impulse is in the 1st subframe
160 : *------------------------------------------------------------*/
161 13012 : test();
162 13012 : test();
163 13012 : test();
164 13012 : test();
165 13012 : test();
166 13012 : test();
167 13012 : IF( ( EQ_16( i_subfr, L_SUBFR ) ) && ( GE_16( tc_subfr, TC_0_128 ) ) )
168 : {
169 : /*--------------------------------------------------------*
170 : * second glottal impulse is in the 3rd or 4th subframe
171 : * - build exc[] in 2nd subframe
172 : *--------------------------------------------------------*/
173 :
174 807 : *T0 = 2 * L_SUBFR;
175 807 : move16();
176 807 : *T0_frac = 0;
177 807 : move16();
178 807 : *Jopt_flag = 0;
179 807 : move16();
180 :
181 : /* set adaptive part of exciation for curent subframe to 0 */
182 807 : set16_fx( &exc[i_subfr], 0, (Word16) ( L_SUBFR + 1 ) );
183 807 : set16_fx( &bwe_exc[i_subfr * HIBND_ACB_L_FAC], 0, (Word16) ( L_SUBFR * HIBND_ACB_L_FAC ) );
184 : }
185 12205 : ELSE IF( ( EQ_16( i_subfr, L_SUBFR ) ) && ( EQ_16( tc_subfr, TC_0_64 ) ) )
186 : {
187 : /*--------------------------------------------------------*
188 : * second glottal impulse is in the 2nd subframe,
189 : * - build exc[] in 2nd subframe
190 : *--------------------------------------------------------*/
191 1180 : pit_start = PIT_MIN;
192 1180 : move16();
193 1180 : if ( GT_16( PIT_MIN, ( *position ) ) )
194 : {
195 893 : pit_start = sub( L_SUBFR, *position );
196 : }
197 :
198 1180 : pit_start = s_max( pit_start, PIT_MIN );
199 1180 : pit_limit = add( shl( pit_start, 1 ), *position );
200 :
201 : /* 7 bit pitch DECODER */
202 1180 : index = (Word16) get_next_indice_fx( st_fx, nBits );
203 :
204 1180 : *T0 = add( pit_start, shr( ( index ), 1 ) );
205 1180 : move16();
206 1180 : *T0_frac = shl( sub( ( index ), shl( sub( *T0, pit_start ), 1 ) ), 1 );
207 1180 : move16();
208 :
209 1180 : limit_T0_fx( L_FRAME, 8, pit_flag, limit_flag, *T0, 0, T0_min, T0_max ); /* find T0_min and T0_max */
210 :
211 : /* Find the adaptive codebook vector - ACELP long-term prediction */
212 1180 : pred_lt4( &exc[i_subfr], &exc[i_subfr], *T0, *T0_frac, L_SUBFR + 1, pitch_inter4_2, L_INTERPOL2, PIT_UP_SAMP );
213 1180 : offset = tbe_celp_exc_offset( *T0, *T0_frac, L_frame );
214 :
215 1180 : move16();
216 1180 : move16(); /* penality for 2 ptrs initialization */
217 :
218 189980 : FOR( i = 0; i < L_SUBFR * HIBND_ACB_L_FAC; i++ )
219 : {
220 188800 : bwe_exc[add( i, i_subfr * HIBND_ACB_L_FAC )] = bwe_exc[sub( add( i, i_subfr * HIBND_ACB_L_FAC ), offset )];
221 188800 : move16();
222 : }
223 : }
224 11025 : ELSE IF( ( EQ_16( i_subfr, 2 * L_SUBFR ) ) && ( EQ_16( tc_subfr, TC_0_128 ) ) )
225 : {
226 : /*--------------------------------------------------------*
227 : * second glottal impulse is in the 3rd subframe
228 : * - build exc[] in 3rd subframe
229 : *--------------------------------------------------------*/
230 :
231 : /* 7bit pitch DECODER */
232 619 : pit_start = sub( 2 * L_SUBFR, ( *position ) );
233 619 : index = (Word16) get_next_indice_fx( st_fx, nBits );
234 :
235 619 : *T0 = add( pit_start, shr( ( index ), 1 ) );
236 619 : move16();
237 619 : *T0_frac = shl( sub( ( index ), shl( sub( *T0, pit_start ), 1 ) ), 1 );
238 619 : move16();
239 619 : limit_T0_fx( L_FRAME, 8, pit_flag, limit_flag, *T0, 0, T0_min, T0_max ); /* find T0_min and T0_max */
240 :
241 : /* Find the adaptive codebook vector. ACELP long-term prediction */
242 619 : pred_lt4( &exc[i_subfr], &exc[i_subfr], *T0, *T0_frac, L_SUBFR + 1, pitch_inter4_2, L_INTERPOL2, PIT_UP_SAMP );
243 619 : offset = tbe_celp_exc_offset( *T0, *T0_frac, L_frame );
244 619 : move16();
245 619 : move16(); /* penality for 2 ptrs initialization */
246 :
247 99659 : FOR( i = 0; i < L_SUBFR * HIBND_ACB_L_FAC; i++ )
248 : {
249 99040 : bwe_exc[add( i, i_subfr * HIBND_ACB_L_FAC )] = bwe_exc[sub( add( i, i_subfr * HIBND_ACB_L_FAC ), offset )];
250 99040 : move16();
251 : }
252 : }
253 10406 : ELSE IF( ( EQ_16( i_subfr, 2 * L_SUBFR ) ) && ( EQ_16( tc_subfr, TC_0_192 ) ) )
254 : {
255 : /*--------------------------------------------------------*
256 : * second glottal impulse is in the 4th subframe
257 : * - build exc[] in 3rd subframe
258 : *--------------------------------------------------------*/
259 :
260 188 : *T0 = 4 * L_SUBFR;
261 188 : move16();
262 188 : *T0_frac = 0;
263 188 : move16();
264 188 : *Jopt_flag = 0;
265 188 : move16();
266 :
267 : /* set adaptive part of exciation for curent subframe to 0 */
268 188 : set16_fx( &exc[i_subfr], 0, (Word16) ( L_SUBFR + 1 ) );
269 188 : set16_fx( &bwe_exc[i_subfr * HIBND_ACB_L_FAC], 0, (Word16) ( L_SUBFR * HIBND_ACB_L_FAC ) );
270 : }
271 10218 : ELSE IF( ( EQ_16( i_subfr, 3 * L_SUBFR ) ) && ( EQ_16( tc_subfr, TC_0_192 ) ) )
272 : {
273 : /*--------------------------------------------------------*
274 : * second glottal impulse is in the 4th subframe
275 : * - build exc[] in 4th subframe
276 : *--------------------------------------------------------*/
277 188 : pit_start = sub( 3 * L_SUBFR, ( *position ) );
278 188 : pit_limit = sub( 2 * L_FRAME - PIT_MAX, add( shl( ( *position ), 1 ), 2 ) );
279 :
280 :
281 188 : index = (Word16) get_next_indice_fx( st_fx, nBits );
282 :
283 188 : IF( LT_16( index, shl( sub( pit_limit, pit_start ), 1 ) ) )
284 : {
285 138 : *T0 = add( pit_start, shr( index, 1 ) );
286 138 : move16();
287 138 : *T0_frac = shl( sub( index, shl( sub( ( *T0 ), pit_start ), 1 ) ), 1 );
288 138 : move16();
289 : }
290 : ELSE
291 : {
292 50 : *T0 = add( index, sub( pit_limit, shl( sub( pit_limit, pit_start ), 1 ) ) );
293 50 : move16();
294 :
295 50 : *T0_frac = 0;
296 50 : move16();
297 : }
298 :
299 : /* biterror detection mechanism */
300 188 : IF( GT_16( add( shl( *T0, 2 ), *T0_frac ), add( ( PIT_MAX << 2 ), 2 ) ) )
301 : {
302 0 : *T0 = L_SUBFR;
303 0 : move16();
304 0 : *T0_frac = 0;
305 0 : move16();
306 0 : st_fx->BER_detect = 1;
307 0 : move16();
308 : }
309 :
310 : /* Find the adaptive codebook vector. ACELP long-term prediction */
311 188 : pred_lt4( &exc[i_subfr], &exc[i_subfr], *T0, *T0_frac, L_SUBFR + 1, pitch_inter4_2, L_INTERPOL2, PIT_UP_SAMP );
312 188 : offset = tbe_celp_exc_offset( *T0, *T0_frac, L_frame );
313 188 : move16();
314 188 : move16(); /* penality for 2 ptrs initialization */
315 30268 : FOR( i = 0; i < L_SUBFR * HIBND_ACB_L_FAC; i++ )
316 : {
317 30080 : bwe_exc[add( i, i_subfr * HIBND_ACB_L_FAC )] = bwe_exc[sub( add( i, i_subfr * HIBND_ACB_L_FAC ), offset )];
318 30080 : move16();
319 : }
320 : }
321 10030 : ELSE IF( ( EQ_16( i_subfr, 3 * L_SUBFR ) ) && ( EQ_16( tc_subfr, TC_0_128 ) ) )
322 : {
323 : /*--------------------------------------------------------*
324 : * second glottal impulse in the 3rd subframe
325 : * build exc[] in 4th subframe
326 : *--------------------------------------------------------*/
327 :
328 :
329 619 : index = (Word16) get_next_indice_fx( st_fx, nBits );
330 619 : delta_pit_dec_fx( 2, index, T0, T0_frac, *T0_min );
331 :
332 : /* Find the adaptive codebook vector. ACELP long-term prediction */
333 619 : pred_lt4( &exc[i_subfr], &exc[i_subfr], *T0, *T0_frac, L_SUBFR + 1, pitch_inter4_2, L_INTERPOL2, PIT_UP_SAMP );
334 619 : offset = tbe_celp_exc_offset( *T0, *T0_frac, L_frame );
335 619 : move16();
336 619 : move16(); /* penality for 2 ptrs initialization */
337 99659 : FOR( i = 0; i < L_SUBFR * HIBND_ACB_L_FAC; i++ )
338 : {
339 99040 : bwe_exc[add( i, i_subfr * HIBND_ACB_L_FAC )] = bwe_exc[sub( add( i, i_subfr * HIBND_ACB_L_FAC ), offset )];
340 99040 : move16();
341 : }
342 : }
343 : /*------------------------------------------------------------*
344 : * first glottal impulse is NOT in the 1st subframe,
345 : * or two impulses are in the 1st subframe
346 : *------------------------------------------------------------*/
347 : ELSE
348 : {
349 9411 : index = (Word16) get_next_indice_fx( st_fx, nBits );
350 :
351 9411 : pit_Q_dec_fx( 0, index, nBits, 8, pit_flag, limit_flag, T0, T0_frac, T0_min, T0_max, &st_fx->BER_detect );
352 :
353 : /* Find the adaptive codebook vector */
354 9411 : pred_lt4( &exc[i_subfr], &exc[i_subfr], *T0, *T0_frac, L_SUBFR + 1, pitch_inter4_2, L_INTERPOL2, PIT_UP_SAMP );
355 9411 : offset = tbe_celp_exc_offset( *T0, *T0_frac, L_frame );
356 9411 : move16();
357 9411 : move16(); /* penality for 2 ptrs initialization */
358 1515171 : FOR( i = 0; i < L_SUBFR * HIBND_ACB_L_FAC; i++ )
359 : {
360 1505760 : bwe_exc[add( i, i_subfr * HIBND_ACB_L_FAC )] = bwe_exc[sub( add( i, i_subfr * HIBND_ACB_L_FAC ), offset )];
361 1505760 : move16();
362 : }
363 : }
364 :
365 : /*-----------------------------------------------------------------*
366 : * LP filtering of the adaptive excitation (if non-zero)
367 : *-----------------------------------------------------------------*/
368 13012 : IF( *Jopt_flag )
369 : {
370 12017 : lp_filt_exc_dec_fx( st_fx, MODE1, i_subfr, L_SUBFR, L_frame, st_fx->acelp_cfg.ltf_mode, exc );
371 : }
372 :
373 : /*---------------------------------------------------------------------*
374 : * fill the pitch buffer - needed for post-processing and FEC_clas_estim()
375 : *---------------------------------------------------------------------*/
376 :
377 13012 : **pt_pitch = shl( add( shl( *T0, 2 ), *T0_frac ), 4 );
378 13012 : move16(); /* save subframe pitch values Q6 */
379 :
380 13012 : test();
381 13012 : test();
382 13012 : test();
383 13012 : test();
384 13012 : test();
385 13012 : IF( ( GE_16( tc_subfr, 2 * L_SUBFR ) ) && ( EQ_16( i_subfr, 3 * L_SUBFR ) ) )
386 : {
387 433 : tmp = shl( add( shl( *T0, 2 ), *T0_frac ), 4 );
388 433 : ( *pt_pitch ) -= 3;
389 433 : move16();
390 433 : **pt_pitch = tmp;
391 433 : move16(); /*Q6*/
392 433 : ( *pt_pitch )++;
393 433 : **pt_pitch = tmp;
394 433 : move16(); /*Q6*/
395 433 : ( *pt_pitch )++;
396 433 : move16();
397 433 : **pt_pitch = tmp;
398 433 : move16(); /*Q6*/
399 433 : ( *pt_pitch )++;
400 433 : move16();
401 : }
402 12579 : ELSE IF( ( EQ_16( tc_subfr, L_SUBFR ) ) && ( EQ_16( i_subfr, 2 * L_SUBFR ) ) )
403 : {
404 1011 : tmp = shl( add( shl( *T0, 2 ), *T0_frac ), 4 );
405 1011 : ( *pt_pitch ) -= 2;
406 1011 : move16();
407 1011 : **pt_pitch = tmp;
408 1011 : move16(); /*Q6*/
409 1011 : ( *pt_pitch )++;
410 1011 : **pt_pitch = tmp;
411 1011 : move16(); /*Q6*/
412 1011 : ( *pt_pitch )++;
413 1011 : move16();
414 : }
415 11568 : ELSE IF( ( EQ_16( tc_subfr, TC_0_64 ) ) && ( EQ_16( i_subfr, L_SUBFR ) ) )
416 : {
417 1180 : tmp = shl( add( shl( *T0, 2 ), *T0_frac ), 4 );
418 1180 : ( *pt_pitch ) -= 1;
419 1180 : move16();
420 1180 : **pt_pitch = tmp;
421 1180 : move16(); /*Q6*/
422 1180 : ( *pt_pitch )++;
423 : }
424 10388 : ELSE IF( ( EQ_16( tc_subfr, TC_0_128 ) ) && ( EQ_16( i_subfr, 2 * L_SUBFR ) ) )
425 : {
426 619 : tmp = shl( add( shl( *T0, 2 ), *T0_frac ), 4 );
427 619 : ( *pt_pitch ) -= 2;
428 619 : move16();
429 619 : **pt_pitch = tmp;
430 619 : move16(); /*Q6*/
431 619 : ( *pt_pitch )++;
432 619 : **pt_pitch = tmp;
433 619 : move16(); /*Q6*/
434 619 : ( *pt_pitch )++;
435 619 : move16();
436 : }
437 9769 : ELSE IF( ( EQ_16( tc_subfr, TC_0_192 ) ) && ( EQ_16( i_subfr, 3 * L_SUBFR ) ) )
438 : {
439 188 : tmp = shl( add( shl( *T0, 2 ), *T0_frac ), 4 );
440 188 : ( *pt_pitch ) -= 3;
441 188 : move16();
442 188 : **pt_pitch = tmp;
443 188 : move16(); /*Q6*/
444 188 : ( *pt_pitch )++;
445 188 : **pt_pitch = tmp;
446 188 : move16(); /*Q6*/
447 188 : ( *pt_pitch )++;
448 188 : move16();
449 188 : **pt_pitch = tmp;
450 188 : move16(); /*Q6*/
451 188 : ( *pt_pitch )++;
452 188 : move16();
453 : }
454 : }
455 : ELSE /* L_frame == L_FRAME16k */
456 : {
457 14584 : if ( GE_16( i_subfr, 2 * L_SUBFR ) )
458 : {
459 12006 : limit_flag = 1;
460 12006 : move16();
461 : }
462 :
463 14584 : IF( EQ_16( sub( i_subfr, tc_subfr ), L_SUBFR ) )
464 : {
465 4361 : limit_T0_fx( L_FRAME16k, 8, 0, limit_flag, *T0, *T0_frac, T0_min, T0_max );
466 : }
467 :
468 : /*-----------------------------------------------------------------*
469 : * get number of bits and index for pitch decoding
470 : *-----------------------------------------------------------------*/
471 14584 : nBits = st_fx->acelp_cfg.pitch_bits[i_subfridx];
472 14584 : move16();
473 :
474 14584 : index = (Word16) get_next_indice_fx( st_fx, nBits );
475 :
476 : /*-----------------------------------------------------------------*
477 : * Find adaptive part of excitation, encode pitch period
478 : *-----------------------------------------------------------------*/
479 :
480 14584 : IF( EQ_16( nBits, 10 ) )
481 : {
482 3513 : pit16k_Q_dec_fx( index, nBits, limit_flag, T0, T0_frac, T0_min, T0_max, &st_fx->BER_detect );
483 : }
484 11071 : ELSE IF( EQ_16( nBits, 8 ) ) /* tc_subfr==0 && i_subfr==L_SUBFR */
485 : {
486 : /*-----------------------------------------------------------------------------*
487 : * The pitch range is encoded absolutely with 8 bits and is divided as follows:
488 : * PIT16k_MIN to PIT16k_FR2_TC0_2SUBFR-1 resolution 1/4 (frac = 0,1,2 or 3)
489 : * PIT16k_FR2_TC0_2SUBFR to 2*L_SUBFR resolution 1/2 (frac = 0 or 2)
490 : *-----------------------------------------------------------------------------*/
491 :
492 2578 : IF( LT_16( index, ( PIT16k_FR2_TC0_2SUBFR - PIT16k_MIN ) * 4 ) ) /*(PIT16k_FR2_TC0_2SUBFR-PIT16k_MIN)*4*/
493 : {
494 1502 : *T0 = add( PIT16k_MIN, shr( index, 2 ) );
495 1502 : move16();
496 1502 : temp = shl( sub( *T0, PIT16k_MIN ), 2 );
497 1502 : move16();
498 1502 : *T0_frac = sub( index, temp );
499 1502 : move16();
500 : }
501 : ELSE
502 : {
503 1076 : index = sub( index, ( PIT16k_FR2_TC0_2SUBFR - PIT16k_MIN ) * 4 ); /* (PIT16k_FR2_TC0_2SUBFR-PIT16k_MIN)*4 */
504 1076 : *T0 = add( PIT16k_FR2_TC0_2SUBFR, shr( index, 1 ) );
505 1076 : move16();
506 1076 : temp = shl( sub( *T0, PIT16k_FR2_TC0_2SUBFR ), 1 );
507 1076 : move16();
508 1076 : *T0_frac = shl( sub( index, temp ), 1 );
509 1076 : move16();
510 : }
511 :
512 : /* biterror detection mechanism */
513 2578 : IF( GT_16( add( shl( *T0, 2 ), *T0_frac ), ( ( 2 * L_SUBFR ) << 2 ) ) )
514 : {
515 0 : *T0 = L_SUBFR;
516 0 : move16();
517 0 : *T0_frac = 0;
518 0 : move16();
519 0 : st_fx->BER_detect = 1;
520 0 : move16();
521 : }
522 : }
523 8493 : ELSE IF( EQ_16( nBits, 6 ) )
524 : {
525 8493 : delta_pit_dec_fx( 4, index, T0, T0_frac, *T0_min );
526 : }
527 14584 : IF( EQ_16( nBits, 6 ) )
528 : {
529 8493 : limit_T0_fx( L_FRAME16k, 8, L_SUBFR, limit_flag, *T0, *T0_frac, T0_min, T0_max ); /* find T0_min and T0_max */
530 : }
531 :
532 : /*-----------------------------------------------------------------*
533 : * - find the adaptive codebook vector
534 : * - LP filtering of the adaptive excitation (if non-zero)
535 : *-----------------------------------------------------------------*/
536 14584 : test();
537 14584 : IF( ( EQ_16( i_subfr, L_SUBFR ) ) && ( EQ_16( *T0, 2 * L_SUBFR ) ) )
538 : {
539 : /* no adaptive excitation in the second subframe */
540 436 : set16_fx( &exc[i_subfr], 0, L_SUBFR + 1 );
541 436 : get_next_indice_fx( st_fx, 1 ); /* this bit is actually not needed */
542 436 : set16_fx( &bwe_exc[i_subfr * 2], 0, L_SUBFR * 2 );
543 : }
544 : ELSE
545 : {
546 : /* Find the adaptive codebook vector - ACELP long-term prediction */
547 14148 : pred_lt4( &exc[i_subfr], &exc[i_subfr], *T0, *T0_frac, L_SUBFR + 1, pitch_inter4_2, L_INTERPOL2, PIT_UP_SAMP );
548 14148 : offset = tbe_celp_exc_offset( *T0, *T0_frac, L_frame );
549 14148 : move16();
550 14148 : move16(); /* penalty for 2 ptrs initialization */
551 1825092 : FOR( i = 0; i < L_SUBFR * 2; i++ )
552 : {
553 : /* bwe_exc[i + i_subfr * 2] = bwe_exc[i + i_subfr * 2 - *T0 * 2 */
554 : /* - (int) ((float) *T0_frac * 0.5f + 4 + 0.5f) + 4];*/
555 1810944 : bwe_exc[add( i, i_subfr * 2 )] = bwe_exc[sub( add( i, i_subfr * 2 ), offset )];
556 : }
557 :
558 14148 : lp_filt_exc_dec_fx( st_fx, MODE1, i_subfr, L_SUBFR, L_frame, st_fx->acelp_cfg.ltf_mode, exc );
559 14148 : *Jopt_flag = 1;
560 14148 : move16();
561 : }
562 :
563 14584 : **pt_pitch = shl( add( shl( *T0, 2 ), *T0_frac ), 4 );
564 14584 : move16(); /* save subframe pitch values Q6 */
565 :
566 : /*---------------------------------------------------------------------*
567 : * fill the pitch buffer - needed for post-processing and FEC_clas_estim()
568 : *---------------------------------------------------------------------*/
569 14584 : test();
570 14584 : test();
571 14584 : IF( ( EQ_16( sub( i_subfr, tc_subfr ), L_SUBFR ) ) || ( tc_subfr == 0 && EQ_16( i_subfr, 2 * L_SUBFR ) ) )
572 : {
573 : /*index = i_subfr/L_SUBFR;*/
574 6939 : index = shr( i_subfr, 6 );
575 :
576 : /*Ptr */
577 6939 : ( *pt_pitch ) -= index;
578 6939 : move16();
579 :
580 19316 : FOR( i = 0; i < index; i++ )
581 : {
582 12377 : **pt_pitch = shl( add( shl( *T0, 2 ), *T0_frac ), 4 );
583 12377 : move16(); /* save subframe pitch values Q6 */
584 :
585 12377 : ( *pt_pitch )++;
586 : }
587 : }
588 : }
589 : }
590 :
591 56861 : return;
592 : }
593 :
594 :
595 : /*======================================================================*/
596 : /* FUNCTION : tc_dec_fx() */
597 : /*----------------------------------------------------------------------*/
598 : /* PURPOSE : Principal function for TC decoding */
599 : /* * Principal function for TC decoding. */
600 : /* * - constructs glottal codebook contribution */
601 : /* * - uses pitch sharpening */
602 : /* * - uses gain_trans */
603 : /*----------------------------------------------------------------------*/
604 : /* GLOBAL INPUT ARGUMENTS : */
605 : /* _ (Word16) L_frame : length of the frame Q0 */
606 : /*----------------------------------------------------------------------*/
607 : /* OUTPUT ARGUMENTS : */
608 : /* _ (Word16[]) exc_fx : adapt. excitation exc (Q0) */
609 : /* _ (Word16[]) exc2_fx : adapt. excitation/total exc (Q0) */
610 : /*----------------------------------------------------------------------*/
611 :
612 :
613 : /*----------------------------------------------------------------------*/
614 : /* RETURN ARGUMENTS : */
615 : /* _ None */
616 : /*======================================================================*/
617 :
618 12668 : static void tc_dec_fx(
619 : Decoder_State *st_fx, /* i/o: decoder state structure */
620 : const Word16 L_frame, /* i : length of the frame Q0*/
621 : Word16 exc[], /* o : glottal codebook contribution Q0*/
622 : Word16 *T0, /* o : close-loop pitch period Q0 */
623 : Word16 *T0_frac, /* o : close-loop pitch period - fractional part Q0 */
624 : const Word16 i_subfr, /* i : subframe index Q0*/
625 : const Word16 tc_subfr, /* i : TC subframe index Q0 */
626 : Word16 *position, /* o : first glottal impulse position in frame Q0*/
627 : Word16 bwe_exc[], /* o : excitation for SWB TBE Q_exc */
628 : Word16 *Q_exc /*i/o : scaling of excitation */
629 : )
630 : {
631 : Word16 i, imp_shape, imp_pos, imp_sign, imp_gain, nBits;
632 : Word16 gain_trans, temp;
633 : const Word16 *pt_shape;
634 : Word16 j, sc;
635 : Word16 tempS;
636 : Word16 index;
637 : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
638 12668 : Flag Overflow = 0;
639 12668 : move32();
640 : #endif
641 : /*----------------------------------------------------------------*
642 : * find the number of bits
643 : *----------------------------------------------------------------*/
644 12668 : nBits = st_fx->acelp_cfg.pitch_bits[shr( i_subfr, 6 )];
645 12668 : move16();
646 : /*----------------------------------------------------------------*
647 : * decode parameter T0 (pitch period)
648 : *----------------------------------------------------------------*/
649 12668 : IF( EQ_16( L_frame, L_FRAME ) )
650 : {
651 6479 : test();
652 6479 : test();
653 6479 : test();
654 6479 : test();
655 6479 : test();
656 6479 : IF( ( ( i_subfr == 0 ) && ( ( tc_subfr == 0 ) || ( EQ_16( tc_subfr, TC_0_64 ) ) || ( EQ_16( tc_subfr, TC_0_128 ) ) || ( EQ_16( tc_subfr, TC_0_192 ) ) ) ) || ( EQ_16( tc_subfr, L_SUBFR ) ) )
657 : {
658 2998 : *T0 = L_SUBFR;
659 2998 : move16();
660 2998 : *T0_frac = 0;
661 2998 : move16();
662 : }
663 3481 : ELSE IF( EQ_16( tc_subfr, 3 * L_SUBFR ) )
664 : {
665 1516 : i = (Word16) get_next_indice_fx( st_fx, nBits );
666 :
667 1516 : IF( EQ_16( nBits, 9 ) )
668 : {
669 1500 : abs_pit_dec_fx( 4, i, 0, T0, T0_frac );
670 : }
671 : ELSE
672 : {
673 16 : abs_pit_dec_fx( 2, i, 0, T0, T0_frac );
674 : }
675 : }
676 : ELSE
677 : {
678 1965 : i = (Word16) get_next_indice_fx( st_fx, nBits );
679 1965 : move16();
680 :
681 1965 : IF( i == 0 )
682 : {
683 287 : *T0 = L_SUBFR;
684 287 : move16();
685 287 : *T0_frac = 0;
686 287 : move16();
687 : }
688 : ELSE
689 : {
690 1678 : IF( EQ_16( tc_subfr, TC_0_0 ) )
691 : {
692 1532 : delta_pit_dec_fx( 2, i, T0, T0_frac, PIT_MIN - 1 );
693 : }
694 : ELSE
695 : {
696 146 : delta_pit_dec_fx( 0, i, T0, T0_frac, PIT_MIN - 1 );
697 : }
698 : }
699 : }
700 : }
701 : ELSE /* L_frame == L_FRAME16k */
702 : {
703 6189 : i = (Word16) get_next_indice_fx( st_fx, nBits );
704 6189 : move16();
705 :
706 6189 : IF( EQ_16( nBits, 10 ) )
707 : {
708 2676 : IF( LT_16( i, ( PIT16k_FR2_EXTEND_10b - PIT16k_MIN_EXTEND ) * 4 ) )
709 : {
710 2614 : *T0 = add( PIT16k_MIN_EXTEND, shr( i, 2 ) );
711 2614 : move16();
712 2614 : temp = shl( sub( *T0, PIT16k_MIN_EXTEND ), 2 );
713 2614 : *T0_frac = sub( i, temp );
714 2614 : move16();
715 : }
716 : ELSE
717 : {
718 62 : index = sub( i, shl( sub( PIT16k_FR2_EXTEND_10b, PIT16k_MIN_EXTEND ), 2 ) );
719 62 : *T0 = add( PIT16k_FR2_EXTEND_10b, shr( index, 1 ) );
720 62 : *T0_frac = sub( index, shl( sub( *T0, PIT16k_FR2_EXTEND_10b ), 1 ) );
721 62 : ( *T0_frac ) = shl( *T0_frac, 1 );
722 : }
723 : }
724 3513 : ELSE IF( EQ_16( nBits, 6 ) )
725 : {
726 3513 : *T0 = add( PIT16k_MIN, shr( i, 1 ) );
727 3513 : move16();
728 3513 : *T0_frac = sub( i, shl( sub( *T0, PIT16k_MIN ), 1 ) );
729 3513 : *T0_frac = shl( *T0_frac, 1 );
730 3513 : move16();
731 : }
732 : }
733 :
734 : /*----------------------------------------------------------------*
735 : * decode other TC parameters
736 : *----------------------------------------------------------------*/
737 :
738 12668 : imp_shape = (Word16) get_next_indice_fx( st_fx, 3 );
739 12668 : move16();
740 12668 : imp_pos = (Word16) get_next_indice_fx( st_fx, 6 );
741 12668 : move16();
742 12668 : imp_sign = (Word16) get_next_indice_fx( st_fx, 1 );
743 12668 : move16();
744 12668 : imp_gain = (Word16) get_next_indice_fx( st_fx, 3 );
745 12668 : move16();
746 :
747 : /*----------------------------------------------------------------*
748 : * - restore gain_trans
749 : * - build glottal codebook contribution
750 : *----------------------------------------------------------------*/
751 :
752 12668 : gain_trans = tbl_gain_trans_tc_fx[imp_gain];
753 12668 : move16();
754 12668 : test();
755 12668 : if ( imp_sign == 0 )
756 : {
757 6265 : gain_trans = negate( gain_trans );
758 6265 : move16();
759 : }
760 :
761 : /* local max for scaling need */
762 : /* maximum of impulse always takes 15 bits (except impulse #7 wich is using 14 bits) */
763 :
764 12668 : tempS = 4;
765 12668 : move16();
766 12668 : test();
767 12668 : if ( LE_16( imp_gain, 3 ) )
768 : {
769 5697 : tempS = 7;
770 5697 : move16();
771 : }
772 :
773 : /* build glottal codebook contribution */
774 12668 : sc = add( 13 - 15, tempS ); /* scaling of curent exc */
775 12668 : sc = sub( *Q_exc, sc );
776 :
777 12668 : pt_shape = &Glottal_cdbk_fx[add( sub( i_mult2( imp_shape, L_IMPULSE ), imp_pos ), L_IMPULSE2 )];
778 12668 : move16();
779 :
780 12668 : j = s_max( 0, sub( imp_pos, L_IMPULSE2 ) );
781 12668 : move16(); /* penalty for exc + i_subfr initialisation */
782 250346 : FOR( i = 0; i < j; i++ )
783 : {
784 237678 : exc[add( i, i_subfr )] = 0;
785 237678 : move16();
786 : }
787 12668 : j = s_min( L_SUBFR, add( imp_pos, L_IMPULSE2 ) );
788 216071 : FOR( ; i <= j; i++ )
789 : {
790 203403 : exc[i + i_subfr] = round_fx_o( L_shl_o( L_mult_o( pt_shape[i], gain_trans, &Overflow ), sc, &Overflow ), &Overflow ); /* (Qx * Q14 ) */
791 203403 : move16();
792 : }
793 383087 : FOR( ; i < L_SUBFR; i++ )
794 : {
795 370419 : exc[i + i_subfr] = 0;
796 370419 : move16();
797 : }
798 :
799 : /*--------------------------------------------------------------*
800 : * adapt. search of the second impulse in the same subframe
801 : * (when appears)
802 : *--------------------------------------------------------------*/
803 :
804 12668 : pred_lt4_tc_fx( exc, *T0, *T0_frac, inter4_2_fx, imp_pos, i_subfr );
805 12668 : IF( st_fx->hBWE_TD != NULL ){
806 12668 : IF( EQ_16( L_frame, L_FRAME ) ){
807 6479 : interp_code_5over2_fx( &exc[i_subfr], &bwe_exc[i_subfr * HIBND_ACB_L_FAC], L_SUBFR );
808 : }
809 : ELSE
810 : {
811 6189 : interp_code_4over2_fx( &exc[i_subfr], &bwe_exc[i_subfr * 2], L_SUBFR );
812 : }
813 : }
814 12668 : *position = add( imp_pos, i_subfr );
815 12668 : move16();
816 12668 : return;
817 : }
818 :
819 : /*-------------------------------------------------------------------*
820 : * tc_classif_fx()
821 : *
822 : * TC subframe classification decoding
823 : *-------------------------------------------------------------------*/
824 :
825 12668 : Word16 tc_classif_fx( /*o: Q0*/
826 : Decoder_State *st_fx, /* i/o: decoder state structure */
827 : const Word16 L_frame /* i : length of the frame Q0 */
828 : )
829 : {
830 : Word16 tc_subfr, indice;
831 : (void) ( L_frame );
832 12668 : IF( EQ_16( st_fx->L_frame, L_FRAME ) )
833 : {
834 6479 : IF( get_next_indice_fx( st_fx, 1 ) )
835 : {
836 1532 : tc_subfr = TC_0_0;
837 1532 : move16();
838 : }
839 : ELSE
840 : {
841 4947 : IF( get_next_indice_fx( st_fx, 1 ) )
842 : {
843 1987 : tc_subfr = 0;
844 1987 : move16();
845 :
846 1987 : IF( get_next_indice_fx( st_fx, 1 ) )
847 : {
848 188 : tc_subfr = TC_0_192;
849 188 : move16();
850 : }
851 : ELSE
852 : {
853 1799 : tc_subfr = TC_0_128;
854 1799 : move16();
855 1799 : if ( get_next_indice_fx( st_fx, 1 ) )
856 : {
857 1180 : tc_subfr = TC_0_64;
858 1180 : move16();
859 : }
860 : }
861 : }
862 : ELSE
863 : {
864 2960 : IF( get_next_indice_fx( st_fx, 1 ) )
865 : {
866 1011 : tc_subfr = L_SUBFR;
867 1011 : move16();
868 : }
869 : ELSE
870 : {
871 1949 : tc_subfr = 3 * L_SUBFR;
872 1949 : move16();
873 1949 : if ( get_next_indice_fx( st_fx, 1 ) )
874 : {
875 433 : tc_subfr = 2 * L_SUBFR;
876 433 : move16();
877 : }
878 : }
879 : }
880 : }
881 : }
882 : ELSE /* L_frame == L_FRAME16k */
883 : {
884 6189 : indice = (Word16) get_next_indice_fx( st_fx, 2 );
885 :
886 6189 : IF( LT_16( indice, 3 ) )
887 : {
888 4132 : tc_subfr = shl( indice, 6 );
889 : }
890 : ELSE
891 : {
892 2057 : tc_subfr = 4 * L_SUBFR;
893 2057 : move16();
894 2057 : if ( get_next_indice_fx( st_fx, 1 ) == 0 )
895 : {
896 229 : tc_subfr = 3 * L_SUBFR;
897 229 : move16();
898 : }
899 : }
900 : }
901 :
902 12668 : return ( tc_subfr );
903 : }
|