Line data Source code
1 : /*====================================================================================
2 : EVS Codec 3GPP TS26.452 Aug 12, 2021. Version 16.3.0
3 : ====================================================================================*/
4 :
5 : #include <assert.h>
6 : #include <stdint.h>
7 : #include "options.h"
8 : #include "cnst.h"
9 : #include "prot_fx.h"
10 : #include "rom_com.h"
11 : #define inv_T0_res InvIntTable
12 :
13 : /*----------------------------------------------------------*
14 : * Mode2_pit_decode()
15 : *
16 : * Decode pitch lag
17 : *----------------------------------------------------------*/
18 :
19 3050 : Word32 Mode2_pit_decode( /* o: pitch value Q16 */
20 : const Word16 coder_type, /* i: coding model */
21 : Word16 i_subfr, /* i: subframe index */
22 : Word16 L_subfr,
23 : Word16 **pt_indice, /* i/o: quantization indices pointer */
24 : Word16 *T0, /* i/o: close loop integer pitch Q0 */
25 : Word16 *T0_frac, /* o: close loop fractional part of the pitch Q0 */
26 : Word16 *T0_res, /* i/o: pitch resolution Q0 */
27 : Word16 *T0_min, /* i/o: lower limit for close-loop search Q0 */
28 : Word16 *T0_min_frac, /* i/o: lower limit for close-loop search Q0 */
29 : Word16 *T0_max, /* i/o: higher limit for close-loop search Q0 */
30 : Word16 *T0_max_frac, /* i/o: higher limit for close-loop search Q0 */
31 : Word16 pit_min,
32 : Word16 pit_fr1,
33 : Word16 pit_fr1b,
34 : Word16 pit_fr2,
35 : Word16 pit_max,
36 : Word16 pit_res_max )
37 : {
38 : Word32 pitch;
39 :
40 3050 : IF( coder_type == 0 )
41 : {
42 0 : *T0 = L_subfr;
43 0 : move16();
44 0 : *T0_frac = 0;
45 0 : move16();
46 0 : *T0_res = 1;
47 0 : move16();
48 : }
49 3050 : ELSE IF( EQ_16( coder_type, 1 ) ) /* 8/4/4/4 (EVS) */
50 : {
51 0 : IF( i_subfr == 0 )
52 : {
53 0 : Mode2_abs_pit_dec( T0, T0_frac, T0_res, pt_indice, pit_min, pit_fr1b, pit_min, pit_res_max );
54 : }
55 : ELSE
56 : {
57 0 : limit_T0_voiced( 4, shr( pit_res_max, 1 ), *T0, *T0_frac, *T0_res, T0_min, T0_min_frac, T0_max, T0_max_frac, pit_min, pit_max );
58 0 : *T0_res = shr( pit_res_max, 1 );
59 0 : move16();
60 0 : Mode2_delta_pit_dec( T0, T0_frac, *T0_res, T0_min, T0_min_frac, pt_indice );
61 : }
62 : }
63 3050 : ELSE IF( EQ_16( coder_type, 2 ) ) /* 8/5/8/5 (EVS) */
64 : {
65 0 : test();
66 0 : IF( ( i_subfr == 0 ) || ( EQ_16( i_subfr, shl( L_subfr, 1 ) ) ) )
67 : {
68 0 : Mode2_abs_pit_dec( T0, T0_frac, T0_res, pt_indice, pit_min, pit_fr1b, pit_min, pit_res_max );
69 : }
70 : ELSE
71 : {
72 0 : limit_T0_voiced( 5, shr( pit_res_max, 1 ), *T0, *T0_frac, *T0_res, T0_min, T0_min_frac, T0_max, T0_max_frac, pit_min, pit_max );
73 0 : *T0_res = shr( pit_res_max, 1 );
74 0 : move16();
75 0 : Mode2_delta_pit_dec( T0, T0_frac, *T0_res, T0_min, T0_min_frac, pt_indice );
76 : }
77 : }
78 3050 : ELSE IF( EQ_16( coder_type, 3 ) ) /* 9/6/6/6 (HRs- VC) */
79 : {
80 1340 : Word16 pit_res_max2 = pit_res_max;
81 1340 : move16();
82 1340 : if ( EQ_16( pit_min, PIT_MIN_16k ) )
83 : {
84 1340 : pit_res_max2 = shr( pit_res_max, 1 );
85 : }
86 :
87 1340 : IF( ( i_subfr == 0 ) )
88 : {
89 268 : Mode2_abs_pit_dec( T0, T0_frac, T0_res, pt_indice, pit_min, pit_fr1, pit_fr2, pit_res_max );
90 : }
91 : ELSE
92 : {
93 1072 : limit_T0_voiced( 6, pit_res_max2, *T0, 0, 1, T0_min, T0_min_frac, T0_max, T0_max_frac, pit_min, pit_max );
94 1072 : *T0_res = pit_res_max2;
95 1072 : move16();
96 1072 : Mode2_delta_pit_dec( T0, T0_frac, *T0_res, T0_min, T0_min_frac, pt_indice );
97 : }
98 : }
99 1710 : ELSE IF( EQ_16( coder_type, 4 ) ) /* 9/6/9/6 (AMRWB) */
100 : {
101 1710 : Word16 pit_res_max2 = pit_res_max;
102 1710 : move16();
103 1710 : if ( EQ_16( pit_min, PIT_MIN_16k ) )
104 : {
105 1710 : pit_res_max2 = shr( pit_res_max, 1 );
106 : }
107 1710 : test();
108 1710 : IF( ( i_subfr == 0 ) || ( EQ_16( i_subfr, shl( L_subfr, 1 ) ) ) )
109 : {
110 684 : Mode2_abs_pit_dec( T0, T0_frac, T0_res, pt_indice, pit_min, pit_fr1, pit_fr2, pit_res_max );
111 : }
112 : ELSE
113 : {
114 1026 : limit_T0_voiced( 6, pit_res_max2, *T0, 0, 1, T0_min, T0_min_frac, T0_max, T0_max_frac, pit_min, pit_max );
115 1026 : *T0_res = pit_res_max2;
116 1026 : move16();
117 1026 : Mode2_delta_pit_dec( T0, T0_frac, *T0_res, T0_min, T0_min_frac, pt_indice );
118 : }
119 : }
120 0 : ELSE IF( EQ_16( coder_type, 8 ) ) /* 8/5/5/5 (RF all pred mode) */
121 : {
122 0 : IF( i_subfr == 0 )
123 : {
124 0 : Mode2_abs_pit_dec( T0, T0_frac, T0_res, pt_indice, pit_min, pit_fr1b, pit_min, pit_res_max );
125 : }
126 : ELSE
127 : {
128 0 : limit_T0_voiced( 5, shr( pit_res_max, 1 ), *T0, *T0_frac, *T0_res, T0_min, T0_min_frac, T0_max, T0_max_frac, pit_min, pit_max );
129 0 : *T0_res = shr( pit_res_max, 1 );
130 0 : move16();
131 0 : Mode2_delta_pit_dec( T0, T0_frac, *T0_res, T0_min, T0_min_frac, pt_indice );
132 : }
133 : }
134 0 : ELSE IF( EQ_16( coder_type, 9 ) ) /* 8/0/8/0 (RF gen pred mode) */
135 : {
136 0 : IF( i_subfr == 0 )
137 : {
138 0 : Mode2_abs_pit_dec( T0, T0_frac, T0_res, pt_indice, pit_min, pit_fr1b, pit_min, pit_res_max );
139 : }
140 : ELSE
141 : {
142 0 : limit_T0_voiced( 4, shr( pit_res_max, 1 ), *T0, *T0_frac, *T0_res, T0_min, T0_min_frac, T0_max, T0_max_frac, pit_min, pit_max );
143 0 : *T0_res = shr( pit_res_max, 1 );
144 0 : move16();
145 0 : Mode2_delta_pit_dec( T0, T0_frac, *T0_res, T0_min, T0_min_frac, pt_indice );
146 : }
147 : }
148 : ELSE
149 : {
150 0 : assert( 0 && "LTP mode not supported" );
151 : }
152 :
153 : /*-------------------------------------------------------*
154 : * Compute floating pitch output
155 : *-------------------------------------------------------*/
156 3050 : assert( *T0_res > 0 || *T0_res <= 6 );
157 :
158 : /*pitch = (float)(*T0) + (float)(*T0_frac)/(float)(*T0_res);*/ /* save subframe pitch values */
159 3050 : pitch = L_mac( L_deposit_h( *T0 ), *T0_frac, inv_T0_res[*T0_res] ); // Q16
160 :
161 :
162 3050 : return pitch;
163 : }
164 :
165 :
166 : /*---------------------------------------------------------------------*
167 : * Mode2_abs_pit_dec()
168 : *
169 : * Decode the absolute pitch
170 : *---------------------------------------------------------------------*/
171 :
172 952 : void Mode2_abs_pit_dec(
173 : Word16 *T0, /* o: integer pitch lag Q0 */
174 : Word16 *T0_frac, /* o: pitch fraction Q0 */
175 : Word16 *T0_res, /* o: pitch resolution Q0 */
176 : Word16 **pt_indice, /* i/o: pointer to Vector of Q indexes */
177 : Word16 pit_min,
178 : Word16 pit_fr1,
179 : Word16 pit_fr2,
180 : Word16 pit_res_max )
181 : {
182 : Word16 index;
183 : Word16 pit_res_max_half, tmp1, tmp2, res;
184 :
185 952 : index = **pt_indice;
186 952 : move16();
187 952 : ( *pt_indice )++;
188 952 : move16();
189 952 : pit_res_max_half = shr( pit_res_max, 1 );
190 :
191 952 : tmp1 = i_mult( sub( pit_fr2, pit_min ), pit_res_max );
192 952 : tmp2 = i_mult( sub( pit_fr1, pit_fr2 ), pit_res_max_half );
193 952 : IF( LT_16( index, tmp1 ) )
194 : {
195 0 : assert( pit_res_max > 1 && pit_res_max <= 6 );
196 :
197 0 : res = pit_res_max;
198 0 : move16();
199 0 : IF( EQ_16( pit_res_max, 6 ) )
200 : {
201 0 : res = shr( res, 1 );
202 : }
203 :
204 0 : *T0 = mult( index, inv_T0_res[res] );
205 0 : move16();
206 0 : IF( EQ_16( pit_res_max, 6 ) )
207 : {
208 0 : *T0 = shr( *T0, 1 );
209 0 : move16();
210 : }
211 :
212 0 : *T0 = add( pit_min, *T0 );
213 0 : move16();
214 :
215 0 : *T0_frac = sub( index, i_mult( sub( *T0, pit_min ), pit_res_max ) );
216 0 : move16();
217 0 : *T0_res = pit_res_max;
218 0 : move16();
219 : }
220 952 : ELSE IF( LT_16( index, add( tmp1, tmp2 ) ) )
221 : {
222 842 : assert( pit_res_max > 1 );
223 :
224 842 : index = sub( index, tmp1 );
225 842 : *T0 = add( pit_fr2, mult( index, inv_T0_res[pit_res_max_half] ) );
226 842 : move16();
227 842 : *T0_frac = sub( index, i_mult( sub( *T0, pit_fr2 ), pit_res_max_half ) );
228 842 : move16();
229 842 : *T0_res = pit_res_max_half;
230 842 : move16();
231 : }
232 : ELSE
233 : {
234 110 : *T0 = add( index, sub( pit_fr1, add( tmp1, tmp2 ) ) );
235 110 : move16();
236 110 : *T0_frac = 0;
237 110 : move16();
238 110 : *T0_res = 1;
239 110 : move16();
240 : }
241 :
242 952 : return;
243 : }
244 :
245 :
246 : /*---------------------------------------------------------------------*
247 : * Routine Mode2_delta_pit_dec()
248 : *
249 : * Decode delta pitch
250 : *---------------------------------------------------------------------*/
251 2098 : void Mode2_delta_pit_dec(
252 : Word16 *T0, /* o: integer pitch lag Q0 */
253 : Word16 *T0_frac, /* o: pitch fraction Q0 */
254 : Word16 T0_res, /* i: pitch resolution Q0 */
255 : Word16 *T0_min, /* i/o: delta search min Q0 */
256 : Word16 *T0_min_frac, /* i: delta search min Q0 */
257 : Word16 **pt_indice /* i/o: pointer to Vector of Q indexes */
258 : )
259 : {
260 : Word16 index, res;
261 :
262 2098 : assert( T0_res > 1 && T0_res <= 6 );
263 :
264 2098 : res = T0_res;
265 2098 : move16();
266 2098 : if ( EQ_16( T0_res, 6 ) )
267 : {
268 0 : res = shr( res, 1 );
269 : }
270 :
271 2098 : index = **pt_indice;
272 2098 : move16();
273 2098 : ( *pt_indice )++;
274 2098 : move16();
275 :
276 :
277 2098 : *T0 = mult( add( index, *T0_min_frac ), inv_T0_res[res] );
278 2098 : move16();
279 2098 : IF( EQ_16( T0_res, 6 ) )
280 : {
281 0 : *T0 = shr( *T0, 1 );
282 0 : move16();
283 : }
284 :
285 2098 : *T0 = add( *T0_min, *T0 );
286 2098 : move16();
287 :
288 2098 : *T0_frac = add( index, sub( *T0_min_frac, i_mult( sub( *T0, *T0_min ), T0_res ) ) );
289 2098 : move16();
290 :
291 2098 : return;
292 : }
293 :
294 :
295 : /*======================================================================*/
296 : /* FUNCTION : pit_decode_fx() */
297 : /*-----------------------------------------------------------------------*/
298 : /* PURPOSE : calculate pitch value */
299 : /* */
300 : /*-----------------------------------------------------------------------*/
301 : /* INPUT ARGUMENTS : */
302 : /* _ (Word32) core_brate : Core bitrate Q0 */
303 : /* _ (Word16) Opt_AMR_WB : flag indicating AMR-WB IO mode Q0 */
304 : /* _ (Word16) L_frame : length of the frame Q0 */
305 : /* _ (Word16) i_subfr : length of the frame Q0 */
306 : /* _ (Word16) coder_type : coding type Q0 */
307 : /* _ (Word16) L_subfr : subframe length */
308 : /*-----------------------------------------------------------------------*/
309 : /* OUTPUT ARGUMENTS : */
310 : /* _ (Word16 *) T0 : close loop integer pitch */
311 : /* _ (Word16 *) T0_frac : close loop fractional part of the pitch */
312 : /* _ (Word16 ) pitch : pitch value Q6 */
313 : /*-----------------------------------------------------------------------*/
314 : /* INPUT OUTPUT ARGUMENTS */
315 : /* _ (Word16 *) T0_min : delta search min for sf 2 & 4 */
316 : /* _ (Word16 *) T0_max : delta search max for sf 2 & 4 */
317 : /*-----------------------------------------------------------------------*/
318 : /* RETURN ARGUMENTS : */
319 : /* _ (Word16 ) pitch : close loop integer pitch Q6 */
320 : /*=======================================================================*/
321 :
322 : /*! r: floating pitch value */
323 503517 : Word16 pit_decode_fx(
324 : Decoder_State *st_fx, /* i/o: decoder state structure */
325 : const Word32 core_brate, /* i : core bitrate */
326 : const Word16 Opt_AMR_WB, /* i : flag indicating AMR-WB IO mode */
327 : const Word16 L_frame, /* i : length of the frame */
328 : Word16 i_subfr, /* i : subframe index */
329 : const Word16 coder_type, /* i : coding type */
330 : Word16 *limit_flag, /* i/o: restrained(0) or extended(1) Q limits */
331 : Word16 *T0, /* o : close loop integer pitch */
332 : Word16 *T0_frac, /* o : close loop fractional part of the pitch */
333 : Word16 *T0_min, /* i/o: delta search min for sf 2 & 4 */
334 : Word16 *T0_max, /* i/o: delta search max for sf 2 & 4 */
335 : const Word16 L_subfr, /* i : subframe length */
336 : const Word16 tdm_Pitch_reuse_flag, /* i : primary channel pitch reuse flag */
337 : const Word16 tdm_Pri_pitch_buf[] /* i : primary channel pitch buffer Q6 */
338 : )
339 : {
340 : Word16 pitch; /*Q2*/
341 : Word16 pitch_index, nBits, pit_flag, tmp;
342 :
343 503517 : pitch_index = 0;
344 503517 : move16();
345 :
346 : /*----------------------------------------------------------------*
347 : * Set pit_flag = 0 for every subframe with absolute pitch search
348 : *----------------------------------------------------------------*/
349 503517 : pit_flag = i_subfr;
350 503517 : move16();
351 :
352 503517 : if ( EQ_16( i_subfr, PIT_DECODE_2XL_SUBFR ) )
353 : {
354 111114 : pit_flag = 0;
355 111114 : move16();
356 : }
357 :
358 : /*-------------------------------------------------------*
359 : * Retrieve the pitch index
360 : *-------------------------------------------------------*/
361 503517 : IF( !Opt_AMR_WB )
362 : {
363 : /*----------------------------------------------------------------*
364 : * pitch Q: Set limit_flag to 0 for restrained limits, and 1 for extended limits
365 : *----------------------------------------------------------------*/
366 503517 : test();
367 503517 : test();
368 503517 : IF( i_subfr == 0 )
369 : {
370 117265 : *limit_flag = 1;
371 117265 : move16();
372 :
373 117265 : if ( EQ_16( coder_type, VOICED ) )
374 : {
375 7412 : *limit_flag = 2;
376 7412 : move16(); /* double-extended limits */
377 : }
378 117265 : test();
379 117265 : if ( EQ_16( coder_type, GENERIC ) && EQ_32( core_brate, ACELP_7k20 ) )
380 : {
381 921 : *limit_flag = 0;
382 921 : move16();
383 : }
384 : }
385 386252 : ELSE IF( EQ_16( i_subfr, 2 * L_SUBFR ) && EQ_32( coder_type, GENERIC ) && LE_32( core_brate, ACELP_13k20 ) )
386 : {
387 30994 : if ( GT_16( *T0, shr( add( PIT_FR1_EXTEND_8b, PIT_MIN ), 1 ) ) )
388 : {
389 23378 : *limit_flag = 0;
390 23378 : move16();
391 : }
392 : }
393 :
394 : /*-------------------------------------------------------*
395 : * Retrieve the number of Q bits
396 : *-------------------------------------------------------*/
397 :
398 503517 : nBits = 0;
399 503517 : move16();
400 503517 : IF( NE_16( coder_type, AUDIO ) )
401 : {
402 : /* find the number of bits */
403 484210 : IF( i_subfr )
404 : {
405 376844 : tmp = idiv1616( i_subfr, L_subfr );
406 376844 : nBits = st_fx->acelp_cfg.pitch_bits[tmp];
407 : }
408 : ELSE
409 : {
410 107366 : nBits = st_fx->acelp_cfg.pitch_bits[0];
411 : }
412 484210 : move16();
413 484210 : pitch_index = (Word16) get_next_indice_fx( st_fx, nBits );
414 : }
415 :
416 : /*-------------------------------------------------------*
417 : * Pitch decoding in AUDIO mode
418 : * (both ACELP@12k8 and ACELP@16k cores)
419 : *-------------------------------------------------------*/
420 503517 : test();
421 503517 : test();
422 503517 : IF( EQ_16( coder_type, AUDIO ) )
423 : {
424 19307 : test();
425 19307 : if ( EQ_16( L_subfr, L_FRAME / 2 ) && i_subfr != 0 )
426 : {
427 2490 : pit_flag = L_SUBFR;
428 2490 : move16();
429 : }
430 19307 : if ( pit_flag == 0 )
431 : {
432 12205 : nBits = 10;
433 12205 : move16();
434 : }
435 19307 : if ( pit_flag != 0 )
436 : {
437 7102 : nBits = 6;
438 7102 : move16();
439 : }
440 :
441 19307 : pitch_index = (Word16) get_next_indice_fx( st_fx, nBits );
442 :
443 19307 : test();
444 19307 : test();
445 19307 : IF( EQ_16( L_subfr, L_FRAME / 2 ) && i_subfr != 0 && GE_16( pitch_index, 32 ) ) /* safety check in case of bit errors */
446 : {
447 0 : pitch_index = shr( pitch_index, 1 );
448 0 : st_fx->BER_detect = 1;
449 0 : move16();
450 : }
451 :
452 19307 : pit_Q_dec_fx( 0, pitch_index, nBits, 4, pit_flag, *limit_flag, T0, T0_frac, T0_min, T0_max, &st_fx->BER_detect );
453 : }
454 484210 : ELSE IF( EQ_16( coder_type, VOICED ) )
455 : {
456 : /*-------------------------------------------------------*
457 : * Pitch decoding in VOICED mode
458 : * (ACELP@12k8 core only)
459 : *-------------------------------------------------------*/
460 29648 : if ( EQ_16( i_subfr, 2 * L_SUBFR ) )
461 : {
462 7412 : pit_flag = i_subfr;
463 7412 : move16();
464 : }
465 :
466 29648 : pit_Q_dec_fx( 0, pitch_index, nBits, 4, pit_flag, *limit_flag, T0, T0_frac, T0_min, T0_max, &st_fx->BER_detect );
467 : }
468 454562 : ELSE IF( EQ_16( st_fx->idchan, 1 ) && ( EQ_16( tdm_Pitch_reuse_flag, 1 ) || EQ_16( nBits, 4 ) ) )
469 132 : {
470 : /*-------------------------------------------------------*
471 : * Pitch decoding with reusing of primary channel information
472 : *-------------------------------------------------------*/
473 : Word16 loc_T0, loc_frac, delta, pit_tmp1, pit_tmp2, isubfridx;
474 :
475 132 : delta = 4;
476 132 : pit_flag = L_SUBFR;
477 132 : move16();
478 132 : move16();
479 132 : isubfridx = shr( i_subfr, 6 );
480 132 : IF( EQ_16( L_subfr, 2 * L_SUBFR ) )
481 : {
482 0 : move16();
483 0 : move16();
484 0 : pit_tmp1 = tdm_Pri_pitch_buf[isubfridx]; /*tdm_Pri_pitch_buf in Q6 ->pit_tmp1 and 2 in Q6 too */
485 0 : pit_tmp2 = tdm_Pri_pitch_buf[( i_subfr + 1 ) / L_SUBFR];
486 : /*loc_T0 = (int16_t)(0.5f * tdm_Pri_pitch_buf[i_subfr / L_SUBFR] + 0.5f * tdm_Pri_pitch_buf[(i_subfr + L_SUBFR) / L_SUBFR]);*/
487 0 : loc_T0 = mac_r( L_mult( 16384, pit_tmp1 ), 16384, pit_tmp2 );
488 : /*loc_frac = (int16_t)(((0.5f * tdm_Pri_pitch_buf[i_subfr / L_SUBFR] + 0.5f * tdm_Pri_pitch_buf[(i_subfr + L_SUBFR) / L_SUBFR]) - loc_T0) * 4.0f);*/
489 : }
490 : ELSE
491 : {
492 : /*loc_T0 = (int16_t)tdm_Pri_pitch_buf[i_subfr / L_SUBFR];*/
493 132 : loc_T0 = tdm_Pri_pitch_buf[isubfridx]; /*Q6*/
494 132 : move16();
495 : /*loc_frac = (int16_t)((tdm_Pri_pitch_buf[i_subfr / L_SUBFR] - loc_T0) * 4.0f);*/
496 : }
497 132 : loc_frac = shr( sub( loc_T0, shl( shr( loc_T0, 6 ), 6 ) ), 4 ); /* Final result in Q 2*/
498 132 : loc_T0 = shr( loc_T0, 6 ); /*Q6 -> Q0*/
499 :
500 :
501 132 : limit_T0_fx( L_FRAME, delta, pit_flag, *limit_flag, loc_T0, loc_frac, T0_min, T0_max );
502 :
503 132 : IF( nBits > 0 )
504 : {
505 0 : pit_Q_dec_fx( 0, pitch_index, nBits, delta, pit_flag, *limit_flag, T0, T0_frac, T0_min, T0_max, &st_fx->BER_detect );
506 : }
507 : ELSE
508 : {
509 132 : *T0 = loc_T0;
510 132 : *T0_frac = loc_frac;
511 132 : move16();
512 132 : move16();
513 : }
514 : }
515 : ELSE
516 : {
517 : /*-------------------------------------------------------*
518 : * Pitch decoding in GENERIC mode
519 : * (both ACELP@12k8 and ACELP@16k cores)
520 : *-------------------------------------------------------*/
521 454430 : IF( EQ_16( L_frame, L_FRAME ) )
522 : {
523 162947 : pit_Q_dec_fx( 0, pitch_index, nBits, 8, pit_flag, *limit_flag, T0, T0_frac, T0_min, T0_max, &st_fx->BER_detect );
524 : }
525 : ELSE
526 : {
527 291483 : pit16k_Q_dec_fx( pitch_index, nBits, *limit_flag, T0, T0_frac, T0_min, T0_max, &st_fx->BER_detect );
528 : }
529 : }
530 : }
531 :
532 : /*-------------------------------------------------------*
533 : * Pitch decoding in AMR-WB IO mode
534 : *-------------------------------------------------------*/
535 :
536 : ELSE
537 : {
538 0 : *limit_flag = 0;
539 0 : move16();
540 0 : test();
541 0 : test();
542 0 : IF( i_subfr == 0 || ( EQ_16( i_subfr, 2 * L_SUBFR ) && EQ_32( core_brate, ACELP_8k85 ) ) )
543 : {
544 0 : nBits = 8;
545 0 : move16();
546 : }
547 : ELSE
548 : {
549 0 : nBits = 5;
550 0 : move16();
551 : }
552 0 : IF( GT_32( core_brate, ACELP_8k85 ) )
553 : {
554 0 : nBits = 6;
555 0 : move16();
556 0 : test();
557 0 : if ( i_subfr == 0 || EQ_16( i_subfr, 2 * L_SUBFR ) )
558 : {
559 0 : nBits = 9;
560 0 : move16();
561 : }
562 : }
563 :
564 0 : pitch_index = (Word16) get_next_indice_fx( st_fx, nBits );
565 :
566 0 : pit_Q_dec_fx( 1, pitch_index, nBits, 8, pit_flag, *limit_flag, T0, T0_frac, T0_min, T0_max, &st_fx->BER_detect );
567 : }
568 :
569 : /*-------------------------------------------------------*
570 : * Compute floating pitch output
571 : *-------------------------------------------------------*/
572 :
573 503517 : pitch = shl( add( shl( *T0, 2 ), *T0_frac ), 4 ); /* save subframe pitch values Q6 */
574 :
575 503517 : return pitch;
576 : }
577 :
578 : /*----------------------------------------------------------*
579 : * pit_Q_dec_fx()
580 : *
581 : * Decode pitch lag
582 : *----------------------------------------------------------*/
583 :
584 221313 : void pit_Q_dec_fx(
585 : const Word16 Opt_AMR_WB, /* i : flag indicating AMR-WB IO mode */
586 : const Word16 pitch_index, /* i : pitch index */
587 : const Word16 nBits, /* i : # of Q bits */
588 : const Word16 delta, /* i : Half the CL searched interval */
589 : const Word16 pit_flag, /* i : absolute(0) or delta(1) pitch Q */
590 : const Word16 limit_flag, /* i : restrained(0) or extended(1) Q limits */
591 : Word16 *T0, /* o : integer pitch lag */
592 : Word16 *T0_frac, /* o : pitch fraction */
593 : Word16 *T0_min, /* i/o: delta search min */
594 : Word16 *T0_max /* i/o: delta search max */
595 : ,
596 : Word16 *BER_detect /* o : BER detect flag */
597 : )
598 : {
599 221313 : IF( EQ_16( nBits, 10 ) ) /* absolute decoding with 10 bits */
600 : {
601 66803 : IF( limit_flag == 0 )
602 : {
603 0 : *T0 = add( PIT_MIN, shr( pitch_index, 2 ) );
604 0 : move16();
605 0 : *T0_frac = sub( pitch_index, shl( sub( *T0, PIT_MIN ), 2 ) );
606 0 : move16();
607 : }
608 66803 : ELSE IF( EQ_16( limit_flag, 1 ) )
609 : {
610 59795 : *T0 = add( PIT_MIN_EXTEND, shr( pitch_index, 2 ) );
611 59795 : move16();
612 59795 : *T0_frac = sub( pitch_index, shl( sub( *T0, PIT_MIN_EXTEND ), 2 ) );
613 59795 : move16();
614 : }
615 : ELSE /* limit_flag == 2 */
616 : {
617 7008 : *T0 = add( PIT_MIN_DOUBLEEXTEND, shr( pitch_index, 2 ) );
618 7008 : move16();
619 7008 : *T0_frac = sub( pitch_index, shl( sub( *T0, PIT_MIN_DOUBLEEXTEND ), 2 ) );
620 7008 : move16();
621 : }
622 : }
623 154510 : ELSE IF( EQ_16( nBits, 9 ) ) /* absolute decoding with 9 bits */
624 : {
625 31442 : abs_pit_dec_fx( 4, pitch_index, limit_flag, T0, T0_frac );
626 :
627 : /* find T0_min and T0_max for delta search */
628 31442 : IF( Opt_AMR_WB )
629 : {
630 0 : limit_T0_fx( L_FRAME, delta, pit_flag, 0, *T0, 0, T0_min, T0_max ); /* T0_frac==0 to keep IO with AMR-WB */
631 : }
632 : }
633 123068 : ELSE IF( EQ_16( nBits, 8 ) ) /* absolute decoding with 8 bits */
634 : {
635 4391 : abs_pit_dec_fx( 2, pitch_index, limit_flag, T0, T0_frac );
636 :
637 : /* find T0_min and T0_max for delta search */
638 4391 : IF( Opt_AMR_WB )
639 : {
640 0 : limit_T0_fx( L_FRAME, delta, pit_flag, 0, *T0, 0, T0_min, T0_max ); /* T0_frac==0 to keep IO with AMR-WB */
641 : }
642 : }
643 118677 : ELSE IF( EQ_16( nBits, 6 ) ) /* relative decoding with 6 bits */
644 : {
645 89675 : delta_pit_dec_fx( 4, pitch_index, T0, T0_frac, *T0_min );
646 : }
647 29002 : ELSE IF( EQ_16( nBits, 5 ) ) /* relative decoding with 5 bits */
648 : {
649 29002 : IF( EQ_16( delta, 8 ) )
650 : {
651 6766 : delta_pit_dec_fx( 2, pitch_index, T0, T0_frac, *T0_min );
652 : }
653 : ELSE /* delta == 4 */
654 : {
655 22236 : delta_pit_dec_fx( 4, pitch_index, T0, T0_frac, *T0_min );
656 : }
657 : }
658 : ELSE /* nBits == 4 */ /* relative decoding with 4 bits */
659 : {
660 0 : IF( EQ_16( delta, 8 ) )
661 : {
662 0 : delta_pit_dec_fx( 0, pitch_index, T0, T0_frac, *T0_min );
663 : }
664 : ELSE /* delta == 4 */
665 : {
666 0 : delta_pit_dec_fx( 2, pitch_index, T0, T0_frac, *T0_min );
667 : }
668 : }
669 :
670 : /* biterror detection mechanism */
671 221313 : test();
672 221313 : test();
673 221313 : IF( GT_16( add( shl( *T0, 2 ), *T0_frac ), ( PIT_MAX << 2 ) + 2 ) && pit_flag == 0 && !Opt_AMR_WB )
674 : {
675 0 : *T0 = L_SUBFR;
676 0 : move16();
677 0 : *T0_frac = 0;
678 0 : move16();
679 0 : *BER_detect = 1;
680 0 : move16();
681 : }
682 :
683 221313 : IF( !Opt_AMR_WB )
684 : {
685 : /* find T0_min and T0_max for delta search */
686 221313 : limit_T0_fx( L_FRAME, delta, L_SUBFR, limit_flag, *T0, *T0_frac, T0_min, T0_max );
687 : }
688 :
689 221313 : return;
690 : }
691 :
692 : /*-------------------------------------------------*
693 : * pit16k_Q_dec()
694 : *
695 : * pitch decoding @16kHz core
696 : *-------------------------------------------------*/
697 :
698 304002 : void pit16k_Q_dec_fx(
699 : const Word16 pitch_index, /* i : pitch index */
700 : const Word16 nBits, /* i : # of Q bits */
701 : const Word16 limit_flag, /* i : restrained(0) or extended(1) limits */
702 : Word16 *T0, /* o : integer pitch lag */
703 : Word16 *T0_frac, /* o : pitch fraction */
704 : Word16 *T0_min, /* i/o: delta search min */
705 : Word16 *T0_max /* i/o: delta search max */
706 : ,
707 : Word16 *BER_detect /* o : BER detect flag */
708 : )
709 : {
710 : Word16 index;
711 :
712 304002 : IF( EQ_16( nBits, 10 ) ) /* absolute decoding with 10 bits */
713 : {
714 : {
715 129738 : IF( LT_16( pitch_index, shl( ( PIT16k_FR2_EXTEND_10b - PIT16k_MIN_EXTEND ), 2 ) ) )
716 : {
717 126295 : *T0 = add( PIT16k_MIN_EXTEND, shr( pitch_index, 2 ) );
718 126295 : move16();
719 126295 : *T0_frac = sub( pitch_index, shl( sub( *T0, PIT16k_MIN_EXTEND ), 2 ) );
720 126295 : move16();
721 : }
722 : ELSE
723 : {
724 3443 : index = sub( pitch_index, shl( ( PIT16k_FR2_EXTEND_10b - PIT16k_MIN_EXTEND ), 2 ) );
725 3443 : *T0 = add( PIT16k_FR2_EXTEND_10b, shr( index, 1 ) );
726 3443 : move16();
727 3443 : *T0_frac = sub( index, shl( sub( *T0, PIT16k_FR2_EXTEND_10b ), 1 ) );
728 3443 : ( *T0_frac ) = shl( *T0_frac, 1 ); //( *T0_frac ) *= 2;
729 3443 : move16();
730 : }
731 : }
732 : }
733 174264 : ELSE IF( EQ_16( nBits, 9 ) ) /* absolute decoding with 9 bits */
734 : {
735 : {
736 0 : IF( LT_16( pitch_index, ( PIT16k_FR2_EXTEND_9b - PIT16k_MIN_EXTEND ) * 4 ) )
737 : {
738 0 : *T0 = add( PIT16k_MIN_EXTEND, shr( pitch_index, 2 ) );
739 0 : move16();
740 0 : *T0_frac = sub( pitch_index, shl( sub( *T0, PIT16k_MIN_EXTEND ), 2 ) );
741 0 : move16();
742 : }
743 0 : ELSE IF( LT_16( pitch_index, ( ( PIT16k_FR2_EXTEND_9b - PIT16k_MIN_EXTEND ) * 4 + ( PIT16k_FR1_EXTEND_9b - PIT16k_FR2_EXTEND_9b ) * 2 ) ) )
744 : {
745 0 : index = sub( pitch_index, ( PIT16k_FR2_EXTEND_9b - PIT16k_MIN_EXTEND ) * 4 );
746 0 : *T0 = add( PIT16k_FR2_EXTEND_9b, shr( index, 1 ) );
747 0 : move16();
748 0 : *T0_frac = sub( index, shl( sub( *T0, PIT16k_FR2_EXTEND_9b ), 1 ) );
749 0 : move16();
750 0 : ( *T0_frac ) = imult1616( ( *T0_frac ), shl( ( *T0_frac ), 1 ) );
751 0 : move16();
752 : }
753 : ELSE
754 : {
755 0 : *T0 = add( pitch_index, PIT16k_FR1_EXTEND_9b - ( ( PIT16k_FR2_EXTEND_9b - PIT16k_MIN_EXTEND ) * 4 ) - ( ( PIT16k_FR1_EXTEND_9b - PIT16k_FR2_EXTEND_9b ) * 2 ) );
756 0 : move16();
757 0 : *T0_frac = 0;
758 0 : move16();
759 : }
760 : }
761 : }
762 : ELSE /* nBits == 6 */ /* relative decoding with 6 bits */
763 : {
764 174264 : delta_pit_dec_fx( 4, pitch_index, T0, T0_frac, *T0_min );
765 : }
766 :
767 : /* biterror detection mechanism */
768 304002 : test();
769 304002 : IF( GT_16( add( shl( *T0, 2 ), *T0_frac ), ( PIT16k_MAX << 2 ) ) && GE_16( nBits, 9 ) )
770 : {
771 0 : *T0 = L_SUBFR;
772 0 : move16();
773 0 : *T0_frac = 0;
774 0 : move16();
775 0 : *BER_detect = 1;
776 0 : move16();
777 : }
778 :
779 : /* find T0_min and T0_max for delta search */
780 304002 : limit_T0_fx( L_FRAME16k, 8, L_SUBFR, limit_flag, *T0, *T0_frac, T0_min, T0_max );
781 :
782 304002 : return;
783 : }
784 :
785 : /*----------------------------------------------------------*
786 : * abs_pit_dec_fx()
787 : *
788 : * Absolute pitch decoding
789 : *----------------------------------------------------------*/
790 :
791 37349 : void abs_pit_dec_fx(
792 : const Word16 fr_steps, /* i: fractional resolution steps (0, 2, 4) */
793 : Word16 pitch_index, /* i: pitch index */
794 : const Word16 limit_flag, /* i : restrained(0) or extended(1) limits */
795 : Word16 *T0, /* o: integer pitch lag */
796 : Word16 *T0_frac /* o: pitch fraction */
797 : )
798 : {
799 : Word16 temp;
800 :
801 37349 : IF( limit_flag == 0 )
802 : {
803 27406 : IF( EQ_16( fr_steps, 2 ) )
804 : {
805 3436 : IF( LT_16( pitch_index, PIT_FR1_8b_MINUS_PIT_MIN_X2 ) )
806 : {
807 1820 : *T0 = add( PIT_MIN, shr( pitch_index, 1 ) );
808 1820 : move16();
809 1820 : temp = shl( sub( *T0, PIT_MIN ), 1 );
810 1820 : *T0_frac = shl( sub( pitch_index, temp ), 1 );
811 1820 : move16();
812 : }
813 : ELSE
814 : {
815 1616 : *T0 = add( pitch_index, PIT_FR1_8b_MINUS_PIT_FR1_8b_MINUS_PIT_MIN_X2 );
816 1616 : move16();
817 1616 : *T0_frac = 0;
818 1616 : move16();
819 : }
820 : }
821 23970 : ELSE IF( EQ_16( fr_steps, 4 ) )
822 : {
823 23970 : IF( LT_16( pitch_index, PIT_FR2_9b_MINUS_PIT_MIN_X4 ) )
824 : {
825 19354 : *T0 = add( PIT_MIN, shr( pitch_index, 2 ) );
826 19354 : move16();
827 19354 : temp = shl( sub( *T0, PIT_MIN ), 2 );
828 19354 : *T0_frac = sub( pitch_index, temp );
829 19354 : move16();
830 : }
831 4616 : ELSE IF( LT_16( pitch_index, PIT_DECODE_1 ) ) /*( (PIT_FR2_9b-PIT_MIN)*4 + (PIT_FR1_9b-PIT_FR2_9b)*2) = 440*/
832 : {
833 2177 : pitch_index = sub( pitch_index, PIT_DECODE_2 ); /*pitch_index -= (PIT_FR2_9b-PIT_MIN)*4(=376);*/
834 2177 : *T0 = add( PIT_FR2_9b, shr( pitch_index, 1 ) );
835 2177 : move16();
836 2177 : temp = shl( sub( *T0, PIT_FR2_9b ), 1 );
837 2177 : *T0_frac = shl( sub( pitch_index, temp ), 1 );
838 2177 : move16();
839 : }
840 : ELSE
841 : {
842 2439 : *T0 = add( pitch_index, PIT_DECODE_3 );
843 2439 : move16();
844 2439 : *T0_frac = 0;
845 2439 : move16();
846 : }
847 : }
848 : ELSE /* fr_steps == 0 */
849 : {
850 : /* not used in the codec */
851 : }
852 : }
853 9943 : ELSE IF( EQ_16( limit_flag, 1 ) ) /* extended Q range */
854 : {
855 9539 : IF( EQ_16( fr_steps, 2 ) )
856 : {
857 971 : IF( LT_16( pitch_index, PIT_FR1_EXT8b_MINUS_PIT_MIN_EXT_X2 ) )
858 : {
859 491 : *T0 = add( PIT_MIN_EXTEND, shr( pitch_index, 1 ) );
860 491 : move16();
861 491 : *T0_frac = sub( pitch_index, shl( sub( *T0, PIT_MIN_EXTEND ), 1 ) );
862 491 : move16();
863 491 : *T0_frac = shl( *T0_frac, 1 );
864 491 : move16();
865 : }
866 : ELSE
867 : {
868 480 : *T0 = add( pitch_index, sub( PIT_FR1_EXTEND_8b, PIT_FR1_EXT8b_MINUS_PIT_MIN_EXT_X2 ) );
869 480 : move16();
870 480 : *T0_frac = 0;
871 480 : move16();
872 : }
873 : }
874 8568 : ELSE IF( EQ_16( fr_steps, 4 ) )
875 : {
876 8568 : IF( LT_16( pitch_index, PIT_FR2_EXT9b_MINUS_PIT_MIN_EXT_X4 ) )
877 : {
878 : /**T0 = PIT_MIN_EXTEND + (pitch_index/4);*/
879 7480 : *T0 = add( PIT_MIN_EXTEND, shr( pitch_index, 2 ) );
880 7480 : move16();
881 : /**T0_frac = pitch_index - (*T0 - PIT_MIN_EXTEND)*4;*/
882 7480 : *T0_frac = sub( pitch_index, shl( sub( *T0, PIT_MIN_EXTEND ), 2 ) );
883 7480 : move16();
884 : }
885 1088 : ELSE IF( LT_16( pitch_index, add( PIT_FR2_EXT9b_MINUS_PIT_MIN_EXT_X4, PIT_FR1_EXT9b_MINUS_PIT_FR2_EXT9b_X2 ) ) )
886 : {
887 : /*pitch_index -= (PIT_FR2_EXTEND_9b-PIT_MIN_EXTEND)*4;*/
888 204 : pitch_index = sub( pitch_index, PIT_FR2_EXT9b_MINUS_PIT_MIN_EXT_X4 );
889 204 : *T0 = add( PIT_FR2_EXTEND_9b, shr( pitch_index, 1 ) );
890 204 : move16();
891 204 : *T0_frac = sub( pitch_index, shl( sub( *T0, PIT_FR2_EXTEND_9b ), 1 ) );
892 204 : move16();
893 204 : ( *T0_frac ) = shl( *T0_frac, 1 );
894 204 : move16();
895 : }
896 : ELSE
897 : {
898 : /**T0 = pitch_index + PIT_FR1_EXTEND_9b - ((PIT_FR2_EXTEND_9b-PIT_MIN_EXTEND)*4) - ((PIT_FR1_EXTEND_9b-PIT_FR2_EXTEND_9b)*2);move16();*/
899 884 : *T0 = add( pitch_index, PIT_DECODE_7 );
900 884 : move16();
901 884 : *T0_frac = 0;
902 884 : move16();
903 : }
904 : }
905 : ELSE /* fr_steps == 0 */
906 : {
907 : /* not used in the codec */
908 : }
909 : }
910 : ELSE /* limit_flag == 2 */
911 : {
912 404 : IF( EQ_16( fr_steps, 2 ) )
913 : {
914 0 : IF( LT_16( pitch_index, PIT_FR1_DEXT8b_MINUS_PIT_MIN_DEXT_X2 ) )
915 : {
916 0 : *T0 = add( PIT_MIN_DOUBLEEXTEND, shr( pitch_index, 1 ) );
917 0 : move16();
918 0 : *T0_frac = sub( pitch_index, shl( sub( *T0, PIT_MIN_DOUBLEEXTEND ), 1 ) );
919 0 : move16();
920 0 : *T0_frac = shl( *T0_frac, 1 );
921 0 : move16();
922 : }
923 : ELSE
924 : {
925 : /**T0 = pitch_index + PIT_FR1_DOUBLEEXTEND_8b - ((PIT_FR1_DOUBLEEXTEND_8b-PIT_MIN_DOUBLEEXTEND)*2);move16();*/
926 0 : *T0 = add( pitch_index, PIT_DECODE_8 );
927 0 : move16();
928 0 : *T0_frac = 0;
929 0 : move16();
930 : }
931 : }
932 404 : ELSE IF( EQ_16( fr_steps, 4 ) )
933 : {
934 404 : IF( LT_16( pitch_index, PIT_FR2_DEXT9b_MINUS_PIT_MIN_DEXT_X4 ) )
935 : {
936 376 : *T0 = add( PIT_MIN_DOUBLEEXTEND, shr( pitch_index, 2 ) );
937 376 : move16();
938 376 : *T0_frac = sub( pitch_index, shl( sub( *T0, PIT_MIN_DOUBLEEXTEND ), 2 ) );
939 376 : move16();
940 : }
941 28 : ELSE IF( LT_16( pitch_index, PIT_DECODE_9 ) )
942 : {
943 : /*pitch_index -= (PIT_FR2_DOUBLEEXTEND_9b-PIT_MIN_DOUBLEEXTEND)*4;move16();*/
944 10 : pitch_index = sub( pitch_index, PIT_FR2_DEXT9b_MINUS_PIT_MIN_DEXT_X4 );
945 10 : move16();
946 10 : *T0 = add( PIT_FR2_DOUBLEEXTEND_9b, shr( pitch_index, 1 ) );
947 10 : move16();
948 10 : *T0_frac = sub( pitch_index, shl( sub( *T0, PIT_FR2_DOUBLEEXTEND_9b ), 1 ) );
949 10 : move16();
950 10 : ( *T0_frac ) = shl( *T0_frac, 1 );
951 10 : move16();
952 : }
953 : ELSE
954 : {
955 18 : *T0 = add( pitch_index, PIT_DECODE_10 );
956 18 : move16();
957 18 : *T0_frac = 0;
958 18 : move16();
959 : }
960 : }
961 : ELSE /* fr_steps == 0 */
962 : {
963 : /* not used in the codec */
964 : }
965 : }
966 :
967 37349 : return;
968 : }
969 :
970 : /*----------------------------------------------------------*
971 : * delta_pit_dec_fx()
972 : *
973 : * Delta pitch decoding
974 : *----------------------------------------------------------*/
975 :
976 303731 : void delta_pit_dec_fx(
977 : const Word16 fr_steps, /* i : fractional resolution steps (0, 2, 4) */
978 : const Word16 pitch_index, /* i : pitch index */
979 : Word16 *T0, /* o : integer pitch lag */
980 : Word16 *T0_frac, /* o : pitch fraction */
981 : const Word16 T0_min /* i : delta search min */
982 : )
983 : {
984 :
985 : Word16 temp;
986 303731 : IF( fr_steps == 0 )
987 : {
988 146 : *T0 = add( T0_min, pitch_index );
989 146 : move16();
990 146 : *T0_frac = 0;
991 146 : move16();
992 : }
993 303585 : ELSE IF( EQ_16( fr_steps, 2 ) )
994 : {
995 8917 : *T0 = add( T0_min, shr( pitch_index, 1 ) );
996 8917 : move16();
997 8917 : temp = shl( sub( *T0, T0_min ), 1 );
998 8917 : *T0_frac = shl( sub( pitch_index, temp ), 1 );
999 8917 : move16();
1000 : }
1001 294668 : ELSE IF( EQ_16( fr_steps, 4 ) )
1002 : {
1003 294668 : *T0 = add( T0_min, shr( pitch_index, 2 ) );
1004 294668 : move16();
1005 294668 : temp = shl( sub( *T0, T0_min ), 2 );
1006 294668 : *T0_frac = sub( pitch_index, temp );
1007 294668 : move16();
1008 : }
1009 :
1010 303731 : return;
1011 : }
|