Line data Source code
1 : /*====================================================================================
2 : EVS Codec 3GPP TS26.452 Aug 12, 2021. Version 16.3.0
3 : ====================================================================================*/
4 :
5 : #include <stdint.h>
6 : #include <assert.h>
7 : #include "options.h" /* Compilation switches */
8 : #include "cnst.h" /* Common constants */
9 : #include "rom_enc.h" /* Encoder static table prototypes */
10 : #include "rom_com_fx.h" /* Static table prototypes */
11 : #include "rom_com.h" /* Static table prototypes */
12 : #include "rom_basop_util.h"
13 : #include "prot_fx.h" /* Function prototypes */
14 : #include "prot_fx_enc.h" /* Function prototypes */
15 : #include "prot_fx_enc.h" /* Function prototypes */
16 :
17 : #define inv_T0_res InvIntTable
18 :
19 : static void limit_T0_voiced2( Word16 res, const Word16 *T_op, Word16 *T0_min, Word16 *T0_min_frac, Word16 *T0_max, Word16 *T0_max_frac, Word16 pit_min, Word16 pit_max, Word16 i_subfr );
20 :
21 : /*==============================================================================*/
22 : /* FUNCTION : pit_encode_fx() */
23 : /*------------------------------------------------------------------------------*/
24 : /* PURPOSE : Close-loop pitch lag search and pitch lag quantization */
25 : /* Adaptive excitation construction */
26 : /*------------------------------------------------------------------------------*/
27 : /* INPUT ARGUMENTS : */
28 : /* _ (Word16) core_brate: core bitrate Q0 */
29 : /* _ (Word16) Opt_AMR_WB: flag indicating AMR-WB IO mode Q0 */
30 : /* _ (Word16) bwidth : input signal bandwidth Q0 */
31 : /* _ (Word16[]) T_op : open loop pitch estimates in current frame Q0 */
32 : /* _ (Word16) T0_min : lower limit for close-loop search Q0 */
33 : /* _ (Word16) T0_max : higher limit for close-loop search Q0 */
34 : /* _ (Word16) T0 : close loop integer pitch Q0 */
35 : /* _ (Word16) T0_frac : close loop fractional part of the pitch Q0 */
36 : /* _ (Word16) L_frame_fx : length of the frame Q0 */
37 : /* _ (Word16[]) h1 : weighted filter input response Q15 */
38 : /* _ (Word16[]) xn : target vector Q_new */
39 : /* _ (Word16) coder_type : coding type Q0 */
40 : /* _ (Word16) i_subfr : current sub frame indicator Q0 */
41 : /* _ (Word16[]) exc_fx : pointer to excitation signal frame Q_new */
42 : /* _ (Word16[]) L_subfr : subframe length Q0 */
43 : /*------------------------------------------------------------------------------*/
44 : /* OUTPUT ARGUMENTS : */
45 : /* _ (Word16[]) exc_fx : adapt. excitation exc (Q0) */
46 : /* _ (Word16) T0_min : lower limit for close-loop search Q0 */
47 : /* _ (Word16) T0_max : higher limit for close-loop search Q0 */
48 : /* _ (Word16) T0 : close loop integer pitch Q0 */
49 : /* _ (Word16) T0_frac : close loop fractional part of the pitch Q0 */
50 : /*------------------------------------------------------------------------------*/
51 :
52 : /*------------------------------------------------------------------------------*/
53 : /* RETURN ARGUMENTS : */
54 : /* _ None */
55 : /*==============================================================================*/
56 :
57 : /* o : Fractional pitch for each subframe */
58 569793 : Word16 pit_encode_fx(
59 : BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle */
60 : const Word16 pitch_bits[], /* i : pitch bits */
61 : const Word32 core_brate, /* i : core bitrate */
62 : const Word16 Opt_AMR_WB, /* i : flag indicating AMR-WB IO mode */
63 : const Word16 L_frame, /* i : length of the frame */
64 : const Word16 coder_type, /* i : coding type */
65 : Word16 *limit_flag, /* i/o: restrained(0) or extended(1) Q limits */
66 : const Word16 i_subfr, /* i : subframe index */
67 : Word16 *exc, /* i/o: pointer to excitation signal frame */
68 : const Word16 L_subfr, /* i : subframe length */
69 : const Word16 *pitch, /* i : open loop pitch estimates in current frame */
70 : Word16 *T0_min, /* i/o: lower limit for close-loop search */
71 : Word16 *T0_max, /* i/o: higher limit for close-loop search */
72 : Word16 *T0, /* i/o: close loop integer pitch */
73 : Word16 *T0_frac, /* i/o: close loop fractional part of the pitch */
74 : const Word16 *h1, /* i : weighted filter input response */
75 : const Word16 *xn, /* i : target vector */
76 : const Word16 tdm_Pitch_reuse_flag, /* i : primary channel pitch reuse flag */
77 : const Word16 tdm_Pri_pitch_buf[], /* i : primary channel pitch buffer */
78 : const Word16 element_mode, /* i : mode element */
79 : const Word16 Q_new /* i : scaling factor */
80 : )
81 : {
82 : Word16 pitch_cl;
83 : Word16 pit_flag, delta, mult_Top, nBits;
84 : Word16 L_sufr_sft;
85 : Word16 T_op[2]; /* values for two half-frames */
86 569793 : L_sufr_sft = 6;
87 569793 : move16();
88 569793 : if ( EQ_16( L_subfr, 2 * L_SUBFR ) )
89 : {
90 6680 : L_sufr_sft = 7;
91 6680 : move16();
92 : }
93 :
94 : /*----------------------------------------------------------------*
95 : * convert pitch values to 16kHz domain
96 : *----------------------------------------------------------------*/
97 569793 : test();
98 569793 : test();
99 569793 : IF( EQ_16( L_frame, L_FRAME ) || ( tdm_Pri_pitch_buf != NULL && tdm_Pri_pitch_buf[0] < 0 ) )
100 : {
101 244584 : Copy( pitch, T_op, 2 );
102 : }
103 : ELSE /* L_frame == L_FRAME16k */
104 : {
105 : /*T_op[0] = (int16_t)(pitch[0] * 1.25f + 0.5f);
106 : T_op[1] = (int16_t)(pitch[1] * 1.25f + 0.5f);*/
107 325209 : T_op[0] = shr( add( round_fx( L_shl( L_mult( 20480, pitch[0] ), 2 ) ), 1 ), 1 );
108 325209 : move16();
109 325209 : T_op[1] = shr( add( round_fx( L_shl( L_mult( 20480, pitch[1] ), 2 ) ), 1 ), 1 );
110 325209 : move16();
111 : }
112 :
113 : /*----------------------------------------------------------------*
114 : * Set pit_flag to 0 for every subframe with absolute pitch search
115 : *----------------------------------------------------------------*/
116 :
117 569793 : pit_flag = i_subfr;
118 569793 : move16();
119 569793 : if ( EQ_16( i_subfr, 2 * L_SUBFR ) )
120 : {
121 125926 : pit_flag = 0;
122 125926 : move16();
123 : }
124 :
125 : /*-----------------------------------------------------------------*
126 : * Limit range of pitch search
127 : * Fractional pitch search
128 : * Pitch quantization
129 : *-----------------------------------------------------------------*/
130 :
131 569793 : mult_Top = 1;
132 569793 : move16();
133 :
134 569793 : IF( !Opt_AMR_WB )
135 : {
136 : /*----------------------------------------------------------------*
137 : * pitch Q: Set limit_flag to 0 for restrained limits, and 1 for extended limits
138 : *----------------------------------------------------------------*/
139 569793 : test();
140 569793 : test();
141 569793 : IF( i_subfr == 0 )
142 : {
143 134185 : *limit_flag = 1;
144 134185 : move16();
145 134185 : if ( EQ_16( coder_type, VOICED ) )
146 : {
147 8897 : *limit_flag = 2;
148 8897 : move16(); /* double-extended limits */
149 : }
150 134185 : test();
151 134185 : if ( EQ_16( coder_type, GENERIC ) && EQ_32( core_brate, ACELP_7k20 ) )
152 : {
153 926 : *limit_flag = 0;
154 926 : move16();
155 : }
156 : }
157 435608 : ELSE IF( EQ_16( i_subfr, 2 * L_SUBFR ) && EQ_16( coder_type, GENERIC ) && LE_32( core_brate, ACELP_13k20 ) )
158 : {
159 : /*if( *T0 > (PIT_FR1_EXTEND_8b + PIT_MIN)>>1 )*/
160 36268 : if ( GT_16( *T0, shr( add( PIT_FR1_EXTEND_8b, PIT_MIN ), 1 ) ) )
161 : {
162 27415 : *limit_flag = 0;
163 27415 : move16();
164 : }
165 : }
166 :
167 569793 : IF( *limit_flag == 0 )
168 : {
169 56953 : test();
170 56953 : test();
171 56953 : IF( i_subfr == 0 && LT_16( T_op[0], PIT_MIN ) )
172 : {
173 89 : mult_Top = 2;
174 89 : move16();
175 : }
176 56864 : ELSE IF( EQ_16( i_subfr, 2 * L_SUBFR ) && LT_16( T_op[1], PIT_MIN ) )
177 : {
178 853 : mult_Top = 2;
179 853 : move16();
180 : }
181 : }
182 :
183 : /*-------------------------------------------------------*
184 : * Retrieve the number of Q bits
185 : *-------------------------------------------------------*/
186 :
187 569793 : nBits = 0;
188 569793 : move16();
189 569793 : IF( NE_16( coder_type, AUDIO ) )
190 : {
191 546454 : nBits = pitch_bits[i_subfr >> L_sufr_sft];
192 546454 : move16();
193 : }
194 569793 : IF( EQ_16( coder_type, AUDIO ) )
195 : {
196 : /*-------------------------------------------------------*
197 : * Pitch encoding in AUDIO coder type
198 : * (both ACELP@12k8 and ACELP@16k cores)
199 : *-------------------------------------------------------*/
200 :
201 23339 : delta = 4;
202 23339 : move16();
203 23339 : test();
204 23339 : test();
205 23339 : if ( EQ_16( L_subfr, shr( L_frame, 1 ) ) && i_subfr != 0 && EQ_16( L_frame, L_FRAME ) )
206 : {
207 3226 : pit_flag = L_SUBFR;
208 3226 : move16();
209 : }
210 23339 : IF( pit_flag == 0 )
211 : {
212 14457 : nBits = 10;
213 14457 : move16();
214 : }
215 : ELSE
216 : {
217 8882 : nBits = 6;
218 8882 : move16();
219 : }
220 :
221 : /* pitch lag search limitation */
222 23339 : test();
223 23339 : IF( i_subfr == 0 )
224 : {
225 11629 : limit_T0_fx( L_frame, delta, pit_flag, *limit_flag, mult_Top * T_op[0], 0, T0_min, T0_max );
226 : }
227 11710 : ELSE IF( EQ_16( i_subfr, 2 * L_SUBFR ) && pit_flag == 0 )
228 : {
229 2828 : limit_T0_fx( L_frame, delta, pit_flag, *limit_flag, mult_Top * T_op[1], 0, T0_min, T0_max );
230 : }
231 :
232 : /* search and encode the closed loop pitch period */
233 23339 : *T0 = pitch_fr4_fx( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, pit_flag, *limit_flag, PIT_MAX, PIT_MAX, L_FRAME, L_subfr, element_mode, Q_new );
234 23339 : move16();
235 :
236 23339 : pit_Q_enc_fx( hBstr, 0, nBits, delta, pit_flag, *limit_flag, *T0, *T0_frac, T0_min, T0_max );
237 : }
238 546454 : ELSE IF( EQ_16( coder_type, VOICED ) )
239 : {
240 : /*-------------------------------------------------------*
241 : * Pitch encoding in VOICED code type (ACELP@12k8 core only)
242 : *-------------------------------------------------------*/
243 :
244 35588 : delta = 4;
245 35588 : move16();
246 35588 : if ( EQ_16( i_subfr, 2 * L_SUBFR ) )
247 : {
248 8897 : pit_flag = i_subfr;
249 8897 : move16();
250 : }
251 :
252 : /* pitch lag search limitation */
253 35588 : IF( i_subfr == 0 )
254 : {
255 8897 : limit_T0_fx( L_FRAME, delta, pit_flag, *limit_flag, mult_Top * T_op[0], 0, T0_min, T0_max );
256 : }
257 :
258 : /* search and encode the closed loop pitch period */
259 :
260 35588 : test();
261 35588 : IF( EQ_16( nBits, 9 ) || EQ_16( nBits, 5 ) )
262 : {
263 27104 : *T0 = pitch_fr4_fx( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, pit_flag, *limit_flag, PIT_FR2_DOUBLEEXTEND_9b, PIT_FR1_DOUBLEEXTEND_9b, L_FRAME, L_SUBFR, element_mode, Q_new );
264 27104 : move16();
265 : }
266 8484 : ELSE IF( EQ_16( nBits, 10 ) )
267 : {
268 8484 : *T0 = pitch_fr4_fx( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, pit_flag, *limit_flag, PIT_MAX, PIT_MAX, L_FRAME, L_SUBFR, element_mode, Q_new );
269 8484 : move16();
270 : }
271 :
272 35588 : pit_Q_enc_fx( hBstr, 0, nBits, delta, pit_flag, *limit_flag, *T0, *T0_frac, T0_min, T0_max );
273 : }
274 510866 : ELSE IF( EQ_16( tdm_Pitch_reuse_flag, 1 ) || EQ_16( nBits, 4 ) )
275 140 : {
276 : /*-------------------------------------------------------*
277 : * Pitch encoding with reusing primary channel information
278 : *-------------------------------------------------------*/
279 : Word16 loc_T0, loc_frac;
280 :
281 140 : delta = 4;
282 140 : move16();
283 :
284 140 : pit_flag = L_SUBFR;
285 140 : move16();
286 :
287 140 : Word16 idx1 = shr( i_subfr, L_sufr_sft );
288 140 : Word16 idx2 = shr( add( i_subfr, 64 ), L_sufr_sft );
289 :
290 140 : IF( EQ_16( L_subfr, 2 * L_SUBFR ) )
291 : {
292 0 : loc_T0 = add( shr( tdm_Pri_pitch_buf[idx1], 1 ), shr( tdm_Pri_pitch_buf[idx2], 1 ) ); // Q6
293 0 : loc_frac = shr( sub( loc_T0, shl( shr( loc_T0, 6 ), 6 ) ), 4 ); // Q2
294 0 : loc_T0 = shr( loc_T0, 6 ); // Q0
295 : }
296 : ELSE
297 : {
298 140 : loc_T0 = tdm_Pri_pitch_buf[idx1]; // Q6
299 140 : loc_frac = shr( sub( loc_T0, shl( shr( loc_T0, 6 ), 6 ) ), 4 ); // Q2
300 140 : loc_T0 = shr( loc_T0, 6 ); // Q0
301 : }
302 :
303 : /* pitch lag search limitation */
304 140 : limit_T0_fx( L_FRAME, delta, pit_flag, *limit_flag, loc_T0, loc_frac, T0_min, T0_max );
305 :
306 140 : IF( nBits > 0 )
307 : {
308 : /* search and encode the closed loop pitch period */
309 0 : *T0 = pitch_fr4_fx( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, pit_flag, *limit_flag, PIT_MIN, PIT_FR1_8b, L_FRAME, L_SUBFR, element_mode, Q_new );
310 0 : move16();
311 :
312 0 : IF( EQ_16( delta, 8 ) )
313 : {
314 0 : *T0_frac = 0;
315 0 : move16();
316 : }
317 :
318 0 : pit_Q_enc_fx( hBstr, 0, nBits, delta, pit_flag, *limit_flag, *T0, *T0_frac, T0_min, T0_max );
319 : }
320 : ELSE
321 : {
322 140 : *T0 = loc_T0;
323 140 : move16();
324 140 : *T0_frac = loc_frac;
325 140 : move16();
326 : }
327 : }
328 : ELSE
329 : {
330 : /*-------------------------------------------------------*
331 : * Pitch encoding in GENERIC coder type
332 : * (both ACELP@12k8 and ACELP@16k cores)
333 : *-------------------------------------------------------*/
334 :
335 510726 : delta = 8;
336 510726 : move16();
337 :
338 : /* pitch lag search limitation */
339 510726 : IF( i_subfr == 0 )
340 : {
341 113624 : limit_T0_fx( L_frame, delta, pit_flag, *limit_flag, mult_Top * T_op[0], 0, T0_min, T0_max );
342 : }
343 397102 : ELSE IF( EQ_16( i_subfr, 2 * L_SUBFR ) )
344 : {
345 110940 : limit_T0_fx( L_frame, delta, pit_flag, *limit_flag, mult_Top * T_op[1], 0, T0_min, T0_max );
346 : }
347 :
348 : /* search and encode the closed loop pitch period */
349 510726 : IF( EQ_16( L_frame, L_FRAME ) )
350 : {
351 185496 : test();
352 185496 : test();
353 185496 : IF( EQ_16( nBits, 8 ) || EQ_16( nBits, 5 ) )
354 : {
355 10499 : IF( *limit_flag == 0 )
356 : {
357 6963 : *T0 = pitch_fr4_fx( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, pit_flag, *limit_flag, PIT_MIN, PIT_FR1_8b, L_FRAME, L_SUBFR, element_mode, Q_new );
358 6963 : move16();
359 : }
360 : ELSE
361 : {
362 3536 : *T0 = pitch_fr4_fx( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, pit_flag, *limit_flag, PIT_MIN_EXTEND, PIT_FR1_EXTEND_8b, L_FRAME, L_SUBFR, element_mode, Q_new );
363 3536 : move16();
364 : }
365 : }
366 174997 : ELSE IF( EQ_16( nBits, 9 ) || EQ_16( nBits, 6 ) )
367 : {
368 121481 : IF( *limit_flag == 0 )
369 : {
370 49920 : *T0 = pitch_fr4_fx( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, pit_flag, *limit_flag, PIT_FR2_9b, PIT_FR1_9b, L_FRAME, L_SUBFR, element_mode, Q_new );
371 49920 : move16();
372 : }
373 : ELSE
374 : {
375 71561 : *T0 = pitch_fr4_fx( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, pit_flag, *limit_flag, PIT_FR2_EXTEND_9b, PIT_FR1_EXTEND_9b, L_FRAME, L_SUBFR, element_mode, Q_new );
376 71561 : move16();
377 : }
378 : }
379 53516 : ELSE IF( EQ_16( nBits, 10 ) )
380 : {
381 53516 : *T0 = pitch_fr4_fx( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, pit_flag, *limit_flag, PIT_MAX, PIT_MAX, L_FRAME, L_SUBFR, element_mode, Q_new );
382 53516 : move16();
383 : }
384 :
385 185496 : pit_Q_enc_fx( hBstr, 0, nBits, delta, pit_flag, *limit_flag, *T0, *T0_frac, T0_min, T0_max );
386 : }
387 : ELSE /* L_frame == L_FRAME16k */
388 : {
389 325230 : test();
390 325230 : IF( EQ_16( nBits, 9 ) || EQ_16( nBits, 6 ) )
391 : {
392 193530 : *T0 = pitch_fr4_fx( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, pit_flag, *limit_flag, PIT16k_FR2_EXTEND_9b, PIT16k_FR1_EXTEND_9b, L_FRAME16k, L_SUBFR, element_mode, Q_new );
393 193530 : move16();
394 : }
395 131700 : ELSE IF( EQ_16( nBits, 10 ) )
396 : {
397 131700 : *T0 = pitch_fr4_fx( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, pit_flag, *limit_flag, PIT16k_FR2_EXTEND_10b, PIT16k_MAX, L_FRAME16k, L_SUBFR, element_mode, Q_new );
398 131700 : move16();
399 : }
400 :
401 325230 : pit16k_Q_enc_fx( hBstr, nBits, *limit_flag, *T0, *T0_frac, T0_min, T0_max );
402 : }
403 : }
404 : }
405 :
406 : /*-------------------------------------------------------*
407 : * Pitch encoding in AMR-WB IO mode
408 : *-------------------------------------------------------*/
409 :
410 : ELSE
411 : {
412 0 : delta = 8;
413 0 : move16();
414 0 : *limit_flag = 0;
415 0 : move16();
416 :
417 0 : IF( EQ_32( core_brate, ACELP_6k60 ) )
418 : {
419 0 : nBits = 5;
420 0 : move16();
421 :
422 : /* pitch lag search limitation */
423 0 : IF( i_subfr == 0 )
424 : {
425 0 : limit_T0_fx( L_FRAME, delta, pit_flag, *limit_flag, i_mult2( mult_Top, T_op[0] ), 0, T0_min, T0_max );
426 0 : nBits = 8;
427 0 : move16();
428 : }
429 :
430 0 : if ( EQ_16( i_subfr, 2 * L_SUBFR ) )
431 : {
432 : /* rewrite pit_flag - it must not be zero */
433 0 : pit_flag = i_subfr;
434 0 : move16();
435 : }
436 :
437 : /* search and encode the closed loop pitch period */
438 0 : *T0 = pitch_fr4_fx( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, pit_flag, *limit_flag, PIT_MIN, PIT_FR1_8b, L_FRAME, L_SUBFR, element_mode, Q_new );
439 0 : move16();
440 : }
441 0 : ELSE IF( EQ_32( core_brate, ACELP_8k85 ) )
442 : {
443 0 : nBits = 5;
444 0 : move16();
445 :
446 : /* pitch lag search limitation */
447 0 : IF( i_subfr == 0 )
448 : {
449 0 : limit_T0_fx( L_FRAME, delta, pit_flag, *limit_flag, i_mult2( mult_Top, T_op[0] ), 0, T0_min, T0_max );
450 0 : nBits = 8;
451 0 : move16();
452 : }
453 0 : ELSE IF( EQ_16( i_subfr, 2 * L_SUBFR ) )
454 : {
455 0 : limit_T0_fx( L_FRAME, delta, pit_flag, *limit_flag, i_mult2( mult_Top, T_op[1] ), 0, T0_min, T0_max );
456 0 : nBits = 8;
457 0 : move16();
458 : }
459 :
460 : /* search and encode the closed loop pitch period */
461 0 : *T0 = pitch_fr4_fx( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, pit_flag, *limit_flag, PIT_MIN, PIT_FR1_8b, L_FRAME, L_SUBFR, element_mode, Q_new );
462 0 : move16();
463 : }
464 : ELSE
465 : {
466 0 : nBits = 6;
467 0 : move16();
468 :
469 : /* pitch lag search limitation */
470 0 : IF( i_subfr == 0 )
471 : {
472 0 : limit_T0_fx( L_FRAME, delta, pit_flag, *limit_flag, i_mult2( mult_Top, T_op[0] ), 0, T0_min, T0_max );
473 0 : nBits = 9;
474 0 : move16();
475 : }
476 0 : ELSE IF( EQ_16( i_subfr, 2 * L_SUBFR ) )
477 : {
478 0 : limit_T0_fx( L_FRAME, delta, pit_flag, *limit_flag, i_mult2( mult_Top, T_op[1] ), 0, T0_min, T0_max );
479 0 : nBits = 9;
480 0 : move16();
481 : }
482 : ELSE
483 : {
484 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 */
485 : }
486 :
487 : /* search and encode the closed loop pitch period */
488 0 : *T0 = pitch_fr4_fx( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, pit_flag, *limit_flag, PIT_FR2_9b, PIT_FR1_9b, L_FRAME, L_SUBFR, element_mode, Q_new );
489 0 : move16();
490 : }
491 :
492 0 : pit_Q_enc_fx( hBstr, 1, nBits, delta, pit_flag, *limit_flag, *T0, *T0_frac, T0_min, T0_max );
493 : }
494 :
495 : /*-------------------------------------------------------*
496 : * Compute floating pitch output
497 : *-------------------------------------------------------*/
498 :
499 : /*pitch = (float)(*T0) + (float)(*T0_frac)/4.0f;*/ /* save subframe pitch values */
500 569793 : pitch_cl = shl( add( shl( *T0, 2 ), *T0_frac ), 4 ); /* save subframe pitch values Q6 */
501 :
502 569793 : return pitch_cl;
503 : }
504 :
505 :
506 : /*-------------------------------------------------------------------*
507 : * abs_pit_enc()
508 : *
509 : * Encode pitch lag absolutely with resolution for shortest pitches
510 : * depending on parameter 'fr_step':
511 : * fr_step = 2: pitch range encoded with 8 bits
512 : * fr_step = 4: pitch range encoded with 8 bits
513 : *-------------------------------------------------------------------*/
514 :
515 : /* o : pitch index */
516 43314 : Word16 abs_pit_enc_fx(
517 : const Word16 fr_steps, /* i : fractional resolution steps (2 or 4) for shortest pitches*/
518 : const Word16 limit_flag, /* i : restrained(0) or extended(1) limits */
519 : const Word16 T0, /* i : integer pitch lag */
520 : const Word16 T0_frac /* i : pitch fraction */
521 : )
522 : {
523 43314 : Word16 pitch_index = 0;
524 :
525 43314 : IF( limit_flag == 0 )
526 : {
527 32038 : IF( EQ_16( fr_steps, 2 ) )
528 : {
529 : /*-----------------------------------------------------------------*
530 : * The pitch range is encoded absolutely with 8 bits
531 : * and is divided as follows:
532 : * PIT_MIN to PIT_FR1_8b-1 resolution 1/2 (frac = 0 or 2)
533 : * PIT_FR1_8b to PIT_MAX resolution 1 (frac = 0)
534 : *-----------------------------------------------------------------*/
535 :
536 3500 : IF( LT_16( T0, PIT_FR1_8b ) )
537 : {
538 : /*pitch_index = T0*2 + (T0_frac>>1) - (PIT_MIN*2);*/
539 1852 : pitch_index = sub( add( shl( T0, 1 ), shr( T0_frac, 1 ) ), ( PIT_MIN * 2 ) );
540 : }
541 : ELSE
542 : {
543 : /*pitch_index = T0 - PIT_FR1_8b + ((PIT_FR1_8b-PIT_MIN)*2);*/
544 1648 : pitch_index = add( sub( T0, PIT_FR1_8b ), ( ( PIT_FR1_8b - PIT_MIN ) * 2 ) );
545 : }
546 : }
547 28538 : ELSE IF( EQ_16( fr_steps, 4 ) )
548 : {
549 : /*-------------------------------------------------------------------*
550 : * The pitch range is encoded absolutely with 9 bits
551 : * and is divided as follows:
552 : * PIT_MIN to PIT_FR2_9b-1 resolution 1/4 (frac = 0,1,2 or 3)
553 : * PIT_FR2_9b to PIT_FR1_9b-1 resolution 1/2 (frac = 0 or 2)
554 : * PIT_FR1_9b to PIT_MAX resolution 1 (frac = 0)
555 : *-------------------------------------------------------------------*/
556 :
557 28538 : IF( LT_16( T0, PIT_FR2_9b ) )
558 : {
559 : /*pitch_index = T0*4 + T0_frac - (PIT_MIN*4);*/
560 23136 : pitch_index = add( shl( T0, 2 ), sub( T0_frac, ( PIT_MIN * 4 ) ) );
561 : }
562 5402 : ELSE IF( LT_16( T0, PIT_FR1_9b ) )
563 : {
564 : /* pitch_index = T0*2 + (T0_frac>>1) - (PIT_FR2_9b*2) + ((PIT_FR2_9b-PIT_MIN)*4);*/
565 2567 : pitch_index = add( sub( add( shl( T0, 1 ), shr( T0_frac, 1 ) ), ( PIT_FR2_9b * 2 ) ), ( ( PIT_FR2_9b - PIT_MIN ) * 4 ) );
566 : }
567 : ELSE
568 : {
569 : /* pitch_index = T0 - PIT_FR1_9b + ((PIT_FR2_9b-PIT_MIN)*4) + ((PIT_FR1_9b-PIT_FR2_9b)*2);*/
570 2835 : pitch_index = add( add( sub( T0, PIT_FR1_9b ), ( ( PIT_FR2_9b - PIT_MIN ) * 4 ) ), ( ( PIT_FR1_9b - PIT_FR2_9b ) * 2 ) );
571 : }
572 : }
573 : ELSE /* fr_step == 0 */
574 : {
575 : /* not used in the codec */
576 0 : pitch_index = 0;
577 0 : move16();
578 : }
579 : }
580 11276 : ELSE IF( EQ_16( limit_flag, 1 ) ) /* extended Q range */
581 : {
582 10863 : IF( EQ_16( fr_steps, 2 ) )
583 : {
584 : /*-----------------------------------------------------------------*
585 : * The pitch range is encoded absolutely with 8 bits
586 : * and is divided as follows:
587 : * PIT_MIN_EXTEND to PIT_FR1_EXTEND_8b-1 resolution 1/2 (frac = 0 or 2)
588 : * PIT_FR1_EXTEND_8b to PIT_MAX_EXTEND resolution 1 (frac = 0)
589 : *-----------------------------------------------------------------*/
590 :
591 1013 : IF( LT_16( T0, PIT_FR1_EXTEND_8b ) )
592 : {
593 : /*pitch_index = T0*2 + (T0_frac>>1) - (PIT_MIN_EXTEND*2);*/
594 517 : pitch_index = sub( add( shl( T0, 1 ), shr( T0_frac, 1 ) ), ( PIT_MIN_EXTEND * 2 ) );
595 : }
596 : ELSE
597 : {
598 : /*pitch_index = T0 - PIT_FR1_EXTEND_8b + ((PIT_FR1_EXTEND_8b-PIT_MIN_EXTEND)*2);*/
599 496 : pitch_index = add( sub( T0, PIT_FR1_EXTEND_8b ), ( ( PIT_FR1_EXTEND_8b - PIT_MIN_EXTEND ) * 2 ) );
600 : }
601 : }
602 9850 : ELSE IF( EQ_16( fr_steps, 4 ) )
603 : {
604 : /*-------------------------------------------------------------------*
605 : * The pitch range is encoded absolutely with 9 bits
606 : * and is divided as follows:
607 : * PIT_MIN_EXTEND to PIT_FR2__EXTEND9b-1 resolution 1/4 (frac = 0,1,2 or 3)
608 : * PIT_FR2_EXTEND_9b to PIT_FR1__EXTEND9b-1 resolution 1/2 (frac = 0 or 2)
609 : * PIT_FR1_EXTEND_9b to PIT_MAX_EXTEND resolution 1 (frac = 0)
610 : *-------------------------------------------------------------------*/
611 :
612 9850 : IF( LT_16( T0, PIT_FR2_EXTEND_9b ) )
613 : {
614 : /*pitch_index = T0*4 + T0_frac - (PIT_MIN_EXTEND*4);*/
615 8629 : pitch_index = add( shl( T0, 2 ), sub( T0_frac, ( PIT_MIN_EXTEND * 4 ) ) );
616 : }
617 1221 : ELSE IF( T0 < PIT_FR1_EXTEND_9b )
618 : {
619 : /*pitch_index = T0*2 + (T0_frac>>1) - (PIT_FR2_EXTEND_9b*2) + ((PIT_FR2_EXTEND_9b-PIT_MIN_EXTEND)*4);*/
620 246 : pitch_index = add( sub( add( shl( T0, 1 ), shr( T0_frac, 1 ) ), ( PIT_FR2_EXTEND_9b * 2 ) ), ( ( PIT_FR2_EXTEND_9b - PIT_MIN_EXTEND ) * 4 ) );
621 : }
622 : ELSE
623 : {
624 : /*pitch_index = T0 - PIT_FR1_EXTEND_9b + ((PIT_FR2_EXTEND_9b-PIT_MIN_EXTEND)*4) + ((PIT_FR1_EXTEND_9b-PIT_FR2_EXTEND_9b)*2);*/
625 975 : pitch_index = add( add( sub( T0, PIT_FR1_EXTEND_9b ), ( ( PIT_FR2_EXTEND_9b - PIT_MIN_EXTEND ) * 4 ) ), ( ( PIT_FR1_EXTEND_9b - PIT_FR2_EXTEND_9b ) * 2 ) );
626 : }
627 : }
628 : ELSE /* fr_step == 0 */
629 : {
630 : /* not used in the codec */
631 0 : pitch_index = 0;
632 0 : move16();
633 : }
634 : }
635 : ELSE /* double-extended Q range */
636 : {
637 413 : IF( EQ_16( fr_steps, 2 ) )
638 : {
639 : /*-----------------------------------------------------------------*
640 : * The pitch range is encoded absolutely with 8 bits
641 : * and is divided as follows:
642 : * PIT_MIN_DOUBLEEXTEND to PIT_FR1_DOUBLEEXTEND_8b-1 resolution 1/2 (frac = 0 or 2)
643 : * PIT_FR1_DOUBLEEXTEND_8b to PIT_MAX_EXTEND resolution 1 (frac = 0)
644 : *-----------------------------------------------------------------*/
645 :
646 0 : IF( LT_16( T0, PIT_FR1_DOUBLEEXTEND_8b ) )
647 : {
648 : /*pitch_index = T0*2 + (T0_frac>>1) - (PIT_MIN_DOUBLEEXTEND*2);*/
649 0 : pitch_index = sub( add( shl( T0, 1 ), shr( T0_frac, 1 ) ), ( PIT_MIN_DOUBLEEXTEND * 2 ) );
650 : }
651 : ELSE
652 : {
653 : /*pitch_index = T0 - PIT_FR1_DOUBLEEXTEND_8b + ((PIT_FR1_DOUBLEEXTEND_8b-PIT_MIN_DOUBLEEXTEND)*2); */
654 0 : pitch_index = add( sub( T0, PIT_FR1_DOUBLEEXTEND_8b ), ( ( PIT_FR1_DOUBLEEXTEND_8b - PIT_MIN_DOUBLEEXTEND ) * 2 ) );
655 : }
656 : }
657 413 : ELSE IF( EQ_16( fr_steps, 4 ) )
658 : {
659 : /*-------------------------------------------------------------------*
660 : * The pitch range is encoded absolutely with 9 bits
661 : * and is divided as follows:
662 : * PIT_MIN_DOUBLEEXTEND to PIT_FR2_DOUBLEEXTEND9b-1 resolution 1/4 (frac = 0,1,2 or 3)
663 : * PIT_FR2_DOUBLEEXTEND_9b to PIT_FR1_DOOBLEEXTEND9b-1 resolution 1/2 (frac = 0 or 2)
664 : * PIT_FR1_DOUBLEEXTEND_9b to PIT_MAX_EXTEND resolution 1 (frac = 0)
665 : *-------------------------------------------------------------------*/
666 :
667 413 : IF( LT_16( T0, PIT_FR2_DOUBLEEXTEND_9b ) )
668 : {
669 : /*pitch_index = T0*4 + T0_frac - (PIT_MIN_DOUBLEEXTEND*4);*/
670 387 : pitch_index = add( shl( T0, 2 ), sub( T0_frac, ( PIT_MIN_DOUBLEEXTEND * 4 ) ) );
671 : }
672 26 : ELSE IF( LT_16( T0, PIT_FR1_DOUBLEEXTEND_9b ) )
673 : {
674 : /*pitch_index = T0*2 + (T0_frac>>1) - (PIT_FR2_DOUBLEEXTEND_9b*2) + ((PIT_FR2_DOUBLEEXTEND_9b-PIT_MIN_DOUBLEEXTEND)*4);*/
675 8 : pitch_index = add( sub( add( shl( T0, 1 ), shr( T0_frac, 1 ) ), ( PIT_FR2_DOUBLEEXTEND_9b * 2 ) ), ( ( PIT_FR2_DOUBLEEXTEND_9b - PIT_MIN_DOUBLEEXTEND ) * 4 ) );
676 : }
677 : ELSE
678 : {
679 : /*pitch_index = T0 - PIT_FR1_DOUBLEEXTEND_9b + ((PIT_FR2_DOUBLEEXTEND_9b-PIT_MIN_DOUBLEEXTEND)*4) + ((PIT_FR1_DOUBLEEXTEND_9b-PIT_FR2_DOUBLEEXTEND_9b)*2);*/
680 18 : pitch_index = add( add( sub( T0, PIT_FR1_DOUBLEEXTEND_9b ), ( ( PIT_FR2_DOUBLEEXTEND_9b - PIT_MIN_DOUBLEEXTEND ) * 4 ) ), ( ( PIT_FR1_DOUBLEEXTEND_9b - PIT_FR2_DOUBLEEXTEND_9b ) * 2 ) );
681 : }
682 : }
683 : ELSE /* fr_step == 0 */
684 : {
685 : /* not used in the codec */
686 0 : pitch_index = 0;
687 0 : move16();
688 : }
689 : }
690 :
691 43314 : return pitch_index;
692 : }
693 :
694 :
695 : /*-------------------------------------------------------------------*
696 : * delta_pit_enc:
697 : *
698 : * Encode pitch lag differentially from T0_min to T0_max
699 : * with resolution depending on parameter 'fr_step':
700 : * fr_step = 0: resolusion 1 (frac = 0), or
701 : * fr_step = 2: resolusion 1/2 (frac = 0 or 2), or
702 : * fr_step = 4: resolution 1/4 (frac = 0, 1, 2, or 3)
703 : *-------------------------------------------------------------------*/
704 :
705 : /* o : pitch index */
706 149126 : Word16 delta_pit_enc_fx(
707 : const Word16 fr_steps, /* i : fractional resolution steps (2 or 4)*/
708 : const Word16 T0, /* i : integer pitch lag */
709 : const Word16 T0_frac, /* i : pitch fraction */
710 : const Word16 T0_min /* i : delta search min */
711 : )
712 : {
713 149126 : Word16 pitch_index = 0;
714 149126 : move16();
715 :
716 149126 : IF( fr_steps == 0 )
717 : {
718 195 : pitch_index = sub( T0, T0_min );
719 : }
720 148931 : ELSE IF( EQ_16( fr_steps, 2 ) )
721 : {
722 : /* pitch_index = (T0 - T0_min) * 2 + (T0_frac>>1);*/
723 9434 : pitch_index = add( shl( sub( T0, T0_min ), 1 ), shr( T0_frac, 1 ) );
724 : }
725 139497 : ELSE IF( EQ_16( fr_steps, 4 ) )
726 : {
727 : /*pitch_index = (T0 - T0_min) * 4 + T0_frac;*/
728 139497 : pitch_index = add( shl( sub( T0, T0_min ), 2 ), T0_frac );
729 : }
730 :
731 149126 : return pitch_index;
732 : }
733 :
734 : /*-------------------------------------------------------------------*
735 : * pitch_fr4()
736 : *
737 : * Find the closed loop pitch period with 1/4 subsample resolution.
738 : *-------------------------------------------------------------------*/
739 :
740 : /* o : chosen integer pitch lag */
741 611766 : Word16 pitch_fr4_fx(
742 : const Word16 exc[], /* i : excitation buffer Q_new*/
743 : const Word16 xn[], /* i : target signal Q_new-1+shift*/
744 : const Word16 h[], /* i : weighted synthesis filter impulse response Q(14+shift)*/
745 : const Word16 t0_min, /* i : minimum value in the searched range. Q0*/
746 : const Word16 t0_max, /* i : maximum value in the searched range. Q0*/
747 : Word16 *pit_frac, /* o : chosen fraction (0, 1, 2 or 3) */
748 : const Word16 i_subfr, /* i : flag to first subframe */
749 : const Word16 limit_flag, /* i : flag for limits (0=restrained, 1=extended) */
750 : const Word16 t0_fr2, /* i : minimum value for resolution 1/2 */
751 : const Word16 t0_fr1, /* i : minimum value for resolution 1 */
752 : const Word16 L_frame, /* i : length of the frame */
753 : const Word16 L_subfr, /* i : size of subframe */
754 : const Word16 element_mode, /* i : mode element */
755 : const Word16 Q_new /* i : scaling factor */
756 : )
757 : {
758 : Word16 i;
759 : Word16 t_min, t_max;
760 : Word16 max_val, t0, t1, fraction, step, temp;
761 : Word16 *corr;
762 : Word16 corr_v[15 + 2 * L_INTERPOL1 + 1]; /* Total length = t0_max-t0_min+1+2*L_inter */
763 : Word16 pit_min;
764 : Word16 cor_max;
765 :
766 : /* initialization */
767 611766 : IF( limit_flag == 0 )
768 : {
769 82878 : IF( EQ_16( L_frame, L_FRAME ) )
770 : {
771 76219 : pit_min = PIT_MIN;
772 76219 : move16();
773 : }
774 : ELSE /* L_frame == L_FRAME16k */
775 : {
776 6659 : pit_min = PIT16k_MIN;
777 6659 : move16();
778 : }
779 : }
780 : ELSE
781 : {
782 528888 : IF( EQ_16( L_frame, L_FRAME ) )
783 : {
784 187540 : pit_min = PIT_MIN_EXTEND;
785 187540 : move16();
786 187540 : IF( EQ_16( limit_flag, 2 ) )
787 : {
788 35588 : pit_min = PIT_MIN_DOUBLEEXTEND;
789 35588 : move16();
790 : }
791 : }
792 : ELSE /* L_frame == L_FRAME16k */
793 : {
794 341348 : pit_min = PIT16k_MIN_EXTEND;
795 341348 : move16();
796 : }
797 : }
798 :
799 : /*-----------------------------------------------------------------*
800 : * - Find interval to compute normalized correlation
801 : * - allocate memory to normalized correlation vector
802 : * - Compute normalized correlation between target and filtered
803 : * excitation
804 : *-----------------------------------------------------------------*/
805 :
806 611766 : t_min = sub( t0_min, L_INTERPOL1 );
807 611766 : t_max = add( t0_max, L_INTERPOL1 );
808 611766 : corr = &corr_v[-t_min];
809 : /* corr[t_min..t_max] */
810 611766 : if ( element_mode > EVS_MONO )
811 : {
812 563824 : norm_corr_ivas_fx( exc, xn, h, t_min, t_max, corr, L_subfr, Q_new );
813 : }
814 : else
815 : {
816 47942 : norm_corr_fx( exc, xn, h, t_min, t_max, corr, L_subfr );
817 : }
818 :
819 : /*-----------------------------------------------------------------*
820 : * Find integer pitch
821 : *-----------------------------------------------------------------*/
822 :
823 611766 : max_val = corr[t0_min];
824 611766 : move16();
825 611766 : t0 = t0_min;
826 611766 : move16();
827 :
828 9316840 : FOR( i = t0_min + 1; i <= t0_max; i++ )
829 : {
830 8705074 : if ( GE_16( corr[i], max_val ) )
831 : {
832 3417806 : t0 = i;
833 3417806 : move16();
834 : }
835 8705074 : max_val = s_max( corr[i], max_val );
836 : }
837 :
838 611766 : IF( EQ_16( t0_fr1, pit_min ) )
839 : {
840 : /* don't search fraction (for 7b/4b quant) */
841 577 : test();
842 577 : IF( ( i_subfr == 0 ) && ( GE_16( t0, t0_fr2 ) ) )
843 : {
844 0 : i = shl( shr( t0, 1 ), 1 ); /* 2 samples resolution */
845 0 : IF( GT_16( add( i, 2 ), PIT_MAX ) )
846 : {
847 0 : i = sub( i, 2 );
848 : }
849 0 : IF( GT_16( corr[i], corr[i + 2] ) )
850 : {
851 0 : t0 = i;
852 0 : move16();
853 : }
854 : ELSE
855 : {
856 0 : t0 = add( i, 2 );
857 : }
858 : }
859 577 : *pit_frac = 0;
860 577 : move16();
861 :
862 577 : return ( t0 );
863 : }
864 :
865 611189 : test();
866 611189 : IF( ( i_subfr == 0 ) && ( GE_16( t0, t0_fr1 ) ) )
867 : {
868 11674 : *pit_frac = 0;
869 11674 : move16();
870 :
871 11674 : return ( t0 );
872 : }
873 :
874 : /*------------------------------------------------------------------*
875 : * Search fractionnal pitch with 1/4 subsample resolution.
876 : * search the fractions around t0 and choose the one which maximizes
877 : * the interpolated normalized correlation.
878 : *-----------------------------------------------------------------*/
879 :
880 599515 : t1 = t0;
881 599515 : move16();
882 599515 : step = 1;
883 599515 : move16(); /* 1/4 subsample resolution */
884 599515 : fraction = 1;
885 599515 : move16();
886 599515 : test();
887 599515 : test();
888 599515 : IF( ( ( i_subfr == 0 ) && ( GE_16( t0, t0_fr2 ) ) ) || ( EQ_16( t0_fr2, pit_min ) ) )
889 : {
890 21720 : step = 2;
891 21720 : move16(); /* 1/2 subsample resolution */
892 21720 : fraction = 2;
893 21720 : move16();
894 : }
895 :
896 599515 : IF( EQ_16( t0, t0_min ) ) /* Limit case */
897 : {
898 37142 : fraction = 0;
899 37142 : move16();
900 37142 : cor_max = Interpol_4( &corr[t0], fraction );
901 : }
902 : ELSE
903 : {
904 562373 : t0 = sub( t0, 1 );
905 562373 : cor_max = Interpol_4( &corr[t0], fraction );
906 1647479 : FOR( i = fraction + step; i <= 3; i = ( i + step ) )
907 : {
908 1085106 : temp = Interpol_4( &corr[t0], i );
909 1085106 : IF( GT_16( temp, cor_max ) )
910 : {
911 1011301 : cor_max = temp;
912 1011301 : move16();
913 1011301 : fraction = i;
914 1011301 : move16();
915 : }
916 : }
917 : }
918 :
919 2954135 : FOR( i = 0; i <= 3; i = ( i + step ) )
920 : {
921 2354620 : temp = Interpol_4( &corr[t1], i );
922 2354620 : IF( GT_16( temp, cor_max ) )
923 : {
924 699046 : cor_max = temp;
925 699046 : move16();
926 699046 : fraction = i;
927 699046 : move16();
928 699046 : t0 = t1;
929 699046 : move16();
930 : }
931 : }
932 :
933 599515 : *pit_frac = fraction;
934 599515 : move16();
935 :
936 599515 : return ( t0 );
937 : }
938 :
939 :
940 : /*-------------------------------------------------------------------*
941 : * norm_corr()
942 : *
943 : * Find the normalized correlation between the target vector and the
944 : * filtered past excitation (correlation between target and filtered
945 : * excitation divided by the square root of energy of filtered
946 : * excitation)
947 : *---------------------------------------------------------------------*/
948 :
949 563824 : void norm_corr_ivas_fx(
950 : const Word16 exc[], /* i : excitation buffer Q_new */
951 : const Word16 xn[], /* i : target signal Q_new-1 */
952 : const Word16 h[], /* i : weighted synthesis filter impulse response Q(14 - norm_s(h[0])) */
953 : const Word16 t_min, /* i : minimum value of searched range */
954 : const Word16 t_max, /* i : maximum value of searched range */
955 : Word16 ncorr[], /* o : normalized correlation */
956 : const Word16 L_subfr, /* i : subframe size */
957 : Word16 Q_new /* i */
958 : )
959 : {
960 : Word16 i, k, t;
961 : Word16 corr, exp_corr, norm, exp_norm, exp;
962 : Word16 excf[L_FRAME16k];
963 : Word16 ncorr_e[15 + 2 * L_INTERPOL1 + 1];
964 : Word16 h_e, e_max;
965 : Word32 L_tmp;
966 : Word64 W_tmp;
967 : #ifndef ISSUE_1867_replace_overflow_libenc
968 : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
969 : Flag Overflow = 0;
970 : move16();
971 : #endif
972 : #endif
973 :
974 563824 : k = negate( t_min );
975 563824 : h_e = add( 1, norm_s( h[0] ) ); // exponent of h
976 :
977 : /*-----------------------------------------------------------------*
978 : * compute the filtered excitation for the first delay t_min
979 : *-----------------------------------------------------------------*/
980 :
981 563824 : conv_fx( &exc[k], h, excf, L_subfr ); // Q_new - h_e
982 :
983 : /*----------------------------------------------------------------*
984 : * loop for every possible period
985 : *----------------------------------------------------------------*/
986 :
987 13631096 : FOR( t = t_min; t <= t_max; t++ )
988 : {
989 : /* Compute correlation between xn[] and excf[] */
990 :
991 13067272 : L_tmp = L_mac( 0, xn[0], excf[0] );
992 860022272 : FOR( i = 1; i < L_subfr; i++ )
993 : {
994 : #ifdef ISSUE_1867_replace_overflow_libenc
995 846955000 : L_tmp = L_mac_sat( L_tmp, xn[i], excf[i] ); // (Q_new - 1) + (Q_new - h_e) + 1
996 : #else
997 : L_tmp = L_mac_o( L_tmp, xn[i], excf[i], &Overflow ); // (Q_new - 1) + (Q_new - h_e) + 1
998 : #endif
999 : }
1000 13067272 : exp = norm_l( L_tmp );
1001 13067272 : L_tmp = L_shl( L_tmp, exp );
1002 13067272 : exp_corr = sub( 31, add( exp, ( Q_new - 1 ) + ( Q_new - h_e ) + 1 ) );
1003 13067272 : corr = extract_h( L_tmp );
1004 :
1005 : /* Compute 1/sqrt(energy of excf[]) */
1006 13067272 : Word16 _0_01 = extract_l( L_shr( 21474836 /* 0.01 in Q31 */, sub( 31, add( shl( sub( Q_new, h_e ), 1 ), 1 ) ) ) );
1007 :
1008 13067272 : W_tmp = W_mac_16_16( _0_01, excf[0], excf[0] );
1009 860022272 : FOR( i = 1; i < L_subfr; i++ )
1010 : {
1011 846955000 : W_tmp = W_mac_16_16( W_tmp, excf[i], excf[i] ); // 2*(Q_new - h_e) + 1
1012 : }
1013 :
1014 13067272 : exp = W_norm( W_tmp );
1015 13067272 : L_tmp = W_extract_h( W_shl( W_tmp, exp ) ); // 2*(Q_new - h_e) + 1 + exp - 32
1016 13067272 : exp_norm = sub( 31 + 32, add( add( shl( sub( Q_new, h_e ), 1 ), 1 ), exp ) );
1017 :
1018 13067272 : L_tmp = Isqrt_lc( L_tmp, &exp_norm );
1019 13067272 : norm = extract_h( L_tmp );
1020 :
1021 : /* Normalize correlation = correlation * (1/sqrt(energy)) */
1022 13067272 : L_tmp = L_mult( corr, norm );
1023 :
1024 13067272 : ncorr[t] = round_fx_sat( L_tmp );
1025 13067272 : ncorr_e[t - t_min] = add( exp_corr, exp_norm );
1026 13067272 : move16();
1027 13067272 : move16();
1028 :
1029 : /* update the filtered excitation excf[] for the next iteration */
1030 13067272 : IF( NE_16( t, t_max ) )
1031 : {
1032 12503448 : k--;
1033 822455232 : FOR( i = (Word16) ( L_subfr - 1 ); i > 0; i-- )
1034 : {
1035 : /* saturation can occur in add() */
1036 809951784 : excf[i] = round_fx_sat( L_mac_sat( L_mult( excf[i - 1], 32767 ), exc[k], h[i] ) );
1037 809951784 : move16();
1038 : }
1039 12503448 : excf[0] = mult_r( exc[k], h[0] );
1040 12503448 : move16();
1041 : }
1042 : }
1043 : // Aligning the values of ncorr to a common exponent
1044 563824 : maximum_fx( ncorr_e, t_max + 1 - t_min, &e_max );
1045 13631096 : FOR( t = t_min; t <= t_max; t++ )
1046 : {
1047 13067272 : ncorr[t] = shr( ncorr[t], sub( e_max, ncorr_e[t - t_min] ) );
1048 13067272 : move16();
1049 : }
1050 :
1051 563824 : return;
1052 : }
1053 :
1054 :
1055 47942 : void norm_corr_fx(
1056 : const Word16 exc[], /* i : excitation buffer Q_new*/
1057 : const Word16 xn[], /* i : target signal Q_new-1+shift*/
1058 : const Word16 h[], /* i : weighted synthesis filter impulse response Q(14+shift)*/
1059 : const Word16 t_min, /* i : minimum value of searched range */
1060 : const Word16 t_max, /* i : maximum value of searched range */
1061 : Word16 ncorr[], /* o : normalized correlation Q15 */
1062 : const Word16 L_subfr /* i : subframe size */
1063 : )
1064 : {
1065 : Word16 i, k, t;
1066 : Word16 corr, exp_corr, norm, exp_norm, exp, scale;
1067 : Word16 excf[L_FRAME16k];
1068 : Word32 L_tmp;
1069 : #ifndef ISSUE_1867_replace_overflow_libenc
1070 : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
1071 : Flag Overflow = 0;
1072 : move32();
1073 : #endif
1074 : #endif
1075 :
1076 47942 : k = negate( t_min );
1077 :
1078 : /*-----------------------------------------------------------------*
1079 : * compute the filtered excitation for the first delay t_min
1080 : *-----------------------------------------------------------------*/
1081 :
1082 47942 : conv_fx( &exc[k], h, excf, L_subfr );
1083 :
1084 : /* Compute rounded down 1/sqrt(energy of xn[]) */
1085 : #ifdef ISSUE_1867_replace_overflow_libenc
1086 47942 : L_tmp = L_mac_sat( 1, xn[0], xn[0] );
1087 : #else
1088 : L_tmp = L_mac_o( 1, xn[0], xn[0], &Overflow );
1089 : #endif
1090 3069312 : FOR( i = 1; i < L_subfr; i++ )
1091 : {
1092 : #ifdef ISSUE_1867_replace_overflow_libenc
1093 3021370 : L_tmp = L_mac_sat( L_tmp, xn[i], xn[i] );
1094 : #else
1095 : L_tmp = L_mac_o( L_tmp, xn[i], xn[i], &Overflow );
1096 : #endif
1097 : }
1098 47942 : exp = norm_l( L_tmp );
1099 47942 : exp = sub( 30, exp );
1100 :
1101 47942 : exp = add( exp, 2 ); /* energy of xn[] x 2 + rounded up */
1102 47942 : scale = negate( shr( exp, 1 ) ); /* (1<<scale) < 1/sqrt(energy rounded) */
1103 :
1104 : /*----------------------------------------------------------------*
1105 : * loop for every possible period
1106 : *----------------------------------------------------------------*/
1107 :
1108 1191638 : FOR( t = t_min; t <= t_max; t++ )
1109 : {
1110 : /* Compute correlation between xn[] and excf[] */
1111 :
1112 1143696 : L_tmp = L_mac( 1, xn[0], excf[0] );
1113 73212928 : FOR( i = 1; i < L_subfr; i++ )
1114 : {
1115 : #ifdef ISSUE_1867_replace_overflow_libenc
1116 72069232 : L_tmp = L_mac_sat( L_tmp, xn[i], excf[i] );
1117 : #else
1118 : L_tmp = L_mac_o( L_tmp, xn[i], excf[i], &Overflow );
1119 : #endif
1120 : }
1121 1143696 : exp = norm_l( L_tmp );
1122 1143696 : L_tmp = L_shl( L_tmp, exp );
1123 1143696 : exp_corr = sub( 30, exp );
1124 1143696 : corr = extract_h( L_tmp );
1125 :
1126 : /* Compute 1/sqrt(energy of excf[]) */
1127 1143696 : L_tmp = L_mac( 1, excf[0], excf[0] );
1128 73212928 : FOR( i = 1; i < L_subfr; i++ )
1129 : {
1130 : #ifdef ISSUE_1867_replace_overflow_libenc
1131 72069232 : L_tmp = L_mac_sat( L_tmp, excf[i], excf[i] );
1132 : #else
1133 : L_tmp = L_mac_o( L_tmp, excf[i], excf[i], &Overflow );
1134 : #endif
1135 : }
1136 :
1137 1143696 : exp = norm_l( L_tmp );
1138 1143696 : L_tmp = L_shl( L_tmp, exp );
1139 1143696 : exp_norm = sub( 30, exp );
1140 :
1141 1143696 : L_tmp = Isqrt_lc( L_tmp, &exp_norm );
1142 1143696 : norm = extract_h( L_tmp );
1143 :
1144 : /* Normalize correlation = correlation * (1/sqrt(energy)) */
1145 1143696 : L_tmp = L_mult( corr, norm );
1146 1143696 : L_tmp = L_shl_sat( L_tmp, add( add( exp_corr, exp_norm ), scale ) );
1147 1143696 : ncorr[t] = round_fx_sat( L_tmp );
1148 :
1149 : /* update the filtered excitation excf[] for the next iteration */
1150 1143696 : IF( NE_16( t, t_max ) )
1151 : {
1152 1095754 : k--;
1153 70143616 : FOR( i = ( L_subfr - 1 ); i > 0; i-- )
1154 : {
1155 : /* saturation can occur in add() */
1156 : /*excf[i] = add(mult(exc[k], h[i]), excf[i - 1]); move16(); */
1157 69047862 : excf[i] = round_fx_sat( L_mac_sat( L_mult( excf[i - 1], 32767 ), exc[k], h[i] ) );
1158 69047862 : move16();
1159 : }
1160 1095754 : excf[0] = mult_r( exc[k], h[0] );
1161 1095754 : move16();
1162 : }
1163 : }
1164 :
1165 47942 : return;
1166 : }
1167 :
1168 :
1169 : /*-------------------------------------------------------------------*
1170 : * pit_Q_enc()
1171 : *
1172 : * Encode subframe pitch lag
1173 : *-------------------------------------------------------------------*/
1174 :
1175 255202 : void pit_Q_enc_fx(
1176 : BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle */
1177 : const Word16 Opt_AMR_WB, /* i : flag indicating AMR-WB IO mode */
1178 : const Word16 nBits, /* i : # of Q bits */
1179 : const Word16 delta, /* i : Half the CL searched interval */
1180 : const Word16 pit_flag, /* i : absolute(0) or delta(1) pitch Q */
1181 : const Word16 limit_flag, /* i : restrained(0) or extended(1) Q limits */
1182 : const Word16 T0, /* i : integer pitch lag */
1183 : const Word16 T0_frac, /* i : pitch fraction */
1184 : Word16 *T0_min, /* i/o: delta search min */
1185 : Word16 *T0_max /* o : delta search max_val */
1186 : )
1187 : {
1188 : Word16 pitch_index;
1189 :
1190 255202 : IF( EQ_16( nBits, 10 ) ) /* absolute encoding with 10 bits */
1191 : {
1192 76457 : IF( limit_flag == 0 )
1193 : {
1194 : /* pitch_index = T0*4 + T0_frac - (PIT_MIN*4);*/
1195 0 : pitch_index = sub( add( shl( T0, 2 ), T0_frac ), ( PIT_MIN * 4 ) );
1196 : }
1197 76457 : ELSE IF( EQ_16( limit_flag, 1 ) )
1198 : {
1199 : /*pitch_index = T0*4 + T0_frac - (PIT_MIN_EXTEND*4);*/
1200 67973 : pitch_index = sub( add( shl( T0, 2 ), T0_frac ), ( PIT_MIN_EXTEND * 4 ) );
1201 : }
1202 : ELSE /* limit_flag == 2 */
1203 : {
1204 : /*pitch_index = T0*4 + T0_frac - (PIT_MIN_DOUBLEEXTEND*4);*/
1205 8484 : pitch_index = sub( add( shl( T0, 2 ), T0_frac ), ( PIT_MIN_DOUBLEEXTEND * 4 ) );
1206 : }
1207 : }
1208 178745 : ELSE IF( EQ_16( nBits, 9 ) ) /* absolute encoding with 9 bits */
1209 : {
1210 36983 : pitch_index = abs_pit_enc_fx( 4, limit_flag, T0, T0_frac );
1211 :
1212 : /* find T0_min and T0_max for delta search */
1213 36983 : IF( Opt_AMR_WB )
1214 : {
1215 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 */
1216 : }
1217 : }
1218 141762 : ELSE IF( EQ_16( nBits, 8 ) ) /* absolute encoding with 8 bits */
1219 : {
1220 4497 : pitch_index = abs_pit_enc_fx( 2, limit_flag, T0, T0_frac );
1221 :
1222 : /* find T0_min and T0_max for delta search */
1223 4497 : IF( Opt_AMR_WB )
1224 : {
1225 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 */
1226 : }
1227 : }
1228 137265 : ELSE IF( EQ_16( nBits, 6 ) ) /* relative encoding with 6 bits */
1229 : {
1230 103540 : pitch_index = delta_pit_enc_fx( 4, T0, T0_frac, *T0_min );
1231 : }
1232 33725 : ELSE IF( EQ_16( nBits, 5 ) ) /* relative encoding with 5 bits */
1233 : {
1234 33725 : IF( EQ_16( delta, 8 ) )
1235 : {
1236 7034 : pitch_index = delta_pit_enc_fx( 2, T0, T0_frac, *T0_min );
1237 : }
1238 : ELSE /* delta == 4 */
1239 : {
1240 26691 : pitch_index = delta_pit_enc_fx( 4, T0, T0_frac, *T0_min );
1241 : }
1242 : }
1243 : ELSE /* nBits == 4 ) */ /* relative encoding with 4 bits */
1244 : {
1245 0 : IF( EQ_16( delta, 8 ) )
1246 : {
1247 0 : pitch_index = delta_pit_enc_fx( 0, T0, T0_frac, *T0_min );
1248 : }
1249 : ELSE /* delta == 4 */
1250 : {
1251 0 : pitch_index = delta_pit_enc_fx( 2, T0, T0_frac, *T0_min );
1252 : }
1253 : }
1254 :
1255 255202 : IF( !Opt_AMR_WB )
1256 : {
1257 : /* find T0_min and T0_max for delta search */
1258 255202 : limit_T0_fx( L_FRAME, delta, L_SUBFR, limit_flag, T0, T0_frac, T0_min, T0_max );
1259 : }
1260 :
1261 : {
1262 255202 : push_indice( hBstr, IND_PITCH, pitch_index, nBits );
1263 : }
1264 :
1265 255202 : return;
1266 : }
1267 :
1268 :
1269 : /*-------------------------------------------------------------------*
1270 : * pit16k_Q_enc()
1271 : *
1272 : * Encode subframe pitch lag @16kHz core
1273 : *-------------------------------------------------------------------*/
1274 :
1275 332082 : void pit16k_Q_enc_fx(
1276 : BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle */
1277 : const Word16 nBits, /* i : # of Q bits */
1278 : const Word16 limit_flag, /* i : restrained(0) or extended(1) Q limits */
1279 : const Word16 T0, /* i : integer pitch lag */
1280 : const Word16 T0_frac, /* i : pitch fraction */
1281 : Word16 *T0_min, /* i/o: delta search min */
1282 : Word16 *T0_max /* o : delta search max_val */
1283 : )
1284 : {
1285 : Word16 pitch_index;
1286 :
1287 332082 : IF( EQ_16( nBits, 10 ) ) /* absolute encoding with 10 bits */
1288 : {
1289 : {
1290 138552 : IF( LT_16( T0, PIT16k_FR2_EXTEND_10b ) )
1291 : {
1292 : /*pitch_index = T0*4 + T0_frac - (PIT16k_MIN_EXTEND*4);*/
1293 135065 : pitch_index = add( shl( T0, 2 ), sub( T0_frac, ( PIT16k_MIN_EXTEND * 4 ) ) );
1294 : }
1295 : ELSE
1296 : {
1297 : /*pitch_index = T0*2 + (T0_frac>>1) - (PIT16k_FR2_EXTEND_10b*2) + ((PIT16k_FR2_EXTEND_10b-PIT16k_MIN_EXTEND)*4);*/
1298 3487 : pitch_index = add( sub( add( shl( T0, 1 ), shr( T0_frac, 1 ) ), ( PIT16k_FR2_EXTEND_10b * 2 ) ), ( ( PIT16k_FR2_EXTEND_10b - PIT16k_MIN_EXTEND ) * 4 ) );
1299 : }
1300 : }
1301 :
1302 138552 : push_indice( hBstr, IND_PITCH, pitch_index, nBits );
1303 : }
1304 193530 : ELSE IF( EQ_16( nBits, 9 ) ) /* absolute encoding with 9 bits */
1305 : {
1306 : {
1307 : /*-------------------------------------------------------------------*
1308 : * The pitch range is encoded absolutely with 9 bits
1309 : * and is divided as follows:
1310 : * PIT16k_EXTEND_MIN to PIT16k_FR2_EXTEND_9b-1 resolution 1/4 (frac = 0,1,2 or 3)
1311 : * PIT16k_FR2_EXTEND_9b to PIT16k_FR1_EXTEND_9b-1 resolution 1/2 (frac = 0 or 2)
1312 : * PIT16k_FR1_EXTEND_9b to PIT16k_MAX_EXTEND resolution 1 (frac = 0)
1313 : *-------------------------------------------------------------------*/
1314 :
1315 0 : IF( LT_16( T0, PIT16k_FR2_EXTEND_9b ) )
1316 : {
1317 : /*pitch_index = T0*4 + T0_frac - (PIT16k_MIN_EXTEND*4);*/
1318 0 : pitch_index = add( shl( T0, 2 ), sub( T0_frac, ( PIT16k_MIN_EXTEND * 4 ) ) );
1319 : }
1320 0 : ELSE IF( LT_16( T0, PIT16k_FR1_EXTEND_9b ) )
1321 : {
1322 : /*pitch_index = T0*2 + (T0_frac>>1) - (PIT16k_FR2_EXTEND_9b*2) + ((PIT16k_FR2_EXTEND_9b-PIT16k_MIN_EXTEND)*4);*/
1323 0 : pitch_index = add( sub( add( shl( T0, 1 ), shr( T0_frac, 1 ) ), ( PIT16k_FR2_EXTEND_9b * 2 ) ), ( ( PIT16k_FR2_EXTEND_9b - PIT16k_MIN_EXTEND ) * 4 ) );
1324 : }
1325 : ELSE
1326 : {
1327 : /*pitch_index = T0 - PIT16k_FR1_EXTEND_9b + ((PIT16k_FR2_EXTEND_9b-PIT16k_MIN_EXTEND)*4) + ((PIT16k_FR1_EXTEND_9b-PIT16k_FR2_EXTEND_9b)*2);*/
1328 0 : pitch_index = add( add( sub( T0, PIT16k_FR1_EXTEND_9b ), ( ( PIT16k_FR2_EXTEND_9b - PIT16k_MIN_EXTEND ) * 4 ) ), ( ( PIT16k_FR1_EXTEND_9b - PIT16k_FR2_EXTEND_9b ) * 2 ) );
1329 : }
1330 : }
1331 :
1332 0 : push_indice( hBstr, IND_PITCH, pitch_index, 9 );
1333 : }
1334 : ELSE /* nBits == 6 */ /* relative encoding with 6 bits */
1335 : {
1336 : /*pitch_index = (T0 - *T0_min) * 4 + T0_frac;*/
1337 193530 : pitch_index = add( shl( sub( T0, *T0_min ), 2 ), T0_frac );
1338 :
1339 193530 : push_indice( hBstr, IND_PITCH, pitch_index, nBits );
1340 : }
1341 :
1342 332082 : limit_T0_fx( L_FRAME16k, 8, L_SUBFR, limit_flag, T0, T0_frac, T0_min, T0_max );
1343 :
1344 332082 : return;
1345 : }
1346 :
1347 :
1348 : /*------------------------------------------------------------------*
1349 : * pit_encode:
1350 : *
1351 : * Close-loop pitch lag search and pitch lag quantization
1352 : * Adaptive excitation construction
1353 : *------------------------------------------------------------------*/
1354 :
1355 3060 : void Mode2_pit_encode_fx(
1356 : const Word16 coder_type, /* i : coding model */
1357 : const Word16 i_subfr, /* i : subframe index */
1358 : Word16 **pt_indice, /* i/o: quantization indices pointer */
1359 : Word16 *exc, /* i/o: pointer to excitation signal frame */
1360 : const Word16 *T_op, /* i : open loop pitch estimates in current frame */
1361 : Word16 *T0_min, /* i/o: lower limit for close-loop search */
1362 : Word16 *T0_min_frac, /* i/o: lower limit for close-loop search */
1363 : Word16 *T0_max, /* i/o: higher limit for close-loop search */
1364 : Word16 *T0_max_frac, /* i/o: higher limit for close-loop search */
1365 : Word16 *T0, /* i/o: close loop integer pitch */
1366 : Word16 *T0_frac, /* i/o: close loop fractional part of the pitch */
1367 : Word16 *T0_res, /* i/o: close loop pitch resolution */
1368 : Word16 *h1, /* i : weighted filter impulse response 1Q14+shift*/
1369 : Word16 *xn, /* i : target vector */
1370 : Word16 pit_min,
1371 : Word16 pit_fr1,
1372 : Word16 pit_fr1b,
1373 : Word16 pit_fr2,
1374 : Word16 pit_max,
1375 : Word16 pit_res_max )
1376 : {
1377 : Word16 pit_flag;
1378 :
1379 : BASOP_SATURATE_ERROR_ON_EVS;
1380 :
1381 : /* Pitch flag */
1382 3060 : pit_flag = i_subfr;
1383 3060 : move16();
1384 3060 : if ( EQ_16( i_subfr, ( 2 * L_SUBFR ) ) )
1385 : {
1386 612 : pit_flag = 0;
1387 612 : move16();
1388 : }
1389 :
1390 : /*-----------------------------------------------------------------*
1391 : * - Limit range of pitch search
1392 : * - Fractional pitch search
1393 : * - Pitch quantization
1394 : *-----------------------------------------------------------------*/
1395 :
1396 3060 : IF( coder_type == 0 ) /*Unvoiced Coding do nothing*/
1397 : {
1398 0 : *T0 = L_SUBFR;
1399 0 : move16();
1400 0 : *T0_frac = 0;
1401 0 : move16();
1402 0 : *T0_res = 1;
1403 0 : move16();
1404 : }
1405 3060 : ELSE IF( EQ_16( coder_type, 1 ) ) /* 8/4/4/4 (EVS) */
1406 : {
1407 0 : IF( i_subfr == 0 )
1408 : {
1409 0 : limit_T0_voiced( 4, shr( pit_res_max, 1 ), T_op[0], 0, 1, T0_min, T0_min_frac, T0_max, T0_max_frac, pit_min, pit_max );
1410 : }
1411 : ELSE
1412 : {
1413 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 );
1414 : }
1415 0 : *T0 = E_GAIN_closed_loop_search_fx( exc, xn, h1, *T0_min, *T0_min_frac, *T0_max, *T0_max_frac, shr( pit_res_max, 1 ), T0_frac, T0_res, pit_res_max,
1416 : i_subfr, pit_min, pit_min, pit_fr1b, L_SUBFR );
1417 0 : move16();
1418 :
1419 0 : IF( i_subfr == 0 )
1420 : {
1421 0 : Mode2_abs_pit_enc_fx( *T0, *T0_frac, pt_indice, pit_min, pit_fr1b, pit_min, pit_res_max );
1422 : }
1423 : ELSE
1424 : {
1425 0 : Mode2_delta_pit_enc_fx( *T0, *T0_frac, shr( pit_res_max, 1 ), *T0_min, *T0_min_frac, pt_indice );
1426 : }
1427 : }
1428 3060 : ELSE IF( EQ_16( coder_type, 2 ) ) /* 8/5/8/5 (EVS) */
1429 : {
1430 0 : IF( i_subfr == 0 )
1431 : {
1432 0 : limit_T0_voiced( 5, shr( pit_res_max, 1 ), T_op[0], 0, 1, T0_min, T0_min_frac, T0_max, T0_max_frac, pit_min, pit_max );
1433 : }
1434 0 : ELSE IF( i_subfr == 2 * L_SUBFR )
1435 : {
1436 0 : limit_T0_voiced( 5, shr( pit_res_max, 1 ), T_op[1], 0, 1, T0_min, T0_min_frac, T0_max, T0_max_frac, pit_min, pit_max );
1437 : }
1438 : ELSE
1439 : {
1440 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 );
1441 : }
1442 0 : *T0 = E_GAIN_closed_loop_search_fx( exc, xn, h1, *T0_min, *T0_min_frac, *T0_max, *T0_max_frac, shr( pit_res_max, 1 ), T0_frac, T0_res, pit_res_max,
1443 : pit_flag, pit_min, pit_min, pit_fr1b, L_SUBFR );
1444 0 : move16();
1445 :
1446 0 : IF( pit_flag == 0 )
1447 : {
1448 0 : Mode2_abs_pit_enc_fx( *T0, *T0_frac, pt_indice, pit_min, pit_fr1b, pit_min, pit_res_max );
1449 : }
1450 : ELSE
1451 : {
1452 0 : Mode2_delta_pit_enc_fx( *T0, *T0_frac, shr( pit_res_max, 1 ), *T0_min, *T0_min_frac, pt_indice );
1453 : }
1454 : }
1455 3060 : ELSE IF( EQ_16( coder_type, 3 ) ) /* 9/6/6/6 (HRs- VC) */
1456 : {
1457 1350 : Word16 pit_res_max2 = pit_res_max;
1458 :
1459 1350 : IF( EQ_16( pit_min, PIT_MIN_16k ) )
1460 : {
1461 1350 : pit_res_max2 = shr( pit_res_max, 1 );
1462 : }
1463 :
1464 1350 : IF( ( i_subfr == 0 ) )
1465 : {
1466 270 : limit_T0_voiced2( pit_res_max2, T_op, T0_min, T0_min_frac, T0_max, T0_max_frac, pit_min, pit_max, i_subfr );
1467 : }
1468 : ELSE
1469 : {
1470 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 );
1471 : }
1472 1350 : *T0 = E_GAIN_closed_loop_search_fx( exc, xn, h1, *T0_min, *T0_min_frac, *T0_max, *T0_max_frac, pit_res_max2, T0_frac, T0_res, pit_res_max,
1473 : i_subfr, pit_min, pit_fr2, pit_fr1, L_SUBFR );
1474 1350 : move16();
1475 :
1476 1350 : IF( i_subfr == 0 ) /* if 1st subframe */
1477 : {
1478 270 : Mode2_abs_pit_enc_fx( *T0, *T0_frac, pt_indice, pit_min, pit_fr1, pit_fr2, pit_res_max );
1479 : }
1480 : ELSE
1481 : {
1482 1080 : Mode2_delta_pit_enc_fx( *T0, *T0_frac, pit_res_max2, *T0_min, *T0_min_frac, pt_indice );
1483 : }
1484 : }
1485 1710 : ELSE IF( coder_type == 4 ) /* 9/6/9/6 (AMRWB) */
1486 : {
1487 1710 : Word16 pit_res_max2 = pit_res_max;
1488 1710 : IF( EQ_16( pit_min, PIT_MIN_16k ) )
1489 : {
1490 1710 : pit_res_max2 = shr( pit_res_max, 1 );
1491 : }
1492 1710 : test();
1493 1710 : IF( ( i_subfr == 0 ) || EQ_16( i_subfr, shl( L_SUBFR, 1 ) ) )
1494 : {
1495 684 : limit_T0_voiced2( pit_res_max2, T_op, T0_min, T0_min_frac, T0_max, T0_max_frac, pit_min, pit_max, i_subfr );
1496 : }
1497 : ELSE
1498 : {
1499 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 );
1500 : }
1501 1710 : *T0 = E_GAIN_closed_loop_search_fx( exc, xn, h1, *T0_min, *T0_min_frac, *T0_max, *T0_max_frac, pit_res_max2, T0_frac, T0_res, pit_res_max,
1502 : pit_flag, pit_min, pit_fr2, pit_fr1, L_SUBFR );
1503 1710 : move16();
1504 1710 : IF( pit_flag == 0 ) /* if 1st/3rd/5th subframe */
1505 : {
1506 684 : Mode2_abs_pit_enc_fx( *T0, *T0_frac, pt_indice, pit_min, pit_fr1, pit_fr2, pit_res_max );
1507 : }
1508 : ELSE /* if subframe 2 or 4 */
1509 : {
1510 1026 : Mode2_delta_pit_enc_fx( *T0, *T0_frac, pit_res_max2, *T0_min, *T0_min_frac, pt_indice );
1511 : }
1512 : }
1513 0 : ELSE IF( EQ_16( coder_type, 8 ) ) /* 8/5/5/5 (RF all pred mode) */
1514 : {
1515 0 : IF( i_subfr == 0 )
1516 : {
1517 0 : limit_T0_voiced( 5, shr( pit_res_max, 1 ), T_op[0], 0, 1, T0_min, T0_min_frac, T0_max, T0_max_frac, pit_min, pit_max );
1518 : }
1519 : ELSE
1520 : {
1521 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 );
1522 : }
1523 0 : *T0 = E_GAIN_closed_loop_search_fx( exc, xn, h1, *T0_min, *T0_min_frac, *T0_max, *T0_max_frac, shr( pit_res_max, 1 ), T0_frac, T0_res, pit_res_max,
1524 : i_subfr, pit_min, pit_min, pit_fr1b, L_SUBFR );
1525 0 : move16();
1526 :
1527 0 : IF( i_subfr == 0 )
1528 : {
1529 0 : Mode2_abs_pit_enc_fx( *T0, *T0_frac, pt_indice, pit_min, pit_fr1b, pit_min, pit_res_max );
1530 : }
1531 : ELSE
1532 : {
1533 0 : Mode2_delta_pit_enc_fx( *T0, *T0_frac, shr( pit_res_max, 1 ), *T0_min, *T0_min_frac, pt_indice );
1534 : }
1535 : }
1536 0 : ELSE IF( EQ_16( coder_type, 9 ) ) /* 8/0/8/0 (RF mode Gen pred) */
1537 : {
1538 0 : IF( i_subfr == 0 )
1539 : {
1540 0 : limit_T0_voiced( 4, shr( pit_res_max, 1 ), T_op[0], 0, 1, T0_min, T0_min_frac, T0_max, T0_max_frac, pit_min, pit_max );
1541 : }
1542 : ELSE
1543 : {
1544 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 );
1545 : }
1546 0 : *T0 = E_GAIN_closed_loop_search_fx( exc, xn, h1, *T0_min, *T0_min_frac, *T0_max, *T0_max_frac, shr( pit_res_max, 1 ), T0_frac, T0_res, pit_res_max,
1547 : i_subfr, pit_min, pit_min, pit_fr1b, L_SUBFR );
1548 0 : move16();
1549 :
1550 0 : IF( i_subfr == 0 )
1551 : {
1552 0 : Mode2_abs_pit_enc_fx( *T0, *T0_frac, pt_indice, pit_min, pit_fr1b, pit_min, pit_res_max );
1553 : }
1554 : ELSE
1555 : {
1556 0 : Mode2_delta_pit_enc_fx( *T0, *T0_frac, shr( pit_res_max, 1 ), *T0_min, *T0_min_frac, pt_indice );
1557 : }
1558 : }
1559 :
1560 : BASOP_SATURATE_ERROR_OFF_EVS;
1561 :
1562 3060 : return;
1563 : }
1564 :
1565 :
1566 954 : static void limit_T0_voiced2(
1567 : Word16 res,
1568 : const Word16 *T_op,
1569 : Word16 *T0_min,
1570 : Word16 *T0_min_frac,
1571 : Word16 *T0_max,
1572 : Word16 *T0_max_frac,
1573 : Word16 pit_min,
1574 : Word16 pit_max,
1575 : Word16 i_subfr )
1576 : {
1577 : Word16 t, temp1, temp2, res2;
1578 :
1579 954 : assert( res > 1 && res <= 6 );
1580 :
1581 954 : res2 = res;
1582 954 : move16();
1583 954 : if ( EQ_16( res, 6 ) )
1584 : {
1585 0 : res2 = shr( res2, 1 );
1586 : }
1587 :
1588 : /* Lower-bound */
1589 954 : IF( i_subfr == 0 )
1590 : {
1591 612 : temp1 = sub( i_mult2( T_op[0], res ), 32 );
1592 : }
1593 : ELSE
1594 : {
1595 342 : temp1 = sub( i_mult2( T_op[1], res ), 32 );
1596 : }
1597 :
1598 954 : IF( LT_16( T_op[0], T_op[1] ) )
1599 : {
1600 264 : t = sub( i_mult2( T_op[0], res ), 16 );
1601 : }
1602 : ELSE
1603 : {
1604 690 : t = sub( i_mult2( T_op[1], res ), 16 );
1605 : }
1606 :
1607 954 : if ( LT_16( temp1, t ) )
1608 : {
1609 840 : temp1 = t;
1610 840 : move16();
1611 : }
1612 :
1613 954 : temp2 = mult( temp1, inv_T0_res[res2] );
1614 954 : if ( EQ_16( res, 6 ) )
1615 : {
1616 0 : temp2 = shr( temp2, 1 );
1617 : }
1618 :
1619 954 : *T0_min = temp2;
1620 954 : move16();
1621 :
1622 954 : *T0_min_frac = sub( temp1, i_mult2( temp2, res ) );
1623 954 : move16();
1624 :
1625 954 : IF( LT_16( *T0_min, pit_min ) )
1626 : {
1627 14 : *T0_min = pit_min;
1628 14 : move16();
1629 14 : *T0_min_frac = 0;
1630 14 : move16();
1631 : }
1632 :
1633 : /* Higher-bound */
1634 954 : temp1 = add( i_mult2( *T0_min, res ), add( *T0_min_frac, 63 ) );
1635 :
1636 954 : IF( T_op[0] < T_op[1] )
1637 : {
1638 264 : t = add( i_mult2( T_op[1], res ), add( 15, res ) );
1639 : }
1640 : ELSE
1641 : {
1642 690 : t = add( i_mult2( T_op[0], res ), add( 15, res ) );
1643 : }
1644 :
1645 954 : if ( GT_16( temp1, t ) )
1646 : {
1647 860 : temp1 = t;
1648 860 : move16();
1649 : }
1650 :
1651 954 : temp2 = mult( temp1, inv_T0_res[res] );
1652 :
1653 954 : *T0_max = temp2;
1654 954 : move16();
1655 :
1656 954 : *T0_max_frac = sub( temp1, i_mult2( temp2, res ) );
1657 954 : move16();
1658 :
1659 954 : IF( GT_16( *T0_max, pit_max ) )
1660 : {
1661 8 : *T0_max = pit_max;
1662 8 : *T0_max_frac = sub( res, 1 );
1663 8 : move16();
1664 8 : move16();
1665 :
1666 8 : temp1 = add( sub( i_mult2( *T0_max, res ), 64 ), res );
1667 :
1668 8 : temp2 = mult( temp1, inv_T0_res[res2] );
1669 8 : IF( EQ_16( res, 6 ) )
1670 : {
1671 0 : temp2 = shr( temp2, 1 );
1672 : }
1673 :
1674 8 : *T0_min = temp2;
1675 8 : move16();
1676 :
1677 8 : *T0_min_frac = sub( temp1, i_mult2( temp2, res ) );
1678 8 : move16();
1679 : }
1680 :
1681 954 : return;
1682 : }
1683 :
1684 :
1685 : /*-------------------------------------------------------------------*
1686 : * abs_pit_enc:
1687 : *
1688 : * Encode pitch lag absolutely
1689 : *-------------------------------------------------------------------*/
1690 :
1691 954 : void Mode2_abs_pit_enc_fx(
1692 : Word16 T0, /* i : integer pitch lag */
1693 : Word16 T0_frac, /* i : pitch fraction */
1694 : Word16 **pt_indice, /* i/o: pointer to Vector of Q indexes */
1695 : Word16 pit_min,
1696 : Word16 pit_fr1,
1697 : Word16 pit_fr2,
1698 : Word16 pit_res_max )
1699 : {
1700 : Word16 pit_res_max_half;
1701 :
1702 :
1703 954 : pit_res_max_half = shr( pit_res_max, 1 );
1704 :
1705 954 : IF( LT_16( T0, pit_fr2 ) )
1706 : {
1707 0 : **pt_indice = add( i_mult2( T0, pit_res_max ), sub( T0_frac, i_mult2( pit_min, pit_res_max ) ) );
1708 0 : move16();
1709 : }
1710 954 : ELSE IF( LT_16( T0, pit_fr1 ) )
1711 : {
1712 842 : **pt_indice = add( sub( add( i_mult2( T0, pit_res_max_half ), T0_frac ), i_mult2( pit_fr2, pit_res_max_half ) ), i_mult2( sub( pit_fr2, pit_min ), pit_res_max ) );
1713 842 : move16();
1714 : }
1715 : ELSE
1716 : {
1717 112 : **pt_indice = add( add( sub( T0, pit_fr1 ), i_mult2( sub( pit_fr2, pit_min ), pit_res_max ) ), i_mult2( sub( pit_fr1, pit_fr2 ), pit_res_max_half ) );
1718 112 : move16();
1719 : }
1720 :
1721 954 : ( *pt_indice )++;
1722 :
1723 954 : return;
1724 : }
1725 :
1726 :
1727 : /*-------------------------------------------------------------------*
1728 : * delta_pit_enc:
1729 : *
1730 : * Encode pitch lag differentially
1731 : *-------------------------------------------------------------------*/
1732 :
1733 2106 : void Mode2_delta_pit_enc_fx(
1734 : Word16 T0, /* i : integer pitch lag */
1735 : Word16 T0_frac, /* i : pitch fraction */
1736 : Word16 T0_res, /* i : pitch resolution */
1737 : Word16 T0_min, /* i/o: delta search min */
1738 : Word16 T0_min_frac, /* i/o: delta search min */
1739 : Word16 **pt_indice /* i/o: pointer to Vector of Q indexes */
1740 : )
1741 : {
1742 : /***pt_indice = (T0 - T0_min) * T0_res + T0_frac - T0_min_frac;*/
1743 2106 : **pt_indice = add( i_mult2( sub( T0, T0_min ), T0_res ), sub( T0_frac, T0_min_frac ) );
1744 2106 : move16();
1745 2106 : ( *pt_indice )++;
1746 :
1747 2106 : return;
1748 : }
|