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.h" /* Static table prototypes */
11 : #include "rom_basop_util.h"
12 : #include "prot_fx.h" /* Function prototypes */
13 : #include "prot_fx_enc.h" /* Function prototypes */
14 : #include "prot_fx_enc.h" /* Function prototypes */
15 :
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 568660 : 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 568660 : L_sufr_sft = 6;
87 568660 : move16();
88 568660 : if ( EQ_16( L_subfr, 2 * L_SUBFR ) )
89 : {
90 6630 : L_sufr_sft = 7;
91 6630 : move16();
92 : }
93 :
94 : /*----------------------------------------------------------------*
95 : * convert pitch values to 16kHz domain
96 : *----------------------------------------------------------------*/
97 568660 : test();
98 568660 : test();
99 568660 : IF( EQ_16( L_frame, L_FRAME ) || ( tdm_Pri_pitch_buf != NULL && tdm_Pri_pitch_buf[0] < 0 ) )
100 : {
101 242761 : 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 325899 : T_op[0] = shr( add( round_fx( L_shl( L_mult( 20480, pitch[0] ), 2 ) ), 1 ), 1 );
108 325899 : move16();
109 325899 : T_op[1] = shr( add( round_fx( L_shl( L_mult( 20480, pitch[1] ), 2 ) ), 1 ), 1 );
110 325899 : move16();
111 : }
112 :
113 : /*----------------------------------------------------------------*
114 : * Set pit_flag to 0 for every subframe with absolute pitch search
115 : *----------------------------------------------------------------*/
116 :
117 568660 : pit_flag = i_subfr;
118 568660 : move16();
119 568660 : if ( EQ_16( i_subfr, 2 * L_SUBFR ) )
120 : {
121 125584 : pit_flag = 0;
122 125584 : move16();
123 : }
124 :
125 : /*-----------------------------------------------------------------*
126 : * Limit range of pitch search
127 : * Fractional pitch search
128 : * Pitch quantization
129 : *-----------------------------------------------------------------*/
130 :
131 568660 : mult_Top = 1;
132 568660 : move16();
133 :
134 568660 : IF( !Opt_AMR_WB )
135 : {
136 : /*----------------------------------------------------------------*
137 : * pitch Q: Set limit_flag to 0 for restrained limits, and 1 for extended limits
138 : *----------------------------------------------------------------*/
139 568660 : test();
140 568660 : test();
141 568660 : IF( i_subfr == 0 )
142 : {
143 133890 : *limit_flag = 1;
144 133890 : move16();
145 133890 : if ( EQ_16( coder_type, VOICED ) )
146 : {
147 8837 : *limit_flag = 2;
148 8837 : move16(); /* double-extended limits */
149 : }
150 133890 : test();
151 133890 : if ( EQ_16( coder_type, GENERIC ) && EQ_32( core_brate, ACELP_7k20 ) )
152 : {
153 921 : *limit_flag = 0;
154 921 : move16();
155 : }
156 : }
157 434770 : 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 35819 : if ( GT_16( *T0, shr( add( PIT_FR1_EXTEND_8b, PIT_MIN ), 1 ) ) )
161 : {
162 27127 : *limit_flag = 0;
163 27127 : move16();
164 : }
165 : }
166 :
167 568660 : IF( *limit_flag == 0 )
168 : {
169 56369 : test();
170 56369 : test();
171 56369 : IF( i_subfr == 0 && LT_16( T_op[0], PIT_MIN ) )
172 : {
173 85 : mult_Top = 2;
174 85 : move16();
175 : }
176 56284 : ELSE IF( EQ_16( i_subfr, 2 * L_SUBFR ) && LT_16( T_op[1], PIT_MIN ) )
177 : {
178 852 : mult_Top = 2;
179 852 : move16();
180 : }
181 : }
182 :
183 : /*-------------------------------------------------------*
184 : * Retrieve the number of Q bits
185 : *-------------------------------------------------------*/
186 :
187 568660 : nBits = 0;
188 568660 : move16();
189 568660 : IF( NE_16( coder_type, AUDIO ) )
190 : {
191 545300 : nBits = pitch_bits[i_subfr >> L_sufr_sft];
192 545300 : move16();
193 : }
194 568660 : 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 23360 : delta = 4;
202 23360 : move16();
203 23360 : test();
204 23360 : test();
205 23360 : if ( EQ_16( L_subfr, shr( L_frame, 1 ) ) && i_subfr != 0 && EQ_16( L_frame, L_FRAME ) )
206 : {
207 3207 : pit_flag = L_SUBFR;
208 3207 : move16();
209 : }
210 23360 : IF( pit_flag == 0 )
211 : {
212 14491 : nBits = 10;
213 14491 : move16();
214 : }
215 : ELSE
216 : {
217 8869 : nBits = 6;
218 8869 : move16();
219 : }
220 :
221 : /* pitch lag search limitation */
222 23360 : test();
223 23360 : IF( i_subfr == 0 )
224 : {
225 11660 : limit_T0_fx( L_frame, delta, pit_flag, *limit_flag, mult_Top * T_op[0], 0, T0_min, T0_max );
226 : }
227 11700 : ELSE IF( EQ_16( i_subfr, 2 * L_SUBFR ) && pit_flag == 0 )
228 : {
229 2831 : 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 23360 : *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 23360 : move16();
235 :
236 23360 : pit_Q_enc_fx( hBstr, 0, nBits, delta, pit_flag, *limit_flag, *T0, *T0_frac, T0_min, T0_max );
237 : }
238 545300 : ELSE IF( EQ_16( coder_type, VOICED ) )
239 : {
240 : /*-------------------------------------------------------*
241 : * Pitch encoding in VOICED code type (ACELP@12k8 core only)
242 : *-------------------------------------------------------*/
243 :
244 35348 : delta = 4;
245 35348 : move16();
246 35348 : if ( EQ_16( i_subfr, 2 * L_SUBFR ) )
247 : {
248 8837 : pit_flag = i_subfr;
249 8837 : move16();
250 : }
251 :
252 : /* pitch lag search limitation */
253 35348 : IF( i_subfr == 0 )
254 : {
255 8837 : 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 35348 : test();
261 35348 : IF( EQ_16( nBits, 9 ) || EQ_16( nBits, 5 ) )
262 : {
263 26922 : *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 26922 : move16();
265 : }
266 8426 : ELSE IF( EQ_16( nBits, 10 ) )
267 : {
268 8426 : *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 8426 : move16();
270 : }
271 :
272 35348 : pit_Q_enc_fx( hBstr, 0, nBits, delta, pit_flag, *limit_flag, *T0, *T0_frac, T0_min, T0_max );
273 : }
274 509952 : 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 509812 : delta = 8;
336 509812 : move16();
337 :
338 : /* pitch lag search limitation */
339 509812 : IF( i_subfr == 0 )
340 : {
341 113358 : limit_T0_fx( L_frame, delta, pit_flag, *limit_flag, mult_Top * T_op[0], 0, T0_min, T0_max );
342 : }
343 396454 : ELSE IF( EQ_16( i_subfr, 2 * L_SUBFR ) )
344 : {
345 110674 : 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 509812 : IF( EQ_16( L_frame, L_FRAME ) )
350 : {
351 183892 : test();
352 183892 : test();
353 183892 : IF( EQ_16( nBits, 8 ) || EQ_16( nBits, 5 ) )
354 : {
355 10438 : IF( *limit_flag == 0 )
356 : {
357 6943 : *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 6943 : move16();
359 : }
360 : ELSE
361 : {
362 3495 : *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 3495 : move16();
364 : }
365 : }
366 173454 : ELSE IF( EQ_16( nBits, 9 ) || EQ_16( nBits, 6 ) )
367 : {
368 120272 : IF( *limit_flag == 0 )
369 : {
370 49356 : *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 49356 : move16();
372 : }
373 : ELSE
374 : {
375 70916 : *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 70916 : move16();
377 : }
378 : }
379 53182 : ELSE IF( EQ_16( nBits, 10 ) )
380 : {
381 53182 : *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 53182 : move16();
383 : }
384 :
385 183892 : 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 325920 : test();
390 325920 : IF( EQ_16( nBits, 9 ) || EQ_16( nBits, 6 ) )
391 : {
392 193944 : *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 193944 : move16();
394 : }
395 131976 : ELSE IF( EQ_16( nBits, 10 ) )
396 : {
397 131976 : *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 131976 : move16();
399 : }
400 :
401 325920 : 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 568660 : pitch_cl = shl( add( shl( *T0, 2 ), *T0_frac ), 4 ); /* save subframe pitch values Q6 */
501 :
502 568660 : 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 42851 : 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 42851 : Word16 pitch_index = 0;
524 :
525 42851 : IF( limit_flag == 0 )
526 : {
527 31762 : 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 3490 : IF( LT_16( T0, PIT_FR1_8b ) )
537 : {
538 : /*pitch_index = T0*2 + (T0_frac>>1) - (PIT_MIN*2);*/
539 1859 : 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 1631 : pitch_index = add( sub( T0, PIT_FR1_8b ), ( ( PIT_FR1_8b - PIT_MIN ) * 2 ) );
545 : }
546 : }
547 28272 : 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 28272 : IF( LT_16( T0, PIT_FR2_9b ) )
558 : {
559 : /*pitch_index = T0*4 + T0_frac - (PIT_MIN*4);*/
560 23006 : pitch_index = add( shl( T0, 2 ), sub( T0_frac, ( PIT_MIN * 4 ) ) );
561 : }
562 5266 : 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 2502 : 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 2764 : 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 11089 : ELSE IF( EQ_16( limit_flag, 1 ) ) /* extended Q range */
581 : {
582 10678 : 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 1000 : IF( LT_16( T0, PIT_FR1_EXTEND_8b ) )
592 : {
593 : /*pitch_index = T0*2 + (T0_frac>>1) - (PIT_MIN_EXTEND*2);*/
594 509 : 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 491 : pitch_index = add( sub( T0, PIT_FR1_EXTEND_8b ), ( ( PIT_FR1_EXTEND_8b - PIT_MIN_EXTEND ) * 2 ) );
600 : }
601 : }
602 9678 : 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 9678 : IF( LT_16( T0, PIT_FR2_EXTEND_9b ) )
613 : {
614 : /*pitch_index = T0*4 + T0_frac - (PIT_MIN_EXTEND*4);*/
615 8449 : pitch_index = add( shl( T0, 2 ), sub( T0_frac, ( PIT_MIN_EXTEND * 4 ) ) );
616 : }
617 1229 : 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 244 : 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 985 : 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 411 : 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 411 : 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 411 : IF( LT_16( T0, PIT_FR2_DOUBLEEXTEND_9b ) )
668 : {
669 : /*pitch_index = T0*4 + T0_frac - (PIT_MIN_DOUBLEEXTEND*4);*/
670 385 : 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 42851 : 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 148199 : 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 148199 : Word16 pitch_index = 0;
714 148199 : move16();
715 :
716 148199 : IF( fr_steps == 0 )
717 : {
718 195 : pitch_index = sub( T0, T0_min );
719 : }
720 148004 : ELSE IF( EQ_16( fr_steps, 2 ) )
721 : {
722 : /* pitch_index = (T0 - T0_min) * 2 + (T0_frac>>1);*/
723 9415 : pitch_index = add( shl( sub( T0, T0_min ), 1 ), shr( T0_frac, 1 ) );
724 : }
725 138589 : ELSE IF( EQ_16( fr_steps, 4 ) )
726 : {
727 : /*pitch_index = (T0 - T0_min) * 4 + T0_frac;*/
728 138589 : pitch_index = add( shl( sub( T0, T0_min ), 2 ), T0_frac );
729 : }
730 :
731 148199 : 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 610636 : 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 610636 : IF( limit_flag == 0 )
768 : {
769 82239 : IF( EQ_16( L_frame, L_FRAME ) )
770 : {
771 75572 : pit_min = PIT_MIN;
772 75572 : move16();
773 : }
774 : ELSE /* L_frame == L_FRAME16k */
775 : {
776 6667 : pit_min = PIT16k_MIN;
777 6667 : move16();
778 : }
779 : }
780 : ELSE
781 : {
782 528397 : IF( EQ_16( L_frame, L_FRAME ) )
783 : {
784 186301 : pit_min = PIT_MIN_EXTEND;
785 186301 : move16();
786 186301 : IF( EQ_16( limit_flag, 2 ) )
787 : {
788 35348 : pit_min = PIT_MIN_DOUBLEEXTEND;
789 35348 : move16();
790 : }
791 : }
792 : ELSE /* L_frame == L_FRAME16k */
793 : {
794 342096 : pit_min = PIT16k_MIN_EXTEND;
795 342096 : 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 610636 : t_min = sub( t0_min, L_INTERPOL1 );
807 610636 : t_max = add( t0_max, L_INTERPOL1 );
808 610636 : corr = &corr_v[-t_min];
809 : /* corr[t_min..t_max] */
810 610636 : if ( element_mode > EVS_MONO )
811 : {
812 562691 : norm_corr_ivas_fx( exc, xn, h, t_min, t_max, corr, L_subfr, Q_new );
813 : }
814 : else
815 : {
816 47945 : norm_corr_fx( exc, xn, h, t_min, t_max, corr, L_subfr );
817 : }
818 :
819 : /*-----------------------------------------------------------------*
820 : * Find integer pitch
821 : *-----------------------------------------------------------------*/
822 :
823 610636 : max_val = corr[t0_min];
824 610636 : move16();
825 610636 : t0 = t0_min;
826 610636 : move16();
827 :
828 9300512 : FOR( i = t0_min + 1; i <= t0_max; i++ )
829 : {
830 8689876 : if ( GE_16( corr[i], max_val ) )
831 : {
832 3406177 : t0 = i;
833 3406177 : move16();
834 : }
835 8689876 : max_val = s_max( corr[i], max_val );
836 : }
837 :
838 610636 : IF( EQ_16( t0_fr1, pit_min ) )
839 : {
840 : /* don't search fraction (for 7b/4b quant) */
841 567 : test();
842 567 : 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 567 : *pit_frac = 0;
860 567 : move16();
861 :
862 567 : return ( t0 );
863 : }
864 :
865 610069 : test();
866 610069 : IF( ( i_subfr == 0 ) && ( GE_16( t0, t0_fr1 ) ) )
867 : {
868 11476 : *pit_frac = 0;
869 11476 : move16();
870 :
871 11476 : 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 598593 : t1 = t0;
881 598593 : move16();
882 598593 : step = 1;
883 598593 : move16(); /* 1/4 subsample resolution */
884 598593 : fraction = 1;
885 598593 : move16();
886 598593 : test();
887 598593 : test();
888 598593 : IF( ( ( i_subfr == 0 ) && ( GE_16( t0, t0_fr2 ) ) ) || ( EQ_16( t0_fr2, pit_min ) ) )
889 : {
890 21723 : step = 2;
891 21723 : move16(); /* 1/2 subsample resolution */
892 21723 : fraction = 2;
893 21723 : move16();
894 : }
895 :
896 598593 : IF( EQ_16( t0, t0_min ) ) /* Limit case */
897 : {
898 37049 : fraction = 0;
899 37049 : move16();
900 37049 : cor_max = Interpol_4( &corr[t0], fraction );
901 : }
902 : ELSE
903 : {
904 561544 : t0 = sub( t0, 1 );
905 561544 : cor_max = Interpol_4( &corr[t0], fraction );
906 1644806 : FOR( i = fraction + step; i <= 3; i = ( i + step ) )
907 : {
908 1083262 : temp = Interpol_4( &corr[t0], i );
909 1083262 : IF( GT_16( temp, cor_max ) )
910 : {
911 1010160 : cor_max = temp;
912 1010160 : move16();
913 1010160 : fraction = i;
914 1010160 : move16();
915 : }
916 : }
917 : }
918 :
919 2949519 : FOR( i = 0; i <= 3; i = ( i + step ) )
920 : {
921 2350926 : temp = Interpol_4( &corr[t1], i );
922 2350926 : IF( GT_16( temp, cor_max ) )
923 : {
924 697634 : cor_max = temp;
925 697634 : move16();
926 697634 : fraction = i;
927 697634 : move16();
928 697634 : t0 = t1;
929 697634 : move16();
930 : }
931 : }
932 :
933 598593 : *pit_frac = fraction;
934 598593 : move16();
935 :
936 598593 : 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 562691 : 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 :
968 562691 : k = negate( t_min );
969 562691 : h_e = add( 1, norm_s( h[0] ) ); // exponent of h
970 :
971 : /*-----------------------------------------------------------------*
972 : * compute the filtered excitation for the first delay t_min
973 : *-----------------------------------------------------------------*/
974 :
975 562691 : conv_fx( &exc[k], h, excf, L_subfr ); // Q_new - h_e
976 :
977 : /*----------------------------------------------------------------*
978 : * loop for every possible period
979 : *----------------------------------------------------------------*/
980 :
981 13604523 : FOR( t = t_min; t <= t_max; t++ )
982 : {
983 : /* Compute correlation between xn[] and excf[] */
984 13041832 : L_tmp = L_mac( 0, xn[0], excf[0] );
985 858499584 : FOR( i = 1; i < L_subfr; i++ )
986 : {
987 845457752 : L_tmp = L_mac_sat( L_tmp, xn[i], excf[i] ); // (Q_new - 1) + (Q_new - h_e) + 1
988 : }
989 13041832 : exp = norm_l( L_tmp );
990 13041832 : L_tmp = L_shl( L_tmp, exp );
991 13041832 : exp_corr = sub( 31, add( exp, ( Q_new - 1 ) + ( Q_new - h_e ) + 1 ) );
992 13041832 : corr = extract_h( L_tmp );
993 :
994 : /* Compute 1/sqrt(energy of excf[]) */
995 13041832 : Word16 _0_01 = extract_l( L_shr( 21474836 /* 0.01 in Q31 */, sub( 31, add( shl( sub( Q_new, h_e ), 1 ), 1 ) ) ) );
996 :
997 13041832 : W_tmp = W_mac_16_16( _0_01, excf[0], excf[0] );
998 858499584 : FOR( i = 1; i < L_subfr; i++ )
999 : {
1000 845457752 : W_tmp = W_mac_16_16( W_tmp, excf[i], excf[i] ); // 2*(Q_new - h_e) + 1
1001 : }
1002 :
1003 13041832 : exp = W_norm( W_tmp );
1004 13041832 : L_tmp = W_extract_h( W_shl( W_tmp, exp ) ); // 2*(Q_new - h_e) + 1 + exp - 32
1005 13041832 : exp_norm = sub( 31 + 32, add( add( shl( sub( Q_new, h_e ), 1 ), 1 ), exp ) );
1006 :
1007 13041832 : L_tmp = Isqrt_lc( L_tmp, &exp_norm );
1008 13041832 : norm = extract_h( L_tmp );
1009 :
1010 : /* Normalize correlation = correlation * (1/sqrt(energy)) */
1011 13041832 : L_tmp = L_mult( corr, norm );
1012 :
1013 13041832 : ncorr[t] = round_fx_sat( L_tmp );
1014 13041832 : ncorr_e[t - t_min] = add( exp_corr, exp_norm );
1015 13041832 : move16();
1016 13041832 : move16();
1017 :
1018 : /* update the filtered excitation excf[] for the next iteration */
1019 13041832 : IF( NE_16( t, t_max ) )
1020 : {
1021 12479141 : k--;
1022 820998464 : FOR( i = (Word16) ( L_subfr - 1 ); i > 0; i-- )
1023 : {
1024 : /* saturation can occur in add() */
1025 808519323 : excf[i] = round_fx_sat( L_mac_sat( L_mult( excf[i - 1], 32767 ), exc[k], h[i] ) );
1026 808519323 : move16();
1027 : }
1028 12479141 : excf[0] = mult_r( exc[k], h[0] );
1029 12479141 : move16();
1030 : }
1031 : }
1032 : // Aligning the values of ncorr to a common exponent
1033 562691 : maximum_fx( ncorr_e, t_max + 1 - t_min, &e_max );
1034 13604523 : FOR( t = t_min; t <= t_max; t++ )
1035 : {
1036 13041832 : ncorr[t] = shr( ncorr[t], sub( e_max, ncorr_e[t - t_min] ) );
1037 13041832 : move16();
1038 : }
1039 :
1040 562691 : return;
1041 : }
1042 :
1043 :
1044 47945 : void norm_corr_fx(
1045 : const Word16 exc[], /* i : excitation buffer Q_new*/
1046 : const Word16 xn[], /* i : target signal Q_new-1+shift*/
1047 : const Word16 h[], /* i : weighted synthesis filter impulse response Q(14+shift)*/
1048 : const Word16 t_min, /* i : minimum value of searched range */
1049 : const Word16 t_max, /* i : maximum value of searched range */
1050 : Word16 ncorr[], /* o : normalized correlation Q15 */
1051 : const Word16 L_subfr /* i : subframe size */
1052 : )
1053 : {
1054 : Word16 i, k, t;
1055 : Word16 corr, exp_corr, norm, exp_norm, exp, scale;
1056 : Word16 excf[L_FRAME16k];
1057 : Word32 L_tmp;
1058 :
1059 47945 : k = negate( t_min );
1060 :
1061 : /*-----------------------------------------------------------------*
1062 : * compute the filtered excitation for the first delay t_min
1063 : *-----------------------------------------------------------------*/
1064 :
1065 47945 : conv_fx( &exc[k], h, excf, L_subfr );
1066 :
1067 : /* Compute rounded down 1/sqrt(energy of xn[]) */
1068 47945 : L_tmp = L_mac_sat( 1, xn[0], xn[0] );
1069 3069504 : FOR( i = 1; i < L_subfr; i++ )
1070 : {
1071 3021559 : L_tmp = L_mac_sat( L_tmp, xn[i], xn[i] );
1072 : }
1073 47945 : exp = norm_l( L_tmp );
1074 47945 : exp = sub( 30, exp );
1075 :
1076 47945 : exp = add( exp, 2 ); /* energy of xn[] x 2 + rounded up */
1077 47945 : scale = negate( shr( exp, 1 ) ); /* (1<<scale) < 1/sqrt(energy rounded) */
1078 :
1079 : /*----------------------------------------------------------------*
1080 : * loop for every possible period
1081 : *----------------------------------------------------------------*/
1082 :
1083 1191713 : FOR( t = t_min; t <= t_max; t++ )
1084 : {
1085 : /* Compute correlation between xn[] and excf[] */
1086 1143768 : L_tmp = L_mac( 1, xn[0], excf[0] );
1087 73217536 : FOR( i = 1; i < L_subfr; i++ )
1088 : {
1089 72073768 : L_tmp = L_mac_sat( L_tmp, xn[i], excf[i] );
1090 : }
1091 1143768 : exp = norm_l( L_tmp );
1092 1143768 : L_tmp = L_shl( L_tmp, exp );
1093 1143768 : exp_corr = sub( 30, exp );
1094 1143768 : corr = extract_h( L_tmp );
1095 :
1096 : /* Compute 1/sqrt(energy of excf[]) */
1097 1143768 : L_tmp = L_mac( 1, excf[0], excf[0] );
1098 73217536 : FOR( i = 1; i < L_subfr; i++ )
1099 : {
1100 72073768 : L_tmp = L_mac_sat( L_tmp, excf[i], excf[i] );
1101 : }
1102 :
1103 1143768 : exp = norm_l( L_tmp );
1104 1143768 : L_tmp = L_shl( L_tmp, exp );
1105 1143768 : exp_norm = sub( 30, exp );
1106 :
1107 1143768 : L_tmp = Isqrt_lc( L_tmp, &exp_norm );
1108 1143768 : norm = extract_h( L_tmp );
1109 :
1110 : /* Normalize correlation = correlation * (1/sqrt(energy)) */
1111 1143768 : L_tmp = L_mult( corr, norm );
1112 1143768 : L_tmp = L_shl_sat( L_tmp, add( add( exp_corr, exp_norm ), scale ) );
1113 1143768 : ncorr[t] = round_fx_sat( L_tmp );
1114 :
1115 : /* update the filtered excitation excf[] for the next iteration */
1116 1143768 : IF( NE_16( t, t_max ) )
1117 : {
1118 1095823 : k--;
1119 70148032 : FOR( i = ( L_subfr - 1 ); i > 0; i-- )
1120 : {
1121 : /* saturation can occur in add() */
1122 : /*excf[i] = add(mult(exc[k], h[i]), excf[i - 1]); move16(); */
1123 69052209 : excf[i] = round_fx_sat( L_mac_sat( L_mult( excf[i - 1], 32767 ), exc[k], h[i] ) );
1124 69052209 : move16();
1125 : }
1126 1095823 : excf[0] = mult_r( exc[k], h[0] );
1127 1095823 : move16();
1128 : }
1129 : }
1130 :
1131 47945 : return;
1132 : }
1133 :
1134 :
1135 : /*-------------------------------------------------------------------*
1136 : * pit_Q_enc()
1137 : *
1138 : * Encode subframe pitch lag
1139 : *-------------------------------------------------------------------*/
1140 :
1141 253388 : void pit_Q_enc_fx(
1142 : BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle */
1143 : const Word16 Opt_AMR_WB, /* i : flag indicating AMR-WB IO mode */
1144 : const Word16 nBits, /* i : # of Q bits */
1145 : const Word16 delta, /* i : Half the CL searched interval */
1146 : const Word16 pit_flag, /* i : absolute(0) or delta(1) pitch Q */
1147 : const Word16 limit_flag, /* i : restrained(0) or extended(1) Q limits */
1148 : const Word16 T0, /* i : integer pitch lag */
1149 : const Word16 T0_frac, /* i : pitch fraction */
1150 : Word16 *T0_min, /* i/o: delta search min */
1151 : Word16 *T0_max /* o : delta search max_val */
1152 : )
1153 : {
1154 : Word16 pitch_index;
1155 :
1156 253388 : IF( EQ_16( nBits, 10 ) ) /* absolute encoding with 10 bits */
1157 : {
1158 76099 : IF( limit_flag == 0 )
1159 : {
1160 : /* pitch_index = T0*4 + T0_frac - (PIT_MIN*4);*/
1161 0 : pitch_index = sub( add( shl( T0, 2 ), T0_frac ), ( PIT_MIN * 4 ) );
1162 : }
1163 76099 : ELSE IF( EQ_16( limit_flag, 1 ) )
1164 : {
1165 : /*pitch_index = T0*4 + T0_frac - (PIT_MIN_EXTEND*4);*/
1166 67673 : pitch_index = sub( add( shl( T0, 2 ), T0_frac ), ( PIT_MIN_EXTEND * 4 ) );
1167 : }
1168 : ELSE /* limit_flag == 2 */
1169 : {
1170 : /*pitch_index = T0*4 + T0_frac - (PIT_MIN_DOUBLEEXTEND*4);*/
1171 8426 : pitch_index = sub( add( shl( T0, 2 ), T0_frac ), ( PIT_MIN_DOUBLEEXTEND * 4 ) );
1172 : }
1173 : }
1174 177289 : ELSE IF( EQ_16( nBits, 9 ) ) /* absolute encoding with 9 bits */
1175 : {
1176 36540 : pitch_index = abs_pit_enc_fx( 4, limit_flag, T0, T0_frac );
1177 :
1178 : /* find T0_min and T0_max for delta search */
1179 36540 : IF( Opt_AMR_WB )
1180 : {
1181 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 */
1182 : }
1183 : }
1184 140749 : ELSE IF( EQ_16( nBits, 8 ) ) /* absolute encoding with 8 bits */
1185 : {
1186 4474 : pitch_index = abs_pit_enc_fx( 2, limit_flag, T0, T0_frac );
1187 :
1188 : /* find T0_min and T0_max for delta search */
1189 4474 : IF( Opt_AMR_WB )
1190 : {
1191 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 */
1192 : }
1193 : }
1194 136275 : ELSE IF( EQ_16( nBits, 6 ) ) /* relative encoding with 6 bits */
1195 : {
1196 102761 : pitch_index = delta_pit_enc_fx( 4, T0, T0_frac, *T0_min );
1197 : }
1198 33514 : ELSE IF( EQ_16( nBits, 5 ) ) /* relative encoding with 5 bits */
1199 : {
1200 33514 : IF( EQ_16( delta, 8 ) )
1201 : {
1202 7003 : pitch_index = delta_pit_enc_fx( 2, T0, T0_frac, *T0_min );
1203 : }
1204 : ELSE /* delta == 4 */
1205 : {
1206 26511 : pitch_index = delta_pit_enc_fx( 4, T0, T0_frac, *T0_min );
1207 : }
1208 : }
1209 : ELSE /* nBits == 4 ) */ /* relative encoding with 4 bits */
1210 : {
1211 0 : IF( EQ_16( delta, 8 ) )
1212 : {
1213 0 : pitch_index = delta_pit_enc_fx( 0, T0, T0_frac, *T0_min );
1214 : }
1215 : ELSE /* delta == 4 */
1216 : {
1217 0 : pitch_index = delta_pit_enc_fx( 2, T0, T0_frac, *T0_min );
1218 : }
1219 : }
1220 :
1221 253388 : IF( !Opt_AMR_WB )
1222 : {
1223 : /* find T0_min and T0_max for delta search */
1224 253388 : limit_T0_fx( L_FRAME, delta, L_SUBFR, limit_flag, T0, T0_frac, T0_min, T0_max );
1225 : }
1226 :
1227 : {
1228 253388 : push_indice( hBstr, IND_PITCH, pitch_index, nBits );
1229 : }
1230 :
1231 253388 : return;
1232 : }
1233 :
1234 :
1235 : /*-------------------------------------------------------------------*
1236 : * pit16k_Q_enc()
1237 : *
1238 : * Encode subframe pitch lag @16kHz core
1239 : *-------------------------------------------------------------------*/
1240 :
1241 332779 : void pit16k_Q_enc_fx(
1242 : BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle */
1243 : const Word16 nBits, /* i : # of Q bits */
1244 : const Word16 limit_flag, /* i : restrained(0) or extended(1) Q limits */
1245 : const Word16 T0, /* i : integer pitch lag */
1246 : const Word16 T0_frac, /* i : pitch fraction */
1247 : Word16 *T0_min, /* i/o: delta search min */
1248 : Word16 *T0_max /* o : delta search max_val */
1249 : )
1250 : {
1251 : Word16 pitch_index;
1252 :
1253 332779 : IF( EQ_16( nBits, 10 ) ) /* absolute encoding with 10 bits */
1254 : {
1255 : {
1256 138835 : IF( LT_16( T0, PIT16k_FR2_EXTEND_10b ) )
1257 : {
1258 : /*pitch_index = T0*4 + T0_frac - (PIT16k_MIN_EXTEND*4);*/
1259 135270 : pitch_index = add( shl( T0, 2 ), sub( T0_frac, ( PIT16k_MIN_EXTEND * 4 ) ) );
1260 : }
1261 : ELSE
1262 : {
1263 : /*pitch_index = T0*2 + (T0_frac>>1) - (PIT16k_FR2_EXTEND_10b*2) + ((PIT16k_FR2_EXTEND_10b-PIT16k_MIN_EXTEND)*4);*/
1264 3565 : 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 ) );
1265 : }
1266 : }
1267 :
1268 138835 : push_indice( hBstr, IND_PITCH, pitch_index, nBits );
1269 : }
1270 193944 : ELSE IF( EQ_16( nBits, 9 ) ) /* absolute encoding with 9 bits */
1271 : {
1272 : {
1273 : /*-------------------------------------------------------------------*
1274 : * The pitch range is encoded absolutely with 9 bits
1275 : * and is divided as follows:
1276 : * PIT16k_EXTEND_MIN to PIT16k_FR2_EXTEND_9b-1 resolution 1/4 (frac = 0,1,2 or 3)
1277 : * PIT16k_FR2_EXTEND_9b to PIT16k_FR1_EXTEND_9b-1 resolution 1/2 (frac = 0 or 2)
1278 : * PIT16k_FR1_EXTEND_9b to PIT16k_MAX_EXTEND resolution 1 (frac = 0)
1279 : *-------------------------------------------------------------------*/
1280 :
1281 0 : IF( LT_16( T0, PIT16k_FR2_EXTEND_9b ) )
1282 : {
1283 : /*pitch_index = T0*4 + T0_frac - (PIT16k_MIN_EXTEND*4);*/
1284 0 : pitch_index = add( shl( T0, 2 ), sub( T0_frac, ( PIT16k_MIN_EXTEND * 4 ) ) );
1285 : }
1286 0 : ELSE IF( LT_16( T0, PIT16k_FR1_EXTEND_9b ) )
1287 : {
1288 : /*pitch_index = T0*2 + (T0_frac>>1) - (PIT16k_FR2_EXTEND_9b*2) + ((PIT16k_FR2_EXTEND_9b-PIT16k_MIN_EXTEND)*4);*/
1289 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 ) );
1290 : }
1291 : ELSE
1292 : {
1293 : /*pitch_index = T0 - PIT16k_FR1_EXTEND_9b + ((PIT16k_FR2_EXTEND_9b-PIT16k_MIN_EXTEND)*4) + ((PIT16k_FR1_EXTEND_9b-PIT16k_FR2_EXTEND_9b)*2);*/
1294 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 ) );
1295 : }
1296 : }
1297 :
1298 0 : push_indice( hBstr, IND_PITCH, pitch_index, 9 );
1299 : }
1300 : ELSE /* nBits == 6 */ /* relative encoding with 6 bits */
1301 : {
1302 : /*pitch_index = (T0 - *T0_min) * 4 + T0_frac;*/
1303 193944 : pitch_index = add( shl( sub( T0, *T0_min ), 2 ), T0_frac );
1304 :
1305 193944 : push_indice( hBstr, IND_PITCH, pitch_index, nBits );
1306 : }
1307 :
1308 332779 : limit_T0_fx( L_FRAME16k, 8, L_SUBFR, limit_flag, T0, T0_frac, T0_min, T0_max );
1309 :
1310 332779 : return;
1311 : }
1312 :
1313 :
1314 : /*------------------------------------------------------------------*
1315 : * pit_encode:
1316 : *
1317 : * Close-loop pitch lag search and pitch lag quantization
1318 : * Adaptive excitation construction
1319 : *------------------------------------------------------------------*/
1320 :
1321 3060 : void Mode2_pit_encode_fx(
1322 : const Word16 coder_type, /* i : coding model */
1323 : const Word16 i_subfr, /* i : subframe index */
1324 : Word16 **pt_indice, /* i/o: quantization indices pointer */
1325 : Word16 *exc, /* i/o: pointer to excitation signal frame */
1326 : const Word16 *T_op, /* i : open loop pitch estimates in current frame */
1327 : Word16 *T0_min, /* i/o: lower limit for close-loop search */
1328 : Word16 *T0_min_frac, /* i/o: lower limit for close-loop search */
1329 : Word16 *T0_max, /* i/o: higher limit for close-loop search */
1330 : Word16 *T0_max_frac, /* i/o: higher limit for close-loop search */
1331 : Word16 *T0, /* i/o: close loop integer pitch */
1332 : Word16 *T0_frac, /* i/o: close loop fractional part of the pitch */
1333 : Word16 *T0_res, /* i/o: close loop pitch resolution */
1334 : Word16 *h1, /* i : weighted filter impulse response 1Q14+shift*/
1335 : Word16 *xn, /* i : target vector */
1336 : Word16 pit_min,
1337 : Word16 pit_fr1,
1338 : Word16 pit_fr1b,
1339 : Word16 pit_fr2,
1340 : Word16 pit_max,
1341 : Word16 pit_res_max )
1342 : {
1343 : Word16 pit_flag;
1344 :
1345 : BASOP_SATURATE_ERROR_ON_EVS;
1346 :
1347 : /* Pitch flag */
1348 3060 : pit_flag = i_subfr;
1349 3060 : move16();
1350 3060 : if ( EQ_16( i_subfr, ( 2 * L_SUBFR ) ) )
1351 : {
1352 612 : pit_flag = 0;
1353 612 : move16();
1354 : }
1355 :
1356 : /*-----------------------------------------------------------------*
1357 : * - Limit range of pitch search
1358 : * - Fractional pitch search
1359 : * - Pitch quantization
1360 : *-----------------------------------------------------------------*/
1361 :
1362 3060 : IF( coder_type == 0 ) /*Unvoiced Coding do nothing*/
1363 : {
1364 0 : *T0 = L_SUBFR;
1365 0 : move16();
1366 0 : *T0_frac = 0;
1367 0 : move16();
1368 0 : *T0_res = 1;
1369 0 : move16();
1370 : }
1371 3060 : ELSE IF( EQ_16( coder_type, 1 ) ) /* 8/4/4/4 (EVS) */
1372 : {
1373 0 : IF( i_subfr == 0 )
1374 : {
1375 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 );
1376 : }
1377 : ELSE
1378 : {
1379 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 );
1380 : }
1381 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,
1382 : i_subfr, pit_min, pit_min, pit_fr1b, L_SUBFR );
1383 0 : move16();
1384 :
1385 0 : IF( i_subfr == 0 )
1386 : {
1387 0 : Mode2_abs_pit_enc_fx( *T0, *T0_frac, pt_indice, pit_min, pit_fr1b, pit_min, pit_res_max );
1388 : }
1389 : ELSE
1390 : {
1391 0 : Mode2_delta_pit_enc_fx( *T0, *T0_frac, shr( pit_res_max, 1 ), *T0_min, *T0_min_frac, pt_indice );
1392 : }
1393 : }
1394 3060 : ELSE IF( EQ_16( coder_type, 2 ) ) /* 8/5/8/5 (EVS) */
1395 : {
1396 0 : IF( i_subfr == 0 )
1397 : {
1398 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 );
1399 : }
1400 0 : ELSE IF( i_subfr == 2 * L_SUBFR )
1401 : {
1402 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 );
1403 : }
1404 : ELSE
1405 : {
1406 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 );
1407 : }
1408 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,
1409 : pit_flag, pit_min, pit_min, pit_fr1b, L_SUBFR );
1410 0 : move16();
1411 :
1412 0 : IF( pit_flag == 0 )
1413 : {
1414 0 : Mode2_abs_pit_enc_fx( *T0, *T0_frac, pt_indice, pit_min, pit_fr1b, pit_min, pit_res_max );
1415 : }
1416 : ELSE
1417 : {
1418 0 : Mode2_delta_pit_enc_fx( *T0, *T0_frac, shr( pit_res_max, 1 ), *T0_min, *T0_min_frac, pt_indice );
1419 : }
1420 : }
1421 3060 : ELSE IF( EQ_16( coder_type, 3 ) ) /* 9/6/6/6 (HRs- VC) */
1422 : {
1423 1350 : Word16 pit_res_max2 = pit_res_max;
1424 :
1425 1350 : IF( EQ_16( pit_min, PIT_MIN_16k ) )
1426 : {
1427 1350 : pit_res_max2 = shr( pit_res_max, 1 );
1428 : }
1429 :
1430 1350 : IF( ( i_subfr == 0 ) )
1431 : {
1432 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 );
1433 : }
1434 : ELSE
1435 : {
1436 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 );
1437 : }
1438 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,
1439 : i_subfr, pit_min, pit_fr2, pit_fr1, L_SUBFR );
1440 1350 : move16();
1441 :
1442 1350 : IF( i_subfr == 0 ) /* if 1st subframe */
1443 : {
1444 270 : Mode2_abs_pit_enc_fx( *T0, *T0_frac, pt_indice, pit_min, pit_fr1, pit_fr2, pit_res_max );
1445 : }
1446 : ELSE
1447 : {
1448 1080 : Mode2_delta_pit_enc_fx( *T0, *T0_frac, pit_res_max2, *T0_min, *T0_min_frac, pt_indice );
1449 : }
1450 : }
1451 1710 : ELSE IF( coder_type == 4 ) /* 9/6/9/6 (AMRWB) */
1452 : {
1453 1710 : Word16 pit_res_max2 = pit_res_max;
1454 1710 : IF( EQ_16( pit_min, PIT_MIN_16k ) )
1455 : {
1456 1710 : pit_res_max2 = shr( pit_res_max, 1 );
1457 : }
1458 1710 : test();
1459 1710 : IF( ( i_subfr == 0 ) || EQ_16( i_subfr, shl( L_SUBFR, 1 ) ) )
1460 : {
1461 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 );
1462 : }
1463 : ELSE
1464 : {
1465 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 );
1466 : }
1467 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,
1468 : pit_flag, pit_min, pit_fr2, pit_fr1, L_SUBFR );
1469 1710 : move16();
1470 1710 : IF( pit_flag == 0 ) /* if 1st/3rd/5th subframe */
1471 : {
1472 684 : Mode2_abs_pit_enc_fx( *T0, *T0_frac, pt_indice, pit_min, pit_fr1, pit_fr2, pit_res_max );
1473 : }
1474 : ELSE /* if subframe 2 or 4 */
1475 : {
1476 1026 : Mode2_delta_pit_enc_fx( *T0, *T0_frac, pit_res_max2, *T0_min, *T0_min_frac, pt_indice );
1477 : }
1478 : }
1479 0 : ELSE IF( EQ_16( coder_type, 8 ) ) /* 8/5/5/5 (RF all pred mode) */
1480 : {
1481 0 : IF( i_subfr == 0 )
1482 : {
1483 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 );
1484 : }
1485 : ELSE
1486 : {
1487 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 );
1488 : }
1489 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,
1490 : i_subfr, pit_min, pit_min, pit_fr1b, L_SUBFR );
1491 0 : move16();
1492 :
1493 0 : IF( i_subfr == 0 )
1494 : {
1495 0 : Mode2_abs_pit_enc_fx( *T0, *T0_frac, pt_indice, pit_min, pit_fr1b, pit_min, pit_res_max );
1496 : }
1497 : ELSE
1498 : {
1499 0 : Mode2_delta_pit_enc_fx( *T0, *T0_frac, shr( pit_res_max, 1 ), *T0_min, *T0_min_frac, pt_indice );
1500 : }
1501 : }
1502 0 : ELSE IF( EQ_16( coder_type, 9 ) ) /* 8/0/8/0 (RF mode Gen pred) */
1503 : {
1504 0 : IF( i_subfr == 0 )
1505 : {
1506 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 );
1507 : }
1508 : ELSE
1509 : {
1510 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 );
1511 : }
1512 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,
1513 : i_subfr, pit_min, pit_min, pit_fr1b, L_SUBFR );
1514 0 : move16();
1515 :
1516 0 : IF( i_subfr == 0 )
1517 : {
1518 0 : Mode2_abs_pit_enc_fx( *T0, *T0_frac, pt_indice, pit_min, pit_fr1b, pit_min, pit_res_max );
1519 : }
1520 : ELSE
1521 : {
1522 0 : Mode2_delta_pit_enc_fx( *T0, *T0_frac, shr( pit_res_max, 1 ), *T0_min, *T0_min_frac, pt_indice );
1523 : }
1524 : }
1525 :
1526 : BASOP_SATURATE_ERROR_OFF_EVS;
1527 :
1528 3060 : return;
1529 : }
1530 :
1531 :
1532 954 : static void limit_T0_voiced2(
1533 : Word16 res,
1534 : const Word16 *T_op,
1535 : Word16 *T0_min,
1536 : Word16 *T0_min_frac,
1537 : Word16 *T0_max,
1538 : Word16 *T0_max_frac,
1539 : Word16 pit_min,
1540 : Word16 pit_max,
1541 : Word16 i_subfr )
1542 : {
1543 : Word16 t, temp1, temp2, res2;
1544 :
1545 954 : assert( res > 1 && res <= 6 );
1546 :
1547 954 : res2 = res;
1548 954 : move16();
1549 954 : if ( EQ_16( res, 6 ) )
1550 : {
1551 0 : res2 = shr( res2, 1 );
1552 : }
1553 :
1554 : /* Lower-bound */
1555 954 : IF( i_subfr == 0 )
1556 : {
1557 612 : temp1 = sub( i_mult2( T_op[0], res ), 32 );
1558 : }
1559 : ELSE
1560 : {
1561 342 : temp1 = sub( i_mult2( T_op[1], res ), 32 );
1562 : }
1563 :
1564 954 : IF( LT_16( T_op[0], T_op[1] ) )
1565 : {
1566 264 : t = sub( i_mult2( T_op[0], res ), 16 );
1567 : }
1568 : ELSE
1569 : {
1570 690 : t = sub( i_mult2( T_op[1], res ), 16 );
1571 : }
1572 :
1573 954 : if ( LT_16( temp1, t ) )
1574 : {
1575 840 : temp1 = t;
1576 840 : move16();
1577 : }
1578 :
1579 954 : temp2 = mult( temp1, inv_T0_res[res2] );
1580 954 : if ( EQ_16( res, 6 ) )
1581 : {
1582 0 : temp2 = shr( temp2, 1 );
1583 : }
1584 :
1585 954 : *T0_min = temp2;
1586 954 : move16();
1587 :
1588 954 : *T0_min_frac = sub( temp1, i_mult2( temp2, res ) );
1589 954 : move16();
1590 :
1591 954 : IF( LT_16( *T0_min, pit_min ) )
1592 : {
1593 14 : *T0_min = pit_min;
1594 14 : move16();
1595 14 : *T0_min_frac = 0;
1596 14 : move16();
1597 : }
1598 :
1599 : /* Higher-bound */
1600 954 : temp1 = add( i_mult2( *T0_min, res ), add( *T0_min_frac, 63 ) );
1601 :
1602 954 : IF( T_op[0] < T_op[1] )
1603 : {
1604 264 : t = add( i_mult2( T_op[1], res ), add( 15, res ) );
1605 : }
1606 : ELSE
1607 : {
1608 690 : t = add( i_mult2( T_op[0], res ), add( 15, res ) );
1609 : }
1610 :
1611 954 : if ( GT_16( temp1, t ) )
1612 : {
1613 860 : temp1 = t;
1614 860 : move16();
1615 : }
1616 :
1617 954 : temp2 = mult( temp1, inv_T0_res[res] );
1618 :
1619 954 : *T0_max = temp2;
1620 954 : move16();
1621 :
1622 954 : *T0_max_frac = sub( temp1, i_mult2( temp2, res ) );
1623 954 : move16();
1624 :
1625 954 : IF( GT_16( *T0_max, pit_max ) )
1626 : {
1627 8 : *T0_max = pit_max;
1628 8 : *T0_max_frac = sub( res, 1 );
1629 8 : move16();
1630 8 : move16();
1631 :
1632 8 : temp1 = add( sub( i_mult2( *T0_max, res ), 64 ), res );
1633 :
1634 8 : temp2 = mult( temp1, inv_T0_res[res2] );
1635 8 : IF( EQ_16( res, 6 ) )
1636 : {
1637 0 : temp2 = shr( temp2, 1 );
1638 : }
1639 :
1640 8 : *T0_min = temp2;
1641 8 : move16();
1642 :
1643 8 : *T0_min_frac = sub( temp1, i_mult2( temp2, res ) );
1644 8 : move16();
1645 : }
1646 :
1647 954 : return;
1648 : }
1649 :
1650 :
1651 : /*-------------------------------------------------------------------*
1652 : * abs_pit_enc:
1653 : *
1654 : * Encode pitch lag absolutely
1655 : *-------------------------------------------------------------------*/
1656 :
1657 954 : void Mode2_abs_pit_enc_fx(
1658 : Word16 T0, /* i : integer pitch lag */
1659 : Word16 T0_frac, /* i : pitch fraction */
1660 : Word16 **pt_indice, /* i/o: pointer to Vector of Q indexes */
1661 : Word16 pit_min,
1662 : Word16 pit_fr1,
1663 : Word16 pit_fr2,
1664 : Word16 pit_res_max )
1665 : {
1666 : Word16 pit_res_max_half;
1667 :
1668 :
1669 954 : pit_res_max_half = shr( pit_res_max, 1 );
1670 :
1671 954 : IF( LT_16( T0, pit_fr2 ) )
1672 : {
1673 0 : **pt_indice = add( i_mult2( T0, pit_res_max ), sub( T0_frac, i_mult2( pit_min, pit_res_max ) ) );
1674 0 : move16();
1675 : }
1676 954 : ELSE IF( LT_16( T0, pit_fr1 ) )
1677 : {
1678 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 ) );
1679 842 : move16();
1680 : }
1681 : ELSE
1682 : {
1683 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 ) );
1684 112 : move16();
1685 : }
1686 :
1687 954 : ( *pt_indice )++;
1688 :
1689 954 : return;
1690 : }
1691 :
1692 :
1693 : /*-------------------------------------------------------------------*
1694 : * delta_pit_enc:
1695 : *
1696 : * Encode pitch lag differentially
1697 : *-------------------------------------------------------------------*/
1698 :
1699 2106 : void Mode2_delta_pit_enc_fx(
1700 : Word16 T0, /* i : integer pitch lag */
1701 : Word16 T0_frac, /* i : pitch fraction */
1702 : Word16 T0_res, /* i : pitch resolution */
1703 : Word16 T0_min, /* i/o: delta search min */
1704 : Word16 T0_min_frac, /* i/o: delta search min */
1705 : Word16 **pt_indice /* i/o: pointer to Vector of Q indexes */
1706 : )
1707 : {
1708 : /***pt_indice = (T0 - T0_min) * T0_res + T0_frac - T0_min_frac;*/
1709 2106 : **pt_indice = add( i_mult2( sub( T0, T0_min ), T0_res ), sub( T0_frac, T0_min_frac ) );
1710 2106 : move16();
1711 2106 : ( *pt_indice )++;
1712 :
1713 2106 : return;
1714 : }
|