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 : IF( st_fx->element_mode != EVS_MONO )
213 : {
214 1172 : pred_lt4_ivas_fx( &exc[i_subfr], &exc[i_subfr], *T0, *T0_frac, L_SUBFR + 1, L_pitch_inter4_2, L_INTERPOL2, PIT_UP_SAMP );
215 : }
216 : ELSE
217 : {
218 8 : pred_lt4( &exc[i_subfr], &exc[i_subfr], *T0, *T0_frac, L_SUBFR + 1, pitch_inter4_2, L_INTERPOL2, PIT_UP_SAMP );
219 : }
220 1180 : offset = tbe_celp_exc_offset( *T0, *T0_frac, L_frame );
221 :
222 1180 : move16();
223 1180 : move16(); /* penality for 2 ptrs initialization */
224 :
225 189980 : FOR( i = 0; i < L_SUBFR * HIBND_ACB_L_FAC; i++ )
226 : {
227 188800 : bwe_exc[add( i, i_subfr * HIBND_ACB_L_FAC )] = bwe_exc[sub( add( i, i_subfr * HIBND_ACB_L_FAC ), offset )];
228 188800 : move16();
229 : }
230 : }
231 11025 : ELSE IF( ( EQ_16( i_subfr, 2 * L_SUBFR ) ) && ( EQ_16( tc_subfr, TC_0_128 ) ) )
232 : {
233 : /*--------------------------------------------------------*
234 : * second glottal impulse is in the 3rd subframe
235 : * - build exc[] in 3rd subframe
236 : *--------------------------------------------------------*/
237 :
238 : /* 7bit pitch DECODER */
239 619 : pit_start = sub( 2 * L_SUBFR, ( *position ) );
240 619 : index = (Word16) get_next_indice_fx( st_fx, nBits );
241 :
242 619 : *T0 = add( pit_start, shr( ( index ), 1 ) );
243 619 : move16();
244 619 : *T0_frac = shl( sub( ( index ), shl( sub( *T0, pit_start ), 1 ) ), 1 );
245 619 : move16();
246 619 : limit_T0_fx( L_FRAME, 8, pit_flag, limit_flag, *T0, 0, T0_min, T0_max ); /* find T0_min and T0_max */
247 :
248 : /* Find the adaptive codebook vector. ACELP long-term prediction */
249 619 : IF( st_fx->element_mode != EVS_MONO )
250 : {
251 615 : pred_lt4_ivas_fx( &exc[i_subfr], &exc[i_subfr], *T0, *T0_frac, L_SUBFR + 1, L_pitch_inter4_2, L_INTERPOL2, PIT_UP_SAMP );
252 : }
253 : ELSE
254 : {
255 4 : pred_lt4( &exc[i_subfr], &exc[i_subfr], *T0, *T0_frac, L_SUBFR + 1, pitch_inter4_2, L_INTERPOL2, PIT_UP_SAMP );
256 : }
257 619 : offset = tbe_celp_exc_offset( *T0, *T0_frac, L_frame );
258 619 : move16();
259 619 : move16(); /* penality for 2 ptrs initialization */
260 :
261 99659 : FOR( i = 0; i < L_SUBFR * HIBND_ACB_L_FAC; i++ )
262 : {
263 99040 : bwe_exc[add( i, i_subfr * HIBND_ACB_L_FAC )] = bwe_exc[sub( add( i, i_subfr * HIBND_ACB_L_FAC ), offset )];
264 99040 : move16();
265 : }
266 : }
267 10406 : ELSE IF( ( EQ_16( i_subfr, 2 * L_SUBFR ) ) && ( EQ_16( tc_subfr, TC_0_192 ) ) )
268 : {
269 : /*--------------------------------------------------------*
270 : * second glottal impulse is in the 4th subframe
271 : * - build exc[] in 3rd subframe
272 : *--------------------------------------------------------*/
273 :
274 188 : *T0 = 4 * L_SUBFR;
275 188 : move16();
276 188 : *T0_frac = 0;
277 188 : move16();
278 188 : *Jopt_flag = 0;
279 188 : move16();
280 :
281 : /* set adaptive part of exciation for curent subframe to 0 */
282 188 : set16_fx( &exc[i_subfr], 0, (Word16) ( L_SUBFR + 1 ) );
283 188 : set16_fx( &bwe_exc[i_subfr * HIBND_ACB_L_FAC], 0, (Word16) ( L_SUBFR * HIBND_ACB_L_FAC ) );
284 : }
285 10218 : ELSE IF( ( EQ_16( i_subfr, 3 * L_SUBFR ) ) && ( EQ_16( tc_subfr, TC_0_192 ) ) )
286 : {
287 : /*--------------------------------------------------------*
288 : * second glottal impulse is in the 4th subframe
289 : * - build exc[] in 4th subframe
290 : *--------------------------------------------------------*/
291 188 : pit_start = sub( 3 * L_SUBFR, ( *position ) );
292 188 : pit_limit = sub( 2 * L_FRAME - PIT_MAX, add( shl( ( *position ), 1 ), 2 ) );
293 :
294 :
295 188 : index = (Word16) get_next_indice_fx( st_fx, nBits );
296 :
297 188 : IF( LT_16( index, shl( sub( pit_limit, pit_start ), 1 ) ) )
298 : {
299 138 : *T0 = add( pit_start, shr( index, 1 ) );
300 138 : move16();
301 138 : *T0_frac = shl( sub( index, shl( sub( ( *T0 ), pit_start ), 1 ) ), 1 );
302 138 : move16();
303 : }
304 : ELSE
305 : {
306 50 : *T0 = add( index, sub( pit_limit, shl( sub( pit_limit, pit_start ), 1 ) ) );
307 50 : move16();
308 :
309 50 : *T0_frac = 0;
310 50 : move16();
311 : }
312 :
313 : /* biterror detection mechanism */
314 188 : IF( GT_16( add( shl( *T0, 2 ), *T0_frac ), add( ( PIT_MAX << 2 ), 2 ) ) )
315 : {
316 0 : *T0 = L_SUBFR;
317 0 : move16();
318 0 : *T0_frac = 0;
319 0 : move16();
320 0 : st_fx->BER_detect = 1;
321 0 : move16();
322 : }
323 :
324 : /* Find the adaptive codebook vector. ACELP long-term prediction */
325 188 : IF( st_fx->element_mode != EVS_MONO )
326 : {
327 184 : pred_lt4_ivas_fx( &exc[i_subfr], &exc[i_subfr], *T0, *T0_frac, L_SUBFR + 1, L_pitch_inter4_2, L_INTERPOL2, PIT_UP_SAMP );
328 : }
329 : ELSE
330 : {
331 4 : pred_lt4( &exc[i_subfr], &exc[i_subfr], *T0, *T0_frac, L_SUBFR + 1, pitch_inter4_2, L_INTERPOL2, PIT_UP_SAMP );
332 : }
333 188 : offset = tbe_celp_exc_offset( *T0, *T0_frac, L_frame );
334 188 : move16();
335 188 : move16(); /* penality for 2 ptrs initialization */
336 30268 : FOR( i = 0; i < L_SUBFR * HIBND_ACB_L_FAC; i++ )
337 : {
338 30080 : bwe_exc[add( i, i_subfr * HIBND_ACB_L_FAC )] = bwe_exc[sub( add( i, i_subfr * HIBND_ACB_L_FAC ), offset )];
339 30080 : move16();
340 : }
341 : }
342 10030 : ELSE IF( ( EQ_16( i_subfr, 3 * L_SUBFR ) ) && ( EQ_16( tc_subfr, TC_0_128 ) ) )
343 : {
344 : /*--------------------------------------------------------*
345 : * second glottal impulse in the 3rd subframe
346 : * build exc[] in 4th subframe
347 : *--------------------------------------------------------*/
348 :
349 :
350 619 : index = (Word16) get_next_indice_fx( st_fx, nBits );
351 619 : delta_pit_dec_fx( 2, index, T0, T0_frac, *T0_min );
352 :
353 : /* Find the adaptive codebook vector. ACELP long-term prediction */
354 619 : IF( st_fx->element_mode != EVS_MONO )
355 : {
356 615 : pred_lt4_ivas_fx( &exc[i_subfr], &exc[i_subfr], *T0, *T0_frac, L_SUBFR + 1, L_pitch_inter4_2, L_INTERPOL2, PIT_UP_SAMP );
357 : }
358 : ELSE
359 : {
360 4 : pred_lt4( &exc[i_subfr], &exc[i_subfr], *T0, *T0_frac, L_SUBFR + 1, pitch_inter4_2, L_INTERPOL2, PIT_UP_SAMP );
361 : }
362 619 : offset = tbe_celp_exc_offset( *T0, *T0_frac, L_frame );
363 619 : move16();
364 619 : move16(); /* penality for 2 ptrs initialization */
365 99659 : FOR( i = 0; i < L_SUBFR * HIBND_ACB_L_FAC; i++ )
366 : {
367 99040 : bwe_exc[add( i, i_subfr * HIBND_ACB_L_FAC )] = bwe_exc[sub( add( i, i_subfr * HIBND_ACB_L_FAC ), offset )];
368 99040 : move16();
369 : }
370 : }
371 : /*------------------------------------------------------------*
372 : * first glottal impulse is NOT in the 1st subframe,
373 : * or two impulses are in the 1st subframe
374 : *------------------------------------------------------------*/
375 : ELSE
376 : {
377 9411 : index = (Word16) get_next_indice_fx( st_fx, nBits );
378 :
379 9411 : pit_Q_dec_fx( 0, index, nBits, 8, pit_flag, limit_flag, T0, T0_frac, T0_min, T0_max, &st_fx->BER_detect );
380 :
381 : /* Find the adaptive codebook vector */
382 9411 : IF( st_fx->element_mode != EVS_MONO )
383 : {
384 9286 : pred_lt4_ivas_fx( &exc[i_subfr], &exc[i_subfr], *T0, *T0_frac, L_SUBFR + 1, L_pitch_inter4_2, L_INTERPOL2, PIT_UP_SAMP );
385 : }
386 : ELSE
387 : {
388 125 : pred_lt4( &exc[i_subfr], &exc[i_subfr], *T0, *T0_frac, L_SUBFR + 1, pitch_inter4_2, L_INTERPOL2, PIT_UP_SAMP );
389 : }
390 9411 : offset = tbe_celp_exc_offset( *T0, *T0_frac, L_frame );
391 9411 : move16();
392 9411 : move16(); /* penality for 2 ptrs initialization */
393 1515171 : FOR( i = 0; i < L_SUBFR * HIBND_ACB_L_FAC; i++ )
394 : {
395 1505760 : bwe_exc[add( i, i_subfr * HIBND_ACB_L_FAC )] = bwe_exc[sub( add( i, i_subfr * HIBND_ACB_L_FAC ), offset )];
396 1505760 : move16();
397 : }
398 : }
399 :
400 : /*-----------------------------------------------------------------*
401 : * LP filtering of the adaptive excitation (if non-zero)
402 : *-----------------------------------------------------------------*/
403 13012 : IF( *Jopt_flag )
404 : {
405 12017 : lp_filt_exc_dec_fx( st_fx, MODE1, i_subfr, L_SUBFR, L_frame, st_fx->acelp_cfg.ltf_mode, exc );
406 : }
407 :
408 : /*---------------------------------------------------------------------*
409 : * fill the pitch buffer - needed for post-processing and FEC_clas_estim()
410 : *---------------------------------------------------------------------*/
411 :
412 13012 : **pt_pitch = shl( add( shl( *T0, 2 ), *T0_frac ), 4 );
413 13012 : move16(); /* save subframe pitch values Q6 */
414 :
415 13012 : test();
416 13012 : test();
417 13012 : test();
418 13012 : test();
419 13012 : test();
420 13012 : IF( ( GE_16( tc_subfr, 2 * L_SUBFR ) ) && ( EQ_16( i_subfr, 3 * L_SUBFR ) ) )
421 : {
422 433 : tmp = shl( add( shl( *T0, 2 ), *T0_frac ), 4 );
423 433 : ( *pt_pitch ) -= 3;
424 433 : move16();
425 433 : **pt_pitch = tmp;
426 433 : move16(); /*Q6*/
427 433 : ( *pt_pitch )++;
428 433 : **pt_pitch = tmp;
429 433 : move16(); /*Q6*/
430 433 : ( *pt_pitch )++;
431 433 : move16();
432 433 : **pt_pitch = tmp;
433 433 : move16(); /*Q6*/
434 433 : ( *pt_pitch )++;
435 433 : move16();
436 : }
437 12579 : ELSE IF( ( EQ_16( tc_subfr, L_SUBFR ) ) && ( EQ_16( i_subfr, 2 * L_SUBFR ) ) )
438 : {
439 1011 : tmp = shl( add( shl( *T0, 2 ), *T0_frac ), 4 );
440 1011 : ( *pt_pitch ) -= 2;
441 1011 : move16();
442 1011 : **pt_pitch = tmp;
443 1011 : move16(); /*Q6*/
444 1011 : ( *pt_pitch )++;
445 1011 : **pt_pitch = tmp;
446 1011 : move16(); /*Q6*/
447 1011 : ( *pt_pitch )++;
448 1011 : move16();
449 : }
450 11568 : ELSE IF( ( EQ_16( tc_subfr, TC_0_64 ) ) && ( EQ_16( i_subfr, L_SUBFR ) ) )
451 : {
452 1180 : tmp = shl( add( shl( *T0, 2 ), *T0_frac ), 4 );
453 1180 : ( *pt_pitch ) -= 1;
454 1180 : move16();
455 1180 : **pt_pitch = tmp;
456 1180 : move16(); /*Q6*/
457 1180 : ( *pt_pitch )++;
458 : }
459 10388 : ELSE IF( ( EQ_16( tc_subfr, TC_0_128 ) ) && ( EQ_16( i_subfr, 2 * L_SUBFR ) ) )
460 : {
461 619 : tmp = shl( add( shl( *T0, 2 ), *T0_frac ), 4 );
462 619 : ( *pt_pitch ) -= 2;
463 619 : move16();
464 619 : **pt_pitch = tmp;
465 619 : move16(); /*Q6*/
466 619 : ( *pt_pitch )++;
467 619 : **pt_pitch = tmp;
468 619 : move16(); /*Q6*/
469 619 : ( *pt_pitch )++;
470 619 : move16();
471 : }
472 9769 : ELSE IF( ( EQ_16( tc_subfr, TC_0_192 ) ) && ( EQ_16( i_subfr, 3 * L_SUBFR ) ) )
473 : {
474 188 : tmp = shl( add( shl( *T0, 2 ), *T0_frac ), 4 );
475 188 : ( *pt_pitch ) -= 3;
476 188 : move16();
477 188 : **pt_pitch = tmp;
478 188 : move16(); /*Q6*/
479 188 : ( *pt_pitch )++;
480 188 : **pt_pitch = tmp;
481 188 : move16(); /*Q6*/
482 188 : ( *pt_pitch )++;
483 188 : move16();
484 188 : **pt_pitch = tmp;
485 188 : move16(); /*Q6*/
486 188 : ( *pt_pitch )++;
487 188 : move16();
488 : }
489 : }
490 : ELSE /* L_frame == L_FRAME16k */
491 : {
492 14584 : if ( GE_16( i_subfr, 2 * L_SUBFR ) )
493 : {
494 12006 : limit_flag = 1;
495 12006 : move16();
496 : }
497 :
498 14584 : IF( EQ_16( sub( i_subfr, tc_subfr ), L_SUBFR ) )
499 : {
500 4361 : limit_T0_fx( L_FRAME16k, 8, 0, limit_flag, *T0, *T0_frac, T0_min, T0_max );
501 : }
502 :
503 : /*-----------------------------------------------------------------*
504 : * get number of bits and index for pitch decoding
505 : *-----------------------------------------------------------------*/
506 14584 : nBits = st_fx->acelp_cfg.pitch_bits[i_subfridx];
507 14584 : move16();
508 :
509 14584 : index = (Word16) get_next_indice_fx( st_fx, nBits );
510 :
511 : /*-----------------------------------------------------------------*
512 : * Find adaptive part of excitation, encode pitch period
513 : *-----------------------------------------------------------------*/
514 :
515 14584 : IF( EQ_16( nBits, 10 ) )
516 : {
517 3513 : pit16k_Q_dec_fx( index, nBits, limit_flag, T0, T0_frac, T0_min, T0_max, &st_fx->BER_detect );
518 : }
519 11071 : ELSE IF( EQ_16( nBits, 8 ) ) /* tc_subfr==0 && i_subfr==L_SUBFR */
520 : {
521 : /*-----------------------------------------------------------------------------*
522 : * The pitch range is encoded absolutely with 8 bits and is divided as follows:
523 : * PIT16k_MIN to PIT16k_FR2_TC0_2SUBFR-1 resolution 1/4 (frac = 0,1,2 or 3)
524 : * PIT16k_FR2_TC0_2SUBFR to 2*L_SUBFR resolution 1/2 (frac = 0 or 2)
525 : *-----------------------------------------------------------------------------*/
526 :
527 2578 : IF( LT_16( index, ( PIT16k_FR2_TC0_2SUBFR - PIT16k_MIN ) * 4 ) ) /*(PIT16k_FR2_TC0_2SUBFR-PIT16k_MIN)*4*/
528 : {
529 1502 : *T0 = add( PIT16k_MIN, shr( index, 2 ) );
530 1502 : move16();
531 1502 : temp = shl( sub( *T0, PIT16k_MIN ), 2 );
532 1502 : move16();
533 1502 : *T0_frac = sub( index, temp );
534 1502 : move16();
535 : }
536 : ELSE
537 : {
538 1076 : index = sub( index, ( PIT16k_FR2_TC0_2SUBFR - PIT16k_MIN ) * 4 ); /* (PIT16k_FR2_TC0_2SUBFR-PIT16k_MIN)*4 */
539 1076 : *T0 = add( PIT16k_FR2_TC0_2SUBFR, shr( index, 1 ) );
540 1076 : move16();
541 1076 : temp = shl( sub( *T0, PIT16k_FR2_TC0_2SUBFR ), 1 );
542 1076 : move16();
543 1076 : *T0_frac = shl( sub( index, temp ), 1 );
544 1076 : move16();
545 : }
546 :
547 : /* biterror detection mechanism */
548 2578 : IF( GT_16( add( shl( *T0, 2 ), *T0_frac ), ( ( 2 * L_SUBFR ) << 2 ) ) )
549 : {
550 0 : *T0 = L_SUBFR;
551 0 : move16();
552 0 : *T0_frac = 0;
553 0 : move16();
554 0 : st_fx->BER_detect = 1;
555 0 : move16();
556 : }
557 : }
558 8493 : ELSE IF( EQ_16( nBits, 6 ) )
559 : {
560 8493 : delta_pit_dec_fx( 4, index, T0, T0_frac, *T0_min );
561 : }
562 14584 : IF( EQ_16( nBits, 6 ) )
563 : {
564 8493 : limit_T0_fx( L_FRAME16k, 8, L_SUBFR, limit_flag, *T0, *T0_frac, T0_min, T0_max ); /* find T0_min and T0_max */
565 : }
566 :
567 : /*-----------------------------------------------------------------*
568 : * - find the adaptive codebook vector
569 : * - LP filtering of the adaptive excitation (if non-zero)
570 : *-----------------------------------------------------------------*/
571 14584 : test();
572 14584 : IF( ( EQ_16( i_subfr, L_SUBFR ) ) && ( EQ_16( *T0, 2 * L_SUBFR ) ) )
573 : {
574 : /* no adaptive excitation in the second subframe */
575 436 : set16_fx( &exc[i_subfr], 0, L_SUBFR + 1 );
576 436 : get_next_indice_fx( st_fx, 1 ); /* this bit is actually not needed */
577 436 : set16_fx( &bwe_exc[i_subfr * 2], 0, L_SUBFR * 2 );
578 : }
579 : ELSE
580 : {
581 : /* Find the adaptive codebook vector - ACELP long-term prediction */
582 14148 : IF( st_fx->element_mode != EVS_MONO )
583 : {
584 13981 : pred_lt4_ivas_fx( &exc[i_subfr], &exc[i_subfr], *T0, *T0_frac, L_SUBFR + 1, L_pitch_inter4_2, L_INTERPOL2, PIT_UP_SAMP );
585 : }
586 : ELSE
587 : {
588 167 : pred_lt4( &exc[i_subfr], &exc[i_subfr], *T0, *T0_frac, L_SUBFR + 1, pitch_inter4_2, L_INTERPOL2, PIT_UP_SAMP );
589 : }
590 14148 : offset = tbe_celp_exc_offset( *T0, *T0_frac, L_frame );
591 14148 : move16();
592 14148 : move16(); /* penalty for 2 ptrs initialization */
593 1825092 : FOR( i = 0; i < L_SUBFR * 2; i++ )
594 : {
595 : /* bwe_exc[i + i_subfr * 2] = bwe_exc[i + i_subfr * 2 - *T0 * 2 */
596 : /* - (int) ((float) *T0_frac * 0.5f + 4 + 0.5f) + 4];*/
597 1810944 : bwe_exc[add( i, i_subfr * 2 )] = bwe_exc[sub( add( i, i_subfr * 2 ), offset )];
598 : }
599 :
600 14148 : lp_filt_exc_dec_fx( st_fx, MODE1, i_subfr, L_SUBFR, L_frame, st_fx->acelp_cfg.ltf_mode, exc );
601 14148 : *Jopt_flag = 1;
602 14148 : move16();
603 : }
604 :
605 14584 : **pt_pitch = shl( add( shl( *T0, 2 ), *T0_frac ), 4 );
606 14584 : move16(); /* save subframe pitch values Q6 */
607 :
608 : /*---------------------------------------------------------------------*
609 : * fill the pitch buffer - needed for post-processing and FEC_clas_estim()
610 : *---------------------------------------------------------------------*/
611 14584 : test();
612 14584 : test();
613 14584 : IF( ( EQ_16( sub( i_subfr, tc_subfr ), L_SUBFR ) ) || ( tc_subfr == 0 && EQ_16( i_subfr, 2 * L_SUBFR ) ) )
614 : {
615 : /*index = i_subfr/L_SUBFR;*/
616 6939 : index = shr( i_subfr, 6 );
617 :
618 : /*Ptr */
619 6939 : ( *pt_pitch ) -= index;
620 6939 : move16();
621 :
622 19316 : FOR( i = 0; i < index; i++ )
623 : {
624 12377 : **pt_pitch = shl( add( shl( *T0, 2 ), *T0_frac ), 4 );
625 12377 : move16(); /* save subframe pitch values Q6 */
626 :
627 12377 : ( *pt_pitch )++;
628 : }
629 : }
630 : }
631 : }
632 :
633 56861 : return;
634 : }
635 :
636 :
637 : /*======================================================================*/
638 : /* FUNCTION : tc_dec_fx() */
639 : /*----------------------------------------------------------------------*/
640 : /* PURPOSE : Principal function for TC decoding */
641 : /* * Principal function for TC decoding. */
642 : /* * - constructs glottal codebook contribution */
643 : /* * - uses pitch sharpening */
644 : /* * - uses gain_trans */
645 : /*----------------------------------------------------------------------*/
646 : /* GLOBAL INPUT ARGUMENTS : */
647 : /* _ (Word16) L_frame : length of the frame Q0 */
648 : /*----------------------------------------------------------------------*/
649 : /* OUTPUT ARGUMENTS : */
650 : /* _ (Word16[]) exc_fx : adapt. excitation exc (Q0) */
651 : /* _ (Word16[]) exc2_fx : adapt. excitation/total exc (Q0) */
652 : /*----------------------------------------------------------------------*/
653 :
654 :
655 : /*----------------------------------------------------------------------*/
656 : /* RETURN ARGUMENTS : */
657 : /* _ None */
658 : /*======================================================================*/
659 :
660 12668 : static void tc_dec_fx(
661 : Decoder_State *st_fx, /* i/o: decoder state structure */
662 : const Word16 L_frame, /* i : length of the frame Q0*/
663 : Word16 exc[], /* o : glottal codebook contribution Q0*/
664 : Word16 *T0, /* o : close-loop pitch period Q0 */
665 : Word16 *T0_frac, /* o : close-loop pitch period - fractional part Q0 */
666 : const Word16 i_subfr, /* i : subframe index Q0*/
667 : const Word16 tc_subfr, /* i : TC subframe index Q0 */
668 : Word16 *position, /* o : first glottal impulse position in frame Q0*/
669 : Word16 bwe_exc[], /* o : excitation for SWB TBE Q_exc */
670 : Word16 *Q_exc /*i/o : scaling of excitation */
671 : )
672 : {
673 : Word16 i, imp_shape, imp_pos, imp_sign, imp_gain, nBits;
674 : Word16 gain_trans, temp;
675 : const Word16 *pt_shape;
676 : Word16 j, sc;
677 : Word16 tempS;
678 : Word16 index;
679 : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
680 12668 : Flag Overflow = 0;
681 12668 : move32();
682 : #endif
683 : /*----------------------------------------------------------------*
684 : * find the number of bits
685 : *----------------------------------------------------------------*/
686 12668 : nBits = st_fx->acelp_cfg.pitch_bits[shr( i_subfr, 6 )];
687 12668 : move16();
688 : /*----------------------------------------------------------------*
689 : * decode parameter T0 (pitch period)
690 : *----------------------------------------------------------------*/
691 12668 : IF( EQ_16( L_frame, L_FRAME ) )
692 : {
693 6479 : test();
694 6479 : test();
695 6479 : test();
696 6479 : test();
697 6479 : test();
698 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 ) ) )
699 : {
700 2998 : *T0 = L_SUBFR;
701 2998 : move16();
702 2998 : *T0_frac = 0;
703 2998 : move16();
704 : }
705 3481 : ELSE IF( EQ_16( tc_subfr, 3 * L_SUBFR ) )
706 : {
707 1516 : i = (Word16) get_next_indice_fx( st_fx, nBits );
708 :
709 1516 : IF( EQ_16( nBits, 9 ) )
710 : {
711 1500 : abs_pit_dec_fx( 4, i, 0, T0, T0_frac );
712 : }
713 : ELSE
714 : {
715 16 : abs_pit_dec_fx( 2, i, 0, T0, T0_frac );
716 : }
717 : }
718 : ELSE
719 : {
720 1965 : i = (Word16) get_next_indice_fx( st_fx, nBits );
721 1965 : move16();
722 :
723 1965 : IF( i == 0 )
724 : {
725 287 : *T0 = L_SUBFR;
726 287 : move16();
727 287 : *T0_frac = 0;
728 287 : move16();
729 : }
730 : ELSE
731 : {
732 1678 : IF( EQ_16( tc_subfr, TC_0_0 ) )
733 : {
734 1532 : delta_pit_dec_fx( 2, i, T0, T0_frac, PIT_MIN - 1 );
735 : }
736 : ELSE
737 : {
738 146 : delta_pit_dec_fx( 0, i, T0, T0_frac, PIT_MIN - 1 );
739 : }
740 : }
741 : }
742 : }
743 : ELSE /* L_frame == L_FRAME16k */
744 : {
745 6189 : i = (Word16) get_next_indice_fx( st_fx, nBits );
746 6189 : move16();
747 :
748 6189 : IF( EQ_16( nBits, 10 ) )
749 : {
750 2676 : IF( LT_16( i, ( PIT16k_FR2_EXTEND_10b - PIT16k_MIN_EXTEND ) * 4 ) )
751 : {
752 2614 : *T0 = add( PIT16k_MIN_EXTEND, shr( i, 2 ) );
753 2614 : move16();
754 2614 : temp = shl( sub( *T0, PIT16k_MIN_EXTEND ), 2 );
755 2614 : *T0_frac = sub( i, temp );
756 2614 : move16();
757 : }
758 : ELSE
759 : {
760 62 : index = sub( i, shl( sub( PIT16k_FR2_EXTEND_10b, PIT16k_MIN_EXTEND ), 2 ) );
761 62 : *T0 = add( PIT16k_FR2_EXTEND_10b, shr( index, 1 ) );
762 62 : *T0_frac = sub( index, shl( sub( *T0, PIT16k_FR2_EXTEND_10b ), 1 ) );
763 62 : ( *T0_frac ) = shl( *T0_frac, 1 );
764 : }
765 : }
766 3513 : ELSE IF( EQ_16( nBits, 6 ) )
767 : {
768 3513 : *T0 = add( PIT16k_MIN, shr( i, 1 ) );
769 3513 : move16();
770 3513 : *T0_frac = sub( i, shl( sub( *T0, PIT16k_MIN ), 1 ) );
771 3513 : *T0_frac = shl( *T0_frac, 1 );
772 3513 : move16();
773 : }
774 : }
775 :
776 : /*----------------------------------------------------------------*
777 : * decode other TC parameters
778 : *----------------------------------------------------------------*/
779 :
780 12668 : imp_shape = (Word16) get_next_indice_fx( st_fx, 3 );
781 12668 : move16();
782 12668 : imp_pos = (Word16) get_next_indice_fx( st_fx, 6 );
783 12668 : move16();
784 12668 : imp_sign = (Word16) get_next_indice_fx( st_fx, 1 );
785 12668 : move16();
786 12668 : imp_gain = (Word16) get_next_indice_fx( st_fx, 3 );
787 12668 : move16();
788 :
789 : /*----------------------------------------------------------------*
790 : * - restore gain_trans
791 : * - build glottal codebook contribution
792 : *----------------------------------------------------------------*/
793 :
794 12668 : gain_trans = tbl_gain_trans_tc_fx[imp_gain];
795 12668 : move16();
796 12668 : test();
797 12668 : if ( imp_sign == 0 )
798 : {
799 6265 : gain_trans = negate( gain_trans );
800 6265 : move16();
801 : }
802 :
803 : /* local max for scaling need */
804 : /* maximum of impulse always takes 15 bits (except impulse #7 wich is using 14 bits) */
805 :
806 12668 : tempS = 4;
807 12668 : move16();
808 12668 : test();
809 12668 : if ( LE_16( imp_gain, 3 ) )
810 : {
811 5697 : tempS = 7;
812 5697 : move16();
813 : }
814 :
815 : /* build glottal codebook contribution */
816 12668 : sc = add( 13 - 15, tempS ); /* scaling of curent exc */
817 12668 : sc = sub( *Q_exc, sc );
818 :
819 12668 : pt_shape = &Glottal_cdbk_fx[add( sub( i_mult2( imp_shape, L_IMPULSE ), imp_pos ), L_IMPULSE2 )];
820 12668 : move16();
821 :
822 12668 : j = s_max( 0, sub( imp_pos, L_IMPULSE2 ) );
823 12668 : move16(); /* penalty for exc + i_subfr initialisation */
824 250346 : FOR( i = 0; i < j; i++ )
825 : {
826 237678 : exc[add( i, i_subfr )] = 0;
827 237678 : move16();
828 : }
829 12668 : j = s_min( L_SUBFR, add( imp_pos, L_IMPULSE2 ) );
830 216071 : FOR( ; i <= j; i++ )
831 : {
832 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 ) */
833 203403 : move16();
834 : }
835 383087 : FOR( ; i < L_SUBFR; i++ )
836 : {
837 370419 : exc[i + i_subfr] = 0;
838 370419 : move16();
839 : }
840 :
841 : /*--------------------------------------------------------------*
842 : * adapt. search of the second impulse in the same subframe
843 : * (when appears)
844 : *--------------------------------------------------------------*/
845 :
846 12668 : pred_lt4_tc_fx( exc, *T0, *T0_frac, inter4_2_fx, imp_pos, i_subfr );
847 12668 : IF( st_fx->hBWE_TD != NULL ){
848 12668 : IF( EQ_16( L_frame, L_FRAME ) ){
849 6479 : interp_code_5over2_fx( &exc[i_subfr], &bwe_exc[i_subfr * HIBND_ACB_L_FAC], L_SUBFR );
850 : }
851 : ELSE
852 : {
853 6189 : interp_code_4over2_fx( &exc[i_subfr], &bwe_exc[i_subfr * 2], L_SUBFR );
854 : }
855 : }
856 12668 : *position = add( imp_pos, i_subfr );
857 12668 : move16();
858 12668 : return;
859 : }
860 :
861 : /*-------------------------------------------------------------------*
862 : * tc_classif_fx()
863 : *
864 : * TC subframe classification decoding
865 : *-------------------------------------------------------------------*/
866 :
867 12668 : Word16 tc_classif_fx( /*o: Q0*/
868 : Decoder_State *st_fx, /* i/o: decoder state structure */
869 : const Word16 L_frame /* i : length of the frame Q0 */
870 : )
871 : {
872 : Word16 tc_subfr, indice;
873 : (void) ( L_frame );
874 12668 : IF( EQ_16( st_fx->L_frame, L_FRAME ) )
875 : {
876 6479 : IF( get_next_indice_fx( st_fx, 1 ) )
877 : {
878 1532 : tc_subfr = TC_0_0;
879 1532 : move16();
880 : }
881 : ELSE
882 : {
883 4947 : IF( get_next_indice_fx( st_fx, 1 ) )
884 : {
885 1987 : tc_subfr = 0;
886 1987 : move16();
887 :
888 1987 : IF( get_next_indice_fx( st_fx, 1 ) )
889 : {
890 188 : tc_subfr = TC_0_192;
891 188 : move16();
892 : }
893 : ELSE
894 : {
895 1799 : tc_subfr = TC_0_128;
896 1799 : move16();
897 1799 : if ( get_next_indice_fx( st_fx, 1 ) )
898 : {
899 1180 : tc_subfr = TC_0_64;
900 1180 : move16();
901 : }
902 : }
903 : }
904 : ELSE
905 : {
906 2960 : IF( get_next_indice_fx( st_fx, 1 ) )
907 : {
908 1011 : tc_subfr = L_SUBFR;
909 1011 : move16();
910 : }
911 : ELSE
912 : {
913 1949 : tc_subfr = 3 * L_SUBFR;
914 1949 : move16();
915 1949 : if ( get_next_indice_fx( st_fx, 1 ) )
916 : {
917 433 : tc_subfr = 2 * L_SUBFR;
918 433 : move16();
919 : }
920 : }
921 : }
922 : }
923 : }
924 : ELSE /* L_frame == L_FRAME16k */
925 : {
926 6189 : indice = (Word16) get_next_indice_fx( st_fx, 2 );
927 :
928 6189 : IF( LT_16( indice, 3 ) )
929 : {
930 4132 : tc_subfr = shl( indice, 6 );
931 : }
932 : ELSE
933 : {
934 2057 : tc_subfr = 4 * L_SUBFR;
935 2057 : move16();
936 2057 : if ( get_next_indice_fx( st_fx, 1 ) == 0 )
937 : {
938 229 : tc_subfr = 3 * L_SUBFR;
939 229 : move16();
940 : }
941 : }
942 : }
943 :
944 12668 : return ( tc_subfr );
945 : }
|