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 3060 : 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 3060 : 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 3060 : 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 3060 : 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 3060 : ELSE IF( EQ_16( coder_type, 3 ) ) /* 9/6/6/6 (HRs- VC) */
79 : {
80 1350 : Word16 pit_res_max2 = pit_res_max;
81 1350 : move16();
82 1350 : if ( EQ_16( pit_min, PIT_MIN_16k ) )
83 : {
84 1350 : pit_res_max2 = shr( pit_res_max, 1 );
85 : }
86 :
87 1350 : IF( ( i_subfr == 0 ) )
88 : {
89 270 : 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 1080 : 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 1080 : *T0_res = pit_res_max2;
95 1080 : move16();
96 1080 : 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 3060 : assert( *T0_res > 0 || *T0_res <= 6 );
157 :
158 : /*pitch = (float)(*T0) + (float)(*T0_frac)/(float)(*T0_res);*/ /* save subframe pitch values */
159 3060 : pitch = L_mac( L_deposit_h( *T0 ), *T0_frac, inv_T0_res[*T0_res] ); // Q16
160 :
161 :
162 3060 : return pitch;
163 : }
164 :
165 :
166 : /*---------------------------------------------------------------------*
167 : * Mode2_abs_pit_dec()
168 : *
169 : * Decode the absolute pitch
170 : *---------------------------------------------------------------------*/
171 :
172 954 : 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 954 : index = **pt_indice;
186 954 : move16();
187 954 : ( *pt_indice )++;
188 954 : move16();
189 954 : pit_res_max_half = shr( pit_res_max, 1 );
190 :
191 954 : tmp1 = i_mult( sub( pit_fr2, pit_min ), pit_res_max );
192 954 : tmp2 = i_mult( sub( pit_fr1, pit_fr2 ), pit_res_max_half );
193 954 : 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 954 : 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 112 : *T0 = add( index, sub( pit_fr1, add( tmp1, tmp2 ) ) );
235 112 : move16();
236 112 : *T0_frac = 0;
237 112 : move16();
238 112 : *T0_res = 1;
239 112 : move16();
240 : }
241 :
242 954 : return;
243 : }
244 :
245 :
246 : /*---------------------------------------------------------------------*
247 : * Routine Mode2_delta_pit_dec()
248 : *
249 : * Decode delta pitch
250 : *---------------------------------------------------------------------*/
251 2106 : 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 2106 : assert( T0_res > 1 && T0_res <= 6 );
263 :
264 2106 : res = T0_res;
265 2106 : move16();
266 2106 : if ( EQ_16( T0_res, 6 ) )
267 : {
268 0 : res = shr( res, 1 );
269 : }
270 :
271 2106 : index = **pt_indice;
272 2106 : move16();
273 2106 : ( *pt_indice )++;
274 2106 : move16();
275 :
276 :
277 2106 : *T0 = mult( add( index, *T0_min_frac ), inv_T0_res[res] );
278 2106 : move16();
279 2106 : IF( EQ_16( T0_res, 6 ) )
280 : {
281 0 : *T0 = shr( *T0, 1 );
282 0 : move16();
283 : }
284 :
285 2106 : *T0 = add( *T0_min, *T0 );
286 2106 : move16();
287 :
288 2106 : *T0_frac = add( index, sub( *T0_min_frac, i_mult( sub( *T0, *T0_min ), T0_res ) ) );
289 2106 : move16();
290 :
291 2106 : 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 510812 : 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 510812 : pitch_index = 0;
344 510812 : move16();
345 :
346 : /*----------------------------------------------------------------*
347 : * Set pit_flag = 0 for every subframe with absolute pitch search
348 : *----------------------------------------------------------------*/
349 510812 : pit_flag = i_subfr;
350 510812 : move16();
351 :
352 510812 : if ( EQ_16( i_subfr, PIT_DECODE_2XL_SUBFR ) )
353 : {
354 112519 : pit_flag = 0;
355 112519 : move16();
356 : }
357 :
358 : /*-------------------------------------------------------*
359 : * Retrieve the pitch index
360 : *-------------------------------------------------------*/
361 510812 : IF( !Opt_AMR_WB )
362 : {
363 : /*----------------------------------------------------------------*
364 : * pitch Q: Set limit_flag to 0 for restrained limits, and 1 for extended limits
365 : *----------------------------------------------------------------*/
366 510812 : test();
367 510812 : test();
368 510812 : IF( i_subfr == 0 )
369 : {
370 118684 : *limit_flag = 1;
371 118684 : move16();
372 :
373 118684 : if ( EQ_16( coder_type, VOICED ) )
374 : {
375 7459 : *limit_flag = 2;
376 7459 : move16(); /* double-extended limits */
377 : }
378 118684 : test();
379 118684 : if ( EQ_16( coder_type, GENERIC ) && EQ_32( core_brate, ACELP_7k20 ) )
380 : {
381 907 : *limit_flag = 0;
382 907 : move16();
383 : }
384 : }
385 392128 : ELSE IF( EQ_16( i_subfr, 2 * L_SUBFR ) && EQ_32( coder_type, GENERIC ) && LE_32( core_brate, ACELP_13k20 ) )
386 : {
387 31392 : if ( GT_16( *T0, shr( add( PIT_FR1_EXTEND_8b, PIT_MIN ), 1 ) ) )
388 : {
389 23666 : *limit_flag = 0;
390 23666 : move16();
391 : }
392 : }
393 :
394 : /*-------------------------------------------------------*
395 : * Retrieve the number of Q bits
396 : *-------------------------------------------------------*/
397 :
398 510812 : nBits = 0;
399 510812 : move16();
400 510812 : IF( NE_16( coder_type, AUDIO ) )
401 : {
402 : /* find the number of bits */
403 492417 : IF( i_subfr )
404 : {
405 383302 : tmp = idiv1616( i_subfr, L_subfr );
406 383302 : nBits = st_fx->acelp_cfg.pitch_bits[tmp];
407 : }
408 : ELSE
409 : {
410 109115 : nBits = st_fx->acelp_cfg.pitch_bits[0];
411 : }
412 492417 : move16();
413 492417 : 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 510812 : test();
421 510812 : test();
422 510812 : IF( EQ_16( coder_type, AUDIO ) )
423 : {
424 18395 : test();
425 18395 : if ( EQ_16( L_subfr, L_FRAME / 2 ) && i_subfr != 0 )
426 : {
427 2289 : pit_flag = L_SUBFR;
428 2289 : move16();
429 : }
430 18395 : if ( pit_flag == 0 )
431 : {
432 11748 : nBits = 10;
433 11748 : move16();
434 : }
435 18395 : if ( pit_flag != 0 )
436 : {
437 6647 : nBits = 6;
438 6647 : move16();
439 : }
440 :
441 18395 : pitch_index = (Word16) get_next_indice_fx( st_fx, nBits );
442 :
443 18395 : test();
444 18395 : test();
445 18395 : 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 18395 : 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 492417 : ELSE IF( EQ_16( coder_type, VOICED ) )
455 : {
456 : /*-------------------------------------------------------*
457 : * Pitch decoding in VOICED mode
458 : * (ACELP@12k8 core only)
459 : *-------------------------------------------------------*/
460 29836 : if ( EQ_16( i_subfr, 2 * L_SUBFR ) )
461 : {
462 7459 : pit_flag = i_subfr;
463 7459 : move16();
464 : }
465 :
466 29836 : 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 462581 : ELSE IF( EQ_16( st_fx->idchan, 1 ) && ( EQ_16( tdm_Pitch_reuse_flag, 1 ) || EQ_16( nBits, 4 ) ) )
469 140 : {
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 140 : delta = 4;
476 140 : pit_flag = L_SUBFR;
477 140 : move16();
478 140 : move16();
479 140 : isubfridx = shr( i_subfr, 6 );
480 140 : 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 140 : loc_T0 = tdm_Pri_pitch_buf[isubfridx]; /*Q6*/
494 140 : move16();
495 : /*loc_frac = (int16_t)((tdm_Pri_pitch_buf[i_subfr / L_SUBFR] - loc_T0) * 4.0f);*/
496 : }
497 140 : loc_frac = shr( sub( loc_T0, shl( shr( loc_T0, 6 ), 6 ) ), 4 ); /* Final result in Q 2*/
498 140 : loc_T0 = shr( loc_T0, 6 ); /*Q6 -> Q0*/
499 :
500 :
501 140 : limit_T0_fx( L_FRAME, delta, pit_flag, *limit_flag, loc_T0, loc_frac, T0_min, T0_max );
502 :
503 140 : 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 140 : *T0 = loc_T0;
510 140 : *T0_frac = loc_frac;
511 140 : move16();
512 140 : move16();
513 : }
514 : }
515 : ELSE
516 : {
517 : /*-------------------------------------------------------*
518 : * Pitch decoding in GENERIC mode
519 : * (both ACELP@12k8 and ACELP@16k cores)
520 : *-------------------------------------------------------*/
521 462441 : IF( EQ_16( L_frame, L_FRAME ) )
522 : {
523 164706 : 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 297735 : 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 510812 : pitch = shl( add( shl( *T0, 2 ), *T0_frac ), 4 ); /* save subframe pitch values Q6 */
574 :
575 510812 : return pitch;
576 : }
577 :
578 : /*----------------------------------------------------------*
579 : * pit_Q_dec_fx()
580 : *
581 : * Decode pitch lag
582 : *----------------------------------------------------------*/
583 :
584 222535 : 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 : Word16 *BER_detect /* o : BER detect flag */
596 : )
597 : {
598 222535 : IF( EQ_16( nBits, 10 ) ) /* absolute decoding with 10 bits */
599 : {
600 66866 : IF( limit_flag == 0 )
601 : {
602 0 : *T0 = add( PIT_MIN, shr( pitch_index, 2 ) );
603 0 : move16();
604 0 : *T0_frac = sub( pitch_index, shl( sub( *T0, PIT_MIN ), 2 ) );
605 0 : move16();
606 : }
607 66866 : ELSE IF( EQ_16( limit_flag, 1 ) )
608 : {
609 59815 : *T0 = add( PIT_MIN_EXTEND, shr( pitch_index, 2 ) );
610 59815 : move16();
611 59815 : *T0_frac = sub( pitch_index, shl( sub( *T0, PIT_MIN_EXTEND ), 2 ) );
612 59815 : move16();
613 : }
614 : ELSE /* limit_flag == 2 */
615 : {
616 7051 : *T0 = add( PIT_MIN_DOUBLEEXTEND, shr( pitch_index, 2 ) );
617 7051 : move16();
618 7051 : *T0_frac = sub( pitch_index, shl( sub( *T0, PIT_MIN_DOUBLEEXTEND ), 2 ) );
619 7051 : move16();
620 : }
621 : }
622 155669 : ELSE IF( EQ_16( nBits, 9 ) ) /* absolute decoding with 9 bits */
623 : {
624 31843 : abs_pit_dec_fx( 4, pitch_index, limit_flag, T0, T0_frac );
625 :
626 : /* find T0_min and T0_max for delta search */
627 31843 : IF( Opt_AMR_WB )
628 : {
629 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 */
630 : }
631 : }
632 123826 : ELSE IF( EQ_16( nBits, 8 ) ) /* absolute decoding with 8 bits */
633 : {
634 4387 : abs_pit_dec_fx( 2, pitch_index, limit_flag, T0, T0_frac );
635 :
636 : /* find T0_min and T0_max for delta search */
637 4387 : IF( Opt_AMR_WB )
638 : {
639 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 */
640 : }
641 : }
642 119439 : ELSE IF( EQ_16( nBits, 6 ) ) /* relative decoding with 6 bits */
643 : {
644 90267 : delta_pit_dec_fx( 4, pitch_index, T0, T0_frac, *T0_min );
645 : }
646 29172 : ELSE IF( EQ_16( nBits, 5 ) ) /* relative decoding with 5 bits */
647 : {
648 29172 : IF( EQ_16( delta, 8 ) )
649 : {
650 6795 : delta_pit_dec_fx( 2, pitch_index, T0, T0_frac, *T0_min );
651 : }
652 : ELSE /* delta == 4 */
653 : {
654 22377 : delta_pit_dec_fx( 4, pitch_index, T0, T0_frac, *T0_min );
655 : }
656 : }
657 : ELSE /* nBits == 4 */ /* relative decoding with 4 bits */
658 : {
659 0 : IF( EQ_16( delta, 8 ) )
660 : {
661 0 : delta_pit_dec_fx( 0, pitch_index, T0, T0_frac, *T0_min );
662 : }
663 : ELSE /* delta == 4 */
664 : {
665 0 : delta_pit_dec_fx( 2, pitch_index, T0, T0_frac, *T0_min );
666 : }
667 : }
668 :
669 : /* biterror detection mechanism */
670 222535 : test();
671 222535 : test();
672 222535 : IF( GT_16( add( shl( *T0, 2 ), *T0_frac ), ( PIT_MAX << 2 ) + 2 ) && pit_flag == 0 && !Opt_AMR_WB )
673 : {
674 0 : *T0 = L_SUBFR;
675 0 : move16();
676 0 : *T0_frac = 0;
677 0 : move16();
678 0 : *BER_detect = 1;
679 0 : move16();
680 : }
681 :
682 222535 : IF( !Opt_AMR_WB )
683 : {
684 : /* find T0_min and T0_max for delta search */
685 222535 : limit_T0_fx( L_FRAME, delta, L_SUBFR, limit_flag, *T0, *T0_frac, T0_min, T0_max );
686 : }
687 :
688 222535 : return;
689 : }
690 :
691 : /*-------------------------------------------------*
692 : * pit16k_Q_dec()
693 : *
694 : * pitch decoding @16kHz core
695 : *-------------------------------------------------*/
696 :
697 310528 : void pit16k_Q_dec_fx(
698 : const Word16 pitch_index, /* i : pitch index */
699 : const Word16 nBits, /* i : # of Q bits */
700 : const Word16 limit_flag, /* i : restrained(0) or extended(1) limits */
701 : Word16 *T0, /* o : integer pitch lag */
702 : Word16 *T0_frac, /* o : pitch fraction */
703 : Word16 *T0_min, /* i/o: delta search min */
704 : Word16 *T0_max, /* i/o: delta search max */
705 : Word16 *BER_detect /* o : BER detect flag */
706 : )
707 : {
708 : Word16 index;
709 :
710 310528 : IF( EQ_16( nBits, 10 ) ) /* absolute decoding with 10 bits */
711 : {
712 : {
713 132523 : IF( LT_16( pitch_index, shl( ( PIT16k_FR2_EXTEND_10b - PIT16k_MIN_EXTEND ), 2 ) ) )
714 : {
715 129024 : *T0 = add( PIT16k_MIN_EXTEND, shr( pitch_index, 2 ) );
716 129024 : move16();
717 129024 : *T0_frac = sub( pitch_index, shl( sub( *T0, PIT16k_MIN_EXTEND ), 2 ) );
718 129024 : move16();
719 : }
720 : ELSE
721 : {
722 3499 : index = sub( pitch_index, shl( ( PIT16k_FR2_EXTEND_10b - PIT16k_MIN_EXTEND ), 2 ) );
723 3499 : *T0 = add( PIT16k_FR2_EXTEND_10b, shr( index, 1 ) );
724 3499 : move16();
725 3499 : *T0_frac = sub( index, shl( sub( *T0, PIT16k_FR2_EXTEND_10b ), 1 ) );
726 3499 : ( *T0_frac ) = shl( *T0_frac, 1 ); //( *T0_frac ) *= 2;
727 3499 : move16();
728 : }
729 : }
730 : }
731 178005 : ELSE IF( EQ_16( nBits, 9 ) ) /* absolute decoding with 9 bits */
732 : {
733 : {
734 0 : IF( LT_16( pitch_index, ( PIT16k_FR2_EXTEND_9b - PIT16k_MIN_EXTEND ) * 4 ) )
735 : {
736 0 : *T0 = add( PIT16k_MIN_EXTEND, shr( pitch_index, 2 ) );
737 0 : move16();
738 0 : *T0_frac = sub( pitch_index, shl( sub( *T0, PIT16k_MIN_EXTEND ), 2 ) );
739 0 : move16();
740 : }
741 0 : ELSE IF( LT_16( pitch_index, ( ( PIT16k_FR2_EXTEND_9b - PIT16k_MIN_EXTEND ) * 4 + ( PIT16k_FR1_EXTEND_9b - PIT16k_FR2_EXTEND_9b ) * 2 ) ) )
742 : {
743 0 : index = sub( pitch_index, ( PIT16k_FR2_EXTEND_9b - PIT16k_MIN_EXTEND ) * 4 );
744 0 : *T0 = add( PIT16k_FR2_EXTEND_9b, shr( index, 1 ) );
745 0 : move16();
746 0 : *T0_frac = sub( index, shl( sub( *T0, PIT16k_FR2_EXTEND_9b ), 1 ) );
747 0 : move16();
748 0 : ( *T0_frac ) = imult1616( ( *T0_frac ), shl( ( *T0_frac ), 1 ) );
749 0 : move16();
750 : }
751 : ELSE
752 : {
753 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 ) );
754 0 : move16();
755 0 : *T0_frac = 0;
756 0 : move16();
757 : }
758 : }
759 : }
760 : ELSE /* nBits == 6 */ /* relative decoding with 6 bits */
761 : {
762 178005 : delta_pit_dec_fx( 4, pitch_index, T0, T0_frac, *T0_min );
763 : }
764 :
765 : /* biterror detection mechanism */
766 310528 : test();
767 310528 : IF( GT_16( add( shl( *T0, 2 ), *T0_frac ), ( PIT16k_MAX << 2 ) ) && GE_16( nBits, 9 ) )
768 : {
769 0 : *T0 = L_SUBFR;
770 0 : move16();
771 0 : *T0_frac = 0;
772 0 : move16();
773 0 : *BER_detect = 1;
774 0 : move16();
775 : }
776 :
777 : /* find T0_min and T0_max for delta search */
778 310528 : limit_T0_fx( L_FRAME16k, 8, L_SUBFR, limit_flag, *T0, *T0_frac, T0_min, T0_max );
779 :
780 310528 : return;
781 : }
782 :
783 : /*----------------------------------------------------------*
784 : * abs_pit_dec_fx()
785 : *
786 : * Absolute pitch decoding
787 : *----------------------------------------------------------*/
788 :
789 37787 : void abs_pit_dec_fx(
790 : const Word16 fr_steps, /* i: fractional resolution steps (0, 2, 4) */
791 : Word16 pitch_index, /* i: pitch index */
792 : const Word16 limit_flag, /* i : restrained(0) or extended(1) limits */
793 : Word16 *T0, /* o: integer pitch lag */
794 : Word16 *T0_frac /* o: pitch fraction */
795 : )
796 : {
797 : Word16 temp;
798 :
799 37787 : IF( limit_flag == 0 )
800 : {
801 27715 : IF( EQ_16( fr_steps, 2 ) )
802 : {
803 3417 : IF( LT_16( pitch_index, PIT_FR1_8b_MINUS_PIT_MIN_X2 ) )
804 : {
805 1809 : *T0 = add( PIT_MIN, shr( pitch_index, 1 ) );
806 1809 : move16();
807 1809 : temp = shl( sub( *T0, PIT_MIN ), 1 );
808 1809 : *T0_frac = shl( sub( pitch_index, temp ), 1 );
809 1809 : move16();
810 : }
811 : ELSE
812 : {
813 1608 : *T0 = add( pitch_index, PIT_FR1_8b_MINUS_PIT_FR1_8b_MINUS_PIT_MIN_X2 );
814 1608 : move16();
815 1608 : *T0_frac = 0;
816 1608 : move16();
817 : }
818 : }
819 24298 : ELSE IF( EQ_16( fr_steps, 4 ) )
820 : {
821 24298 : IF( LT_16( pitch_index, PIT_FR2_9b_MINUS_PIT_MIN_X4 ) )
822 : {
823 19555 : *T0 = add( PIT_MIN, shr( pitch_index, 2 ) );
824 19555 : move16();
825 19555 : temp = shl( sub( *T0, PIT_MIN ), 2 );
826 19555 : *T0_frac = sub( pitch_index, temp );
827 19555 : move16();
828 : }
829 4743 : ELSE IF( LT_16( pitch_index, PIT_DECODE_1 ) ) /*( (PIT_FR2_9b-PIT_MIN)*4 + (PIT_FR1_9b-PIT_FR2_9b)*2) = 440*/
830 : {
831 2225 : pitch_index = sub( pitch_index, PIT_DECODE_2 ); /*pitch_index -= (PIT_FR2_9b-PIT_MIN)*4(=376);*/
832 2225 : *T0 = add( PIT_FR2_9b, shr( pitch_index, 1 ) );
833 2225 : move16();
834 2225 : temp = shl( sub( *T0, PIT_FR2_9b ), 1 );
835 2225 : *T0_frac = shl( sub( pitch_index, temp ), 1 );
836 2225 : move16();
837 : }
838 : ELSE
839 : {
840 2518 : *T0 = add( pitch_index, PIT_DECODE_3 );
841 2518 : move16();
842 2518 : *T0_frac = 0;
843 2518 : move16();
844 : }
845 : }
846 : ELSE /* fr_steps == 0 */
847 : {
848 : /* not used in the codec */
849 : }
850 : }
851 10072 : ELSE IF( EQ_16( limit_flag, 1 ) ) /* extended Q range */
852 : {
853 9664 : IF( EQ_16( fr_steps, 2 ) )
854 : {
855 986 : IF( LT_16( pitch_index, PIT_FR1_EXT8b_MINUS_PIT_MIN_EXT_X2 ) )
856 : {
857 505 : *T0 = add( PIT_MIN_EXTEND, shr( pitch_index, 1 ) );
858 505 : move16();
859 505 : *T0_frac = sub( pitch_index, shl( sub( *T0, PIT_MIN_EXTEND ), 1 ) );
860 505 : move16();
861 505 : *T0_frac = shl( *T0_frac, 1 );
862 505 : move16();
863 : }
864 : ELSE
865 : {
866 481 : *T0 = add( pitch_index, sub( PIT_FR1_EXTEND_8b, PIT_FR1_EXT8b_MINUS_PIT_MIN_EXT_X2 ) );
867 481 : move16();
868 481 : *T0_frac = 0;
869 481 : move16();
870 : }
871 : }
872 8678 : ELSE IF( EQ_16( fr_steps, 4 ) )
873 : {
874 8678 : IF( LT_16( pitch_index, PIT_FR2_EXT9b_MINUS_PIT_MIN_EXT_X4 ) )
875 : {
876 : /**T0 = PIT_MIN_EXTEND + (pitch_index/4);*/
877 7583 : *T0 = add( PIT_MIN_EXTEND, shr( pitch_index, 2 ) );
878 7583 : move16();
879 : /**T0_frac = pitch_index - (*T0 - PIT_MIN_EXTEND)*4;*/
880 7583 : *T0_frac = sub( pitch_index, shl( sub( *T0, PIT_MIN_EXTEND ), 2 ) );
881 7583 : move16();
882 : }
883 1095 : ELSE IF( LT_16( pitch_index, add( PIT_FR2_EXT9b_MINUS_PIT_MIN_EXT_X4, PIT_FR1_EXT9b_MINUS_PIT_FR2_EXT9b_X2 ) ) )
884 : {
885 : /*pitch_index -= (PIT_FR2_EXTEND_9b-PIT_MIN_EXTEND)*4;*/
886 227 : pitch_index = sub( pitch_index, PIT_FR2_EXT9b_MINUS_PIT_MIN_EXT_X4 );
887 227 : *T0 = add( PIT_FR2_EXTEND_9b, shr( pitch_index, 1 ) );
888 227 : move16();
889 227 : *T0_frac = sub( pitch_index, shl( sub( *T0, PIT_FR2_EXTEND_9b ), 1 ) );
890 227 : move16();
891 227 : ( *T0_frac ) = shl( *T0_frac, 1 );
892 227 : move16();
893 : }
894 : ELSE
895 : {
896 : /**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();*/
897 868 : *T0 = add( pitch_index, PIT_DECODE_7 );
898 868 : move16();
899 868 : *T0_frac = 0;
900 868 : move16();
901 : }
902 : }
903 : ELSE /* fr_steps == 0 */
904 : {
905 : /* not used in the codec */
906 : }
907 : }
908 : ELSE /* limit_flag == 2 */
909 : {
910 408 : IF( EQ_16( fr_steps, 2 ) )
911 : {
912 0 : IF( LT_16( pitch_index, PIT_FR1_DEXT8b_MINUS_PIT_MIN_DEXT_X2 ) )
913 : {
914 0 : *T0 = add( PIT_MIN_DOUBLEEXTEND, shr( pitch_index, 1 ) );
915 0 : move16();
916 0 : *T0_frac = sub( pitch_index, shl( sub( *T0, PIT_MIN_DOUBLEEXTEND ), 1 ) );
917 0 : move16();
918 0 : *T0_frac = shl( *T0_frac, 1 );
919 0 : move16();
920 : }
921 : ELSE
922 : {
923 : /**T0 = pitch_index + PIT_FR1_DOUBLEEXTEND_8b - ((PIT_FR1_DOUBLEEXTEND_8b-PIT_MIN_DOUBLEEXTEND)*2);move16();*/
924 0 : *T0 = add( pitch_index, PIT_DECODE_8 );
925 0 : move16();
926 0 : *T0_frac = 0;
927 0 : move16();
928 : }
929 : }
930 408 : ELSE IF( EQ_16( fr_steps, 4 ) )
931 : {
932 408 : IF( LT_16( pitch_index, PIT_FR2_DEXT9b_MINUS_PIT_MIN_DEXT_X4 ) )
933 : {
934 382 : *T0 = add( PIT_MIN_DOUBLEEXTEND, shr( pitch_index, 2 ) );
935 382 : move16();
936 382 : *T0_frac = sub( pitch_index, shl( sub( *T0, PIT_MIN_DOUBLEEXTEND ), 2 ) );
937 382 : move16();
938 : }
939 26 : ELSE IF( LT_16( pitch_index, PIT_DECODE_9 ) )
940 : {
941 : /*pitch_index -= (PIT_FR2_DOUBLEEXTEND_9b-PIT_MIN_DOUBLEEXTEND)*4;move16();*/
942 8 : pitch_index = sub( pitch_index, PIT_FR2_DEXT9b_MINUS_PIT_MIN_DEXT_X4 );
943 8 : move16();
944 8 : *T0 = add( PIT_FR2_DOUBLEEXTEND_9b, shr( pitch_index, 1 ) );
945 8 : move16();
946 8 : *T0_frac = sub( pitch_index, shl( sub( *T0, PIT_FR2_DOUBLEEXTEND_9b ), 1 ) );
947 8 : move16();
948 8 : ( *T0_frac ) = shl( *T0_frac, 1 );
949 8 : move16();
950 : }
951 : ELSE
952 : {
953 18 : *T0 = add( pitch_index, PIT_DECODE_10 );
954 18 : move16();
955 18 : *T0_frac = 0;
956 18 : move16();
957 : }
958 : }
959 : ELSE /* fr_steps == 0 */
960 : {
961 : /* not used in the codec */
962 : }
963 : }
964 :
965 37787 : return;
966 : }
967 :
968 : /*----------------------------------------------------------*
969 : * delta_pit_dec_fx()
970 : *
971 : * Delta pitch decoding
972 : *----------------------------------------------------------*/
973 :
974 308412 : void delta_pit_dec_fx(
975 : const Word16 fr_steps, /* i : fractional resolution steps (0, 2, 4) */
976 : const Word16 pitch_index, /* i : pitch index */
977 : Word16 *T0, /* o : integer pitch lag */
978 : Word16 *T0_frac, /* o : pitch fraction */
979 : const Word16 T0_min /* i : delta search min */
980 : )
981 : {
982 :
983 : Word16 temp;
984 308412 : IF( fr_steps == 0 )
985 : {
986 140 : *T0 = add( T0_min, pitch_index );
987 140 : move16();
988 140 : *T0_frac = 0;
989 140 : move16();
990 : }
991 308272 : ELSE IF( EQ_16( fr_steps, 2 ) )
992 : {
993 8981 : *T0 = add( T0_min, shr( pitch_index, 1 ) );
994 8981 : move16();
995 8981 : temp = shl( sub( *T0, T0_min ), 1 );
996 8981 : *T0_frac = shl( sub( pitch_index, temp ), 1 );
997 8981 : move16();
998 : }
999 299291 : ELSE IF( EQ_16( fr_steps, 4 ) )
1000 : {
1001 299291 : *T0 = add( T0_min, shr( pitch_index, 2 ) );
1002 299291 : move16();
1003 299291 : temp = shl( sub( *T0, T0_min ), 2 );
1004 299291 : *T0_frac = sub( pitch_index, temp );
1005 299291 : move16();
1006 : }
1007 :
1008 308412 : return;
1009 : }
|