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 58787 : 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 58787 : 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 58787 : limit_flag = 0;
64 58787 : move16();
65 : /*---------------------------------------------------------------------*
66 : * zero adaptive contribution (glottal shape codebook search not
67 : * in first subframe(s) )
68 : *---------------------------------------------------------------------*/
69 58787 : test();
70 58787 : IF( GT_16( tc_subfr, add( i_subfr, TC_0_192 ) ) )
71 : {
72 17122 : set16_fx( &exc[i_subfr], 0, L_SUBFR );
73 :
74 17122 : IF( EQ_16( L_frame, L_FRAME ) )
75 : {
76 6529 : 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 10593 : set16_fx( &bwe_exc[i_subfr * 2], 0, (Word16) ( L_SUBFR * 2 ) ); /* set past excitation buffer to 0 */
81 : }
82 :
83 17122 : *T0 = L_SUBFR;
84 17122 : move16();
85 17122 : *T0_frac = 0;
86 17122 : move16();
87 17122 : **pt_pitch = L_SUBFR_Q6;
88 17122 : move16();
89 : }
90 :
91 : /*---------------------------------------------------------------------*
92 : * glottal shape codebook search
93 : *---------------------------------------------------------------------*/
94 :
95 41665 : ELSE IF( ( ( GE_16( tc_subfr, i_subfr ) ) && ( LE_16( sub( tc_subfr, i_subfr ), TC_0_192 ) ) ) )
96 : {
97 13093 : set16_fx( exc - L_EXC_MEM, 0, L_EXC_MEM ); /* set past excitation buffer to 0 */
98 :
99 13093 : IF( EQ_16( L_frame, L_FRAME ) )
100 : {
101 6678 : 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 6415 : 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 13093 : tc_dec_fx( st_fx, L_frame, exc, T0, T0_frac, i_subfr, tc_subfr, position, bwe_exc, Q_exc );
110 13093 : **pt_pitch = shl( add( shl( *T0, 2 ), *T0_frac ), 4 ); // Q6
111 13093 : move16(); /* save subframe pitch values Q6 */
112 :
113 13093 : *Jopt_flag = 1;
114 13093 : 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 28572 : ELSE IF( LT_16( tc_subfr, i_subfr ) )
126 : {
127 28572 : IF( EQ_16( L_frame, L_FRAME ) )
128 : {
129 13505 : *Jopt_flag = 1;
130 13505 : move16();
131 13505 : test();
132 13505 : IF( ( GE_16( sub( i_subfr, tc_subfr ), L_SUBFR ) ) && ( LE_16( sub( i_subfr, tc_subfr ), L_SUBFR + TC_0_192 ) ) )
133 : {
134 1470 : pit_flag = 0;
135 1470 : move16();
136 : }
137 : ELSE
138 : {
139 12035 : pit_flag = L_SUBFR;
140 12035 : move16();
141 : }
142 13505 : IF( EQ_16( tc_subfr, TC_0_0 ) )
143 : {
144 4842 : IF( EQ_16( i_subfr, L_SUBFR ) )
145 : {
146 1614 : limit_T0_fx( L_FRAME, 8, pit_flag, limit_flag, *T0, 0, T0_min, T0_max );
147 : }
148 4842 : pit_flag = 1;
149 4842 : move16();
150 : }
151 :
152 : /*-----------------------------------------------------------------*
153 : * get number of bits for pitch decoding
154 : *-----------------------------------------------------------------*/
155 13505 : nBits = st_fx->acelp_cfg.pitch_bits[i_subfridx];
156 13505 : move16();
157 :
158 : /*------------------------------------------------------------*
159 : * first glottal impulse is in the 1st subframe
160 : *------------------------------------------------------------*/
161 13505 : test();
162 13505 : test();
163 13505 : test();
164 13505 : test();
165 13505 : test();
166 13505 : test();
167 13505 : 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 845 : *T0 = 2 * L_SUBFR;
175 845 : move16();
176 845 : *T0_frac = 0;
177 845 : move16();
178 845 : *Jopt_flag = 0;
179 845 : move16();
180 :
181 : /* set adaptive part of exciation for curent subframe to 0 */
182 845 : set16_fx( &exc[i_subfr], 0, (Word16) ( L_SUBFR + 1 ) );
183 845 : set16_fx( &bwe_exc[i_subfr * HIBND_ACB_L_FAC], 0, (Word16) ( L_SUBFR * HIBND_ACB_L_FAC ) );
184 : }
185 12660 : 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 1213 : pit_start = PIT_MIN;
192 1213 : move16();
193 1213 : if ( GT_16( PIT_MIN, ( *position ) ) )
194 : {
195 909 : pit_start = sub( L_SUBFR, *position );
196 : }
197 :
198 1213 : pit_start = s_max( pit_start, PIT_MIN );
199 1213 : pit_limit = add( shl( pit_start, 1 ), *position );
200 :
201 : /* 7 bit pitch DECODER */
202 1213 : index = (Word16) get_next_indice_fx( st_fx, nBits );
203 :
204 1213 : *T0 = add( pit_start, shr( ( index ), 1 ) );
205 1213 : move16();
206 1213 : *T0_frac = shl( sub( ( index ), shl( sub( *T0, pit_start ), 1 ) ), 1 );
207 1213 : move16();
208 :
209 1213 : 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 1213 : IF( st_fx->element_mode != EVS_MONO )
213 : {
214 1203 : 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 10 : pred_lt4( &exc[i_subfr], &exc[i_subfr], *T0, *T0_frac, L_SUBFR + 1, pitch_inter4_2, L_INTERPOL2, PIT_UP_SAMP );
219 : }
220 1213 : offset = tbe_celp_exc_offset( *T0, *T0_frac, L_frame );
221 :
222 1213 : move16();
223 1213 : move16(); /* penality for 2 ptrs initialization */
224 :
225 195293 : FOR( i = 0; i < L_SUBFR * HIBND_ACB_L_FAC; i++ )
226 : {
227 194080 : bwe_exc[add( i, i_subfr * HIBND_ACB_L_FAC )] = bwe_exc[sub( add( i, i_subfr * HIBND_ACB_L_FAC ), offset )];
228 194080 : move16();
229 : }
230 : }
231 11447 : 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 644 : pit_start = sub( 2 * L_SUBFR, ( *position ) );
240 644 : index = (Word16) get_next_indice_fx( st_fx, nBits );
241 :
242 644 : *T0 = add( pit_start, shr( ( index ), 1 ) );
243 644 : move16();
244 644 : *T0_frac = shl( sub( ( index ), shl( sub( *T0, pit_start ), 1 ) ), 1 );
245 644 : move16();
246 644 : 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 644 : IF( st_fx->element_mode != EVS_MONO )
250 : {
251 638 : 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 6 : pred_lt4( &exc[i_subfr], &exc[i_subfr], *T0, *T0_frac, L_SUBFR + 1, pitch_inter4_2, L_INTERPOL2, PIT_UP_SAMP );
256 : }
257 644 : offset = tbe_celp_exc_offset( *T0, *T0_frac, L_frame );
258 644 : move16();
259 644 : move16(); /* penality for 2 ptrs initialization */
260 :
261 103684 : FOR( i = 0; i < L_SUBFR * HIBND_ACB_L_FAC; i++ )
262 : {
263 103040 : bwe_exc[add( i, i_subfr * HIBND_ACB_L_FAC )] = bwe_exc[sub( add( i, i_subfr * HIBND_ACB_L_FAC ), offset )];
264 103040 : move16();
265 : }
266 : }
267 10803 : 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 201 : *T0 = 4 * L_SUBFR;
275 201 : move16();
276 201 : *T0_frac = 0;
277 201 : move16();
278 201 : *Jopt_flag = 0;
279 201 : move16();
280 :
281 : /* set adaptive part of exciation for curent subframe to 0 */
282 201 : set16_fx( &exc[i_subfr], 0, (Word16) ( L_SUBFR + 1 ) );
283 201 : set16_fx( &bwe_exc[i_subfr * HIBND_ACB_L_FAC], 0, (Word16) ( L_SUBFR * HIBND_ACB_L_FAC ) );
284 : }
285 10602 : 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 201 : pit_start = sub( 3 * L_SUBFR, ( *position ) );
292 201 : pit_limit = sub( 2 * L_FRAME - PIT_MAX, add( shl( ( *position ), 1 ), 2 ) );
293 :
294 :
295 201 : index = (Word16) get_next_indice_fx( st_fx, nBits );
296 :
297 201 : IF( LT_16( index, shl( sub( pit_limit, pit_start ), 1 ) ) )
298 : {
299 149 : *T0 = add( pit_start, shr( index, 1 ) );
300 149 : move16();
301 149 : *T0_frac = shl( sub( index, shl( sub( ( *T0 ), pit_start ), 1 ) ), 1 );
302 149 : move16();
303 : }
304 : ELSE
305 : {
306 52 : *T0 = add( index, sub( pit_limit, shl( sub( pit_limit, pit_start ), 1 ) ) );
307 52 : move16();
308 :
309 52 : *T0_frac = 0;
310 52 : move16();
311 : }
312 :
313 : /* biterror detection mechanism */
314 201 : 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 201 : IF( st_fx->element_mode != EVS_MONO )
326 : {
327 196 : 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 5 : pred_lt4( &exc[i_subfr], &exc[i_subfr], *T0, *T0_frac, L_SUBFR + 1, pitch_inter4_2, L_INTERPOL2, PIT_UP_SAMP );
332 : }
333 201 : offset = tbe_celp_exc_offset( *T0, *T0_frac, L_frame );
334 201 : move16();
335 201 : move16(); /* penality for 2 ptrs initialization */
336 32361 : FOR( i = 0; i < L_SUBFR * HIBND_ACB_L_FAC; i++ )
337 : {
338 32160 : bwe_exc[add( i, i_subfr * HIBND_ACB_L_FAC )] = bwe_exc[sub( add( i, i_subfr * HIBND_ACB_L_FAC ), offset )];
339 32160 : move16();
340 : }
341 : }
342 10401 : 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 644 : index = (Word16) get_next_indice_fx( st_fx, nBits );
351 644 : delta_pit_dec_fx( 2, index, T0, T0_frac, *T0_min );
352 :
353 : /* Find the adaptive codebook vector. ACELP long-term prediction */
354 644 : IF( st_fx->element_mode != EVS_MONO )
355 : {
356 638 : 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 6 : pred_lt4( &exc[i_subfr], &exc[i_subfr], *T0, *T0_frac, L_SUBFR + 1, pitch_inter4_2, L_INTERPOL2, PIT_UP_SAMP );
361 : }
362 644 : offset = tbe_celp_exc_offset( *T0, *T0_frac, L_frame );
363 644 : move16();
364 644 : move16(); /* penality for 2 ptrs initialization */
365 103684 : FOR( i = 0; i < L_SUBFR * HIBND_ACB_L_FAC; i++ )
366 : {
367 103040 : bwe_exc[add( i, i_subfr * HIBND_ACB_L_FAC )] = bwe_exc[sub( add( i, i_subfr * HIBND_ACB_L_FAC ), offset )];
368 103040 : 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 9757 : index = (Word16) get_next_indice_fx( st_fx, nBits );
378 :
379 9757 : 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 9757 : IF( st_fx->element_mode != EVS_MONO )
383 : {
384 9632 : 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 9757 : offset = tbe_celp_exc_offset( *T0, *T0_frac, L_frame );
391 9757 : move16();
392 9757 : move16(); /* penality for 2 ptrs initialization */
393 1570877 : FOR( i = 0; i < L_SUBFR * HIBND_ACB_L_FAC; i++ )
394 : {
395 1561120 : bwe_exc[add( i, i_subfr * HIBND_ACB_L_FAC )] = bwe_exc[sub( add( i, i_subfr * HIBND_ACB_L_FAC ), offset )];
396 1561120 : move16();
397 : }
398 : }
399 :
400 : /*-----------------------------------------------------------------*
401 : * LP filtering of the adaptive excitation (if non-zero)
402 : *-----------------------------------------------------------------*/
403 13505 : IF( *Jopt_flag )
404 : {
405 12459 : 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 13505 : **pt_pitch = shl( add( shl( *T0, 2 ), *T0_frac ), 4 );
413 13505 : move16(); /* save subframe pitch values Q6 */
414 :
415 13505 : test();
416 13505 : test();
417 13505 : test();
418 13505 : test();
419 13505 : test();
420 13505 : IF( ( GE_16( tc_subfr, 2 * L_SUBFR ) ) && ( EQ_16( i_subfr, 3 * L_SUBFR ) ) )
421 : {
422 451 : tmp = shl( add( shl( *T0, 2 ), *T0_frac ), 4 );
423 451 : ( *pt_pitch ) -= 3;
424 451 : move16();
425 451 : **pt_pitch = tmp;
426 451 : move16(); /*Q6*/
427 451 : ( *pt_pitch )++;
428 451 : **pt_pitch = tmp;
429 451 : move16(); /*Q6*/
430 451 : ( *pt_pitch )++;
431 451 : move16();
432 451 : **pt_pitch = tmp;
433 451 : move16(); /*Q6*/
434 451 : ( *pt_pitch )++;
435 451 : move16();
436 : }
437 13054 : ELSE IF( ( EQ_16( tc_subfr, L_SUBFR ) ) && ( EQ_16( i_subfr, 2 * L_SUBFR ) ) )
438 : {
439 1019 : tmp = shl( add( shl( *T0, 2 ), *T0_frac ), 4 );
440 1019 : ( *pt_pitch ) -= 2;
441 1019 : move16();
442 1019 : **pt_pitch = tmp;
443 1019 : move16(); /*Q6*/
444 1019 : ( *pt_pitch )++;
445 1019 : **pt_pitch = tmp;
446 1019 : move16(); /*Q6*/
447 1019 : ( *pt_pitch )++;
448 1019 : move16();
449 : }
450 12035 : ELSE IF( ( EQ_16( tc_subfr, TC_0_64 ) ) && ( EQ_16( i_subfr, L_SUBFR ) ) )
451 : {
452 1213 : tmp = shl( add( shl( *T0, 2 ), *T0_frac ), 4 );
453 1213 : ( *pt_pitch ) -= 1;
454 1213 : move16();
455 1213 : **pt_pitch = tmp;
456 1213 : move16(); /*Q6*/
457 1213 : ( *pt_pitch )++;
458 : }
459 10822 : ELSE IF( ( EQ_16( tc_subfr, TC_0_128 ) ) && ( EQ_16( i_subfr, 2 * L_SUBFR ) ) )
460 : {
461 644 : tmp = shl( add( shl( *T0, 2 ), *T0_frac ), 4 );
462 644 : ( *pt_pitch ) -= 2;
463 644 : move16();
464 644 : **pt_pitch = tmp;
465 644 : move16(); /*Q6*/
466 644 : ( *pt_pitch )++;
467 644 : **pt_pitch = tmp;
468 644 : move16(); /*Q6*/
469 644 : ( *pt_pitch )++;
470 644 : move16();
471 : }
472 10178 : ELSE IF( ( EQ_16( tc_subfr, TC_0_192 ) ) && ( EQ_16( i_subfr, 3 * L_SUBFR ) ) )
473 : {
474 201 : tmp = shl( add( shl( *T0, 2 ), *T0_frac ), 4 );
475 201 : ( *pt_pitch ) -= 3;
476 201 : move16();
477 201 : **pt_pitch = tmp;
478 201 : move16(); /*Q6*/
479 201 : ( *pt_pitch )++;
480 201 : **pt_pitch = tmp;
481 201 : move16(); /*Q6*/
482 201 : ( *pt_pitch )++;
483 201 : move16();
484 201 : **pt_pitch = tmp;
485 201 : move16(); /*Q6*/
486 201 : ( *pt_pitch )++;
487 201 : move16();
488 : }
489 : }
490 : ELSE /* L_frame == L_FRAME16k */
491 : {
492 15067 : if ( GE_16( i_subfr, 2 * L_SUBFR ) )
493 : {
494 12411 : limit_flag = 1;
495 12411 : move16();
496 : }
497 :
498 15067 : IF( EQ_16( sub( i_subfr, tc_subfr ), L_SUBFR ) )
499 : {
500 4516 : 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 15067 : nBits = st_fx->acelp_cfg.pitch_bits[i_subfridx];
507 15067 : move16();
508 :
509 15067 : index = (Word16) get_next_indice_fx( st_fx, nBits );
510 :
511 : /*-----------------------------------------------------------------*
512 : * Find adaptive part of excitation, encode pitch period
513 : *-----------------------------------------------------------------*/
514 :
515 15067 : IF( EQ_16( nBits, 10 ) )
516 : {
517 3618 : pit16k_Q_dec_fx( index, nBits, limit_flag, T0, T0_frac, T0_min, T0_max, &st_fx->BER_detect );
518 : }
519 11449 : 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 2656 : IF( LT_16( index, ( PIT16k_FR2_TC0_2SUBFR - PIT16k_MIN ) * 4 ) ) /*(PIT16k_FR2_TC0_2SUBFR-PIT16k_MIN)*4*/
528 : {
529 1553 : *T0 = add( PIT16k_MIN, shr( index, 2 ) );
530 1553 : move16();
531 1553 : temp = shl( sub( *T0, PIT16k_MIN ), 2 );
532 1553 : move16();
533 1553 : *T0_frac = sub( index, temp );
534 1553 : move16();
535 : }
536 : ELSE
537 : {
538 1103 : index = sub( index, ( PIT16k_FR2_TC0_2SUBFR - PIT16k_MIN ) * 4 ); /* (PIT16k_FR2_TC0_2SUBFR-PIT16k_MIN)*4 */
539 1103 : *T0 = add( PIT16k_FR2_TC0_2SUBFR, shr( index, 1 ) );
540 1103 : move16();
541 1103 : temp = shl( sub( *T0, PIT16k_FR2_TC0_2SUBFR ), 1 );
542 1103 : move16();
543 1103 : *T0_frac = shl( sub( index, temp ), 1 );
544 1103 : move16();
545 : }
546 :
547 : /* biterror detection mechanism */
548 2656 : 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 8793 : ELSE IF( EQ_16( nBits, 6 ) )
559 : {
560 8793 : delta_pit_dec_fx( 4, index, T0, T0_frac, *T0_min );
561 : }
562 15067 : IF( EQ_16( nBits, 6 ) )
563 : {
564 8793 : 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 15067 : test();
572 15067 : IF( ( EQ_16( i_subfr, L_SUBFR ) ) && ( EQ_16( *T0, 2 * L_SUBFR ) ) )
573 : {
574 : /* no adaptive excitation in the second subframe */
575 450 : set16_fx( &exc[i_subfr], 0, L_SUBFR + 1 );
576 450 : get_next_indice_fx( st_fx, 1 ); /* this bit is actually not needed */
577 450 : 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 14617 : IF( st_fx->element_mode != EVS_MONO )
583 : {
584 14450 : 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 14617 : offset = tbe_celp_exc_offset( *T0, *T0_frac, L_frame );
591 14617 : move16();
592 14617 : move16(); /* penalty for 2 ptrs initialization */
593 1885593 : 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 1870976 : bwe_exc[add( i, i_subfr * 2 )] = bwe_exc[sub( add( i, i_subfr * 2 ), offset )];
598 : }
599 :
600 14617 : lp_filt_exc_dec_fx( st_fx, MODE1, i_subfr, L_SUBFR, L_frame, st_fx->acelp_cfg.ltf_mode, exc );
601 14617 : *Jopt_flag = 1;
602 14617 : move16();
603 : }
604 :
605 15067 : **pt_pitch = shl( add( shl( *T0, 2 ), *T0_frac ), 4 );
606 15067 : move16(); /* save subframe pitch values Q6 */
607 :
608 : /*---------------------------------------------------------------------*
609 : * fill the pitch buffer - needed for post-processing and FEC_clas_estim()
610 : *---------------------------------------------------------------------*/
611 15067 : test();
612 15067 : test();
613 15067 : 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 7172 : index = shr( i_subfr, 6 );
617 :
618 : /*Ptr */
619 7172 : ( *pt_pitch ) -= index;
620 7172 : move16();
621 :
622 19997 : FOR( i = 0; i < index; i++ )
623 : {
624 12825 : **pt_pitch = shl( add( shl( *T0, 2 ), *T0_frac ), 4 );
625 12825 : move16(); /* save subframe pitch values Q6 */
626 :
627 12825 : ( *pt_pitch )++;
628 : }
629 : }
630 : }
631 : }
632 :
633 58787 : 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 13093 : 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 : #ifndef ISSUE_1866_replace_overflow_libdec
680 : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
681 : Flag Overflow = 0;
682 : move32();
683 : #endif
684 : #endif
685 : /*----------------------------------------------------------------*
686 : * find the number of bits
687 : *----------------------------------------------------------------*/
688 13093 : nBits = st_fx->acelp_cfg.pitch_bits[shr( i_subfr, 6 )];
689 13093 : move16();
690 : /*----------------------------------------------------------------*
691 : * decode parameter T0 (pitch period)
692 : *----------------------------------------------------------------*/
693 13093 : IF( EQ_16( L_frame, L_FRAME ) )
694 : {
695 6678 : test();
696 6678 : test();
697 6678 : test();
698 6678 : test();
699 6678 : test();
700 6678 : 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 ) ) )
701 : {
702 3077 : *T0 = L_SUBFR;
703 3077 : move16();
704 3077 : *T0_frac = 0;
705 3077 : move16();
706 : }
707 3601 : ELSE IF( EQ_16( tc_subfr, 3 * L_SUBFR ) )
708 : {
709 1536 : i = (Word16) get_next_indice_fx( st_fx, nBits );
710 :
711 1536 : IF( EQ_16( nBits, 9 ) )
712 : {
713 1520 : abs_pit_dec_fx( 4, i, 0, T0, T0_frac );
714 : }
715 : ELSE
716 : {
717 16 : abs_pit_dec_fx( 2, i, 0, T0, T0_frac );
718 : }
719 : }
720 : ELSE
721 : {
722 2065 : i = (Word16) get_next_indice_fx( st_fx, nBits );
723 2065 : move16();
724 :
725 2065 : IF( i == 0 )
726 : {
727 295 : *T0 = L_SUBFR;
728 295 : move16();
729 295 : *T0_frac = 0;
730 295 : move16();
731 : }
732 : ELSE
733 : {
734 1770 : IF( EQ_16( tc_subfr, TC_0_0 ) )
735 : {
736 1614 : delta_pit_dec_fx( 2, i, T0, T0_frac, PIT_MIN - 1 );
737 : }
738 : ELSE
739 : {
740 156 : delta_pit_dec_fx( 0, i, T0, T0_frac, PIT_MIN - 1 );
741 : }
742 : }
743 : }
744 : }
745 : ELSE /* L_frame == L_FRAME16k */
746 : {
747 6415 : i = (Word16) get_next_indice_fx( st_fx, nBits );
748 6415 : move16();
749 :
750 6415 : IF( EQ_16( nBits, 10 ) )
751 : {
752 2797 : IF( LT_16( i, ( PIT16k_FR2_EXTEND_10b - PIT16k_MIN_EXTEND ) * 4 ) )
753 : {
754 2731 : *T0 = add( PIT16k_MIN_EXTEND, shr( i, 2 ) );
755 2731 : move16();
756 2731 : temp = shl( sub( *T0, PIT16k_MIN_EXTEND ), 2 );
757 2731 : *T0_frac = sub( i, temp );
758 2731 : move16();
759 : }
760 : ELSE
761 : {
762 66 : index = sub( i, shl( sub( PIT16k_FR2_EXTEND_10b, PIT16k_MIN_EXTEND ), 2 ) );
763 66 : *T0 = add( PIT16k_FR2_EXTEND_10b, shr( index, 1 ) );
764 66 : *T0_frac = sub( index, shl( sub( *T0, PIT16k_FR2_EXTEND_10b ), 1 ) );
765 66 : ( *T0_frac ) = shl( *T0_frac, 1 );
766 : }
767 : }
768 3618 : ELSE IF( EQ_16( nBits, 6 ) )
769 : {
770 3618 : *T0 = add( PIT16k_MIN, shr( i, 1 ) );
771 3618 : move16();
772 3618 : *T0_frac = sub( i, shl( sub( *T0, PIT16k_MIN ), 1 ) );
773 3618 : *T0_frac = shl( *T0_frac, 1 );
774 3618 : move16();
775 : }
776 : }
777 :
778 : /*----------------------------------------------------------------*
779 : * decode other TC parameters
780 : *----------------------------------------------------------------*/
781 :
782 13093 : imp_shape = (Word16) get_next_indice_fx( st_fx, 3 );
783 13093 : move16();
784 13093 : imp_pos = (Word16) get_next_indice_fx( st_fx, 6 );
785 13093 : move16();
786 13093 : imp_sign = (Word16) get_next_indice_fx( st_fx, 1 );
787 13093 : move16();
788 13093 : imp_gain = (Word16) get_next_indice_fx( st_fx, 3 );
789 13093 : move16();
790 :
791 : /*----------------------------------------------------------------*
792 : * - restore gain_trans
793 : * - build glottal codebook contribution
794 : *----------------------------------------------------------------*/
795 :
796 13093 : gain_trans = tbl_gain_trans_tc_fx[imp_gain];
797 13093 : move16();
798 13093 : test();
799 13093 : if ( imp_sign == 0 )
800 : {
801 6483 : gain_trans = negate( gain_trans );
802 6483 : move16();
803 : }
804 :
805 : /* local max for scaling need */
806 : /* maximum of impulse always takes 15 bits (except impulse #7 wich is using 14 bits) */
807 :
808 13093 : tempS = 4;
809 13093 : move16();
810 13093 : test();
811 13093 : if ( LE_16( imp_gain, 3 ) )
812 : {
813 5946 : tempS = 7;
814 5946 : move16();
815 : }
816 :
817 : /* build glottal codebook contribution */
818 13093 : sc = add( 13 - 15, tempS ); /* scaling of curent exc */
819 13093 : sc = sub( *Q_exc, sc );
820 :
821 13093 : pt_shape = &Glottal_cdbk_fx[add( sub( i_mult2( imp_shape, L_IMPULSE ), imp_pos ), L_IMPULSE2 )];
822 13093 : move16();
823 :
824 13093 : j = s_max( 0, sub( imp_pos, L_IMPULSE2 ) );
825 13093 : move16(); /* penalty for exc + i_subfr initialisation */
826 258691 : FOR( i = 0; i < j; i++ )
827 : {
828 245598 : exc[add( i, i_subfr )] = 0;
829 245598 : move16();
830 : }
831 13093 : j = s_min( L_SUBFR, add( imp_pos, L_IMPULSE2 ) );
832 223300 : FOR( ; i <= j; i++ )
833 : {
834 : #ifdef ISSUE_1866_replace_overflow_libdec
835 210207 : exc[i + i_subfr] = round_fx_sat( L_shl_sat( L_mult_sat( pt_shape[i], gain_trans ), sc ) ); /* (Qx * Q14 ) */
836 : #else
837 : exc[i + i_subfr] = round_fx_o( L_shl_o( L_mult_o( pt_shape[i], gain_trans, &Overflow ), sc, &Overflow ), &Overflow ); /* (Qx * Q14 ) */
838 : #endif
839 210207 : move16();
840 : }
841 396003 : FOR( ; i < L_SUBFR; i++ )
842 : {
843 382910 : exc[i + i_subfr] = 0;
844 382910 : move16();
845 : }
846 :
847 : /*--------------------------------------------------------------*
848 : * adapt. search of the second impulse in the same subframe
849 : * (when appears)
850 : *--------------------------------------------------------------*/
851 :
852 13093 : pred_lt4_tc_fx( exc, *T0, *T0_frac, inter4_2_fx, imp_pos, i_subfr );
853 13093 : IF( st_fx->hBWE_TD != NULL ){
854 13093 : IF( EQ_16( L_frame, L_FRAME ) ){
855 6678 : interp_code_5over2_fx( &exc[i_subfr], &bwe_exc[i_subfr * HIBND_ACB_L_FAC], L_SUBFR );
856 : }
857 : ELSE
858 : {
859 6415 : interp_code_4over2_fx( &exc[i_subfr], &bwe_exc[i_subfr * 2], L_SUBFR );
860 : }
861 : }
862 13093 : *position = add( imp_pos, i_subfr );
863 13093 : move16();
864 13093 : return;
865 : }
866 :
867 : /*-------------------------------------------------------------------*
868 : * tc_classif_fx()
869 : *
870 : * TC subframe classification decoding
871 : *-------------------------------------------------------------------*/
872 :
873 13093 : Word16 tc_classif_fx( /*o: Q0*/
874 : Decoder_State *st_fx, /* i/o: decoder state structure */
875 : const Word16 L_frame /* i : length of the frame Q0 */
876 : )
877 : {
878 : Word16 tc_subfr, indice;
879 : (void) ( L_frame );
880 13093 : IF( EQ_16( st_fx->L_frame, L_FRAME ) )
881 : {
882 6678 : IF( get_next_indice_fx( st_fx, 1 ) )
883 : {
884 1614 : tc_subfr = TC_0_0;
885 1614 : move16();
886 : }
887 : ELSE
888 : {
889 5064 : IF( get_next_indice_fx( st_fx, 1 ) )
890 : {
891 2058 : tc_subfr = 0;
892 2058 : move16();
893 :
894 2058 : IF( get_next_indice_fx( st_fx, 1 ) )
895 : {
896 201 : tc_subfr = TC_0_192;
897 201 : move16();
898 : }
899 : ELSE
900 : {
901 1857 : tc_subfr = TC_0_128;
902 1857 : move16();
903 1857 : if ( get_next_indice_fx( st_fx, 1 ) )
904 : {
905 1213 : tc_subfr = TC_0_64;
906 1213 : move16();
907 : }
908 : }
909 : }
910 : ELSE
911 : {
912 3006 : IF( get_next_indice_fx( st_fx, 1 ) )
913 : {
914 1019 : tc_subfr = L_SUBFR;
915 1019 : move16();
916 : }
917 : ELSE
918 : {
919 1987 : tc_subfr = 3 * L_SUBFR;
920 1987 : move16();
921 1987 : if ( get_next_indice_fx( st_fx, 1 ) )
922 : {
923 451 : tc_subfr = 2 * L_SUBFR;
924 451 : move16();
925 : }
926 : }
927 : }
928 : }
929 : }
930 : ELSE /* L_frame == L_FRAME16k */
931 : {
932 6415 : indice = (Word16) get_next_indice_fx( st_fx, 2 );
933 :
934 6415 : IF( LT_16( indice, 3 ) )
935 : {
936 4277 : tc_subfr = shl( indice, 6 );
937 : }
938 : ELSE
939 : {
940 2138 : tc_subfr = 4 * L_SUBFR;
941 2138 : move16();
942 2138 : if ( get_next_indice_fx( st_fx, 1 ) == 0 )
943 : {
944 239 : tc_subfr = 3 * L_SUBFR;
945 239 : move16();
946 : }
947 : }
948 : }
949 :
950 13093 : return ( tc_subfr );
951 : }
|