Line data Source code
1 : /******************************************************************************************************
2 :
3 : (C) 2022-2026 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
4 : Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
5 : Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
6 : Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
7 : contributors to this repository. All Rights Reserved.
8 :
9 : This software is protected by copyright law and by international treaties.
10 : The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
11 : Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
12 : Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
13 : Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
14 : contributors to this repository retain full ownership rights in their respective contributions in
15 : the software. This notice grants no license of any kind, including but not limited to patent
16 : license, nor is any license granted by implication, estoppel or otherwise.
17 :
18 : Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
19 : contributions.
20 :
21 : This software is provided "AS IS", without any express or implied warranties. The software is in the
22 : development stage. It is intended exclusively for experts who have experience with such software and
23 : solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
24 : and fitness for a particular purpose are hereby disclaimed and excluded.
25 :
26 : Any dispute, controversy or claim arising under or in relation to providing this software shall be
27 : submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
28 : accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
29 : the United Nations Convention on Contracts on the International Sales of Goods.
30 :
31 : *******************************************************************************************************/
32 :
33 : /*====================================================================================
34 : EVS Codec 3GPP TS26.443 Nov 04, 2021. Version 12.14.0 / 13.10.0 / 14.6.0 / 15.4.0 / 16.3.0
35 : ====================================================================================*/
36 :
37 : #include <assert.h>
38 : #include <stdint.h>
39 : #include "options.h"
40 : #include <math.h>
41 : #include "cnst.h"
42 : #include "prot.h"
43 : #include "rom_enc.h"
44 : #include "rom_com.h"
45 : #include "wmc_auto.h"
46 :
47 : /*------------------------------------------------------------------*
48 : * pit_encode()
49 : *
50 : * Close-loop pitch lag search and pitch lag quantization
51 : * Adaptive excitation construction
52 : *------------------------------------------------------------------*/
53 :
54 : /*! r: Fractional pitch for each subframe */
55 4482947 : float pit_encode(
56 : BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle */
57 : const int16_t pitch_bits[], /* i : pitch bits */
58 : const int32_t core_brate, /* i : core bitrate */
59 : const int16_t Opt_AMR_WB, /* i : flag indicating AMR-WB IO mode */
60 : const int16_t L_frame, /* i : length of the frame */
61 : const int16_t coder_type, /* i : coding type */
62 : int16_t *limit_flag, /* i/o: restrained(0) or extended(1) Q limits */
63 : const int16_t i_subfr, /* i : subframe index */
64 : float *exc, /* i/o: pointer to excitation signal frame */
65 : const int16_t L_subfr, /* i : subframe length */
66 : const int16_t *pitch, /* i : open loop pitch estimates in current frame */
67 : int16_t *T0_min, /* i/o: lower limit for close-loop search */
68 : int16_t *T0_max, /* i/o: higher limit for close-loop search */
69 : int16_t *T0, /* i/o: close loop integer pitch */
70 : int16_t *T0_frac, /* i/o: close loop fractional part of the pitch */
71 : const float *h1, /* i : weighted filter input response */
72 : const float *xn, /* i : target vector */
73 : const int16_t tdm_Pitch_reuse_flag, /* i : primary channel pitch reuse flag */
74 : const float tdm_Pri_pitch_buf[] /* i : primary channel pitch buffer */
75 : )
76 : {
77 : float pitch_cl;
78 : int16_t pit_flag, delta, mult_Top, nBits;
79 : int16_t T_op[2]; /* values for two half-frames */
80 :
81 : /*----------------------------------------------------------------*
82 : * convert pitch values to 16kHz domain
83 : *----------------------------------------------------------------*/
84 :
85 4482947 : if ( L_frame == L_FRAME || ( tdm_Pri_pitch_buf != NULL && tdm_Pri_pitch_buf[0] < 0 ) )
86 : {
87 1870355 : mvs2s( pitch, T_op, 2 );
88 : }
89 : else /* L_frame == L_FRAME16k */
90 : {
91 2612592 : T_op[0] = (int16_t) ( pitch[0] * 1.25f + 0.5f );
92 2612592 : T_op[1] = (int16_t) ( pitch[1] * 1.25f + 0.5f );
93 : }
94 :
95 : /*----------------------------------------------------------------*
96 : * Initializations
97 : *----------------------------------------------------------------*/
98 :
99 : /* Set pit_flag to 0 for every subframe with absolute pitch search */
100 4482947 : pit_flag = i_subfr;
101 4482947 : if ( i_subfr == 2 * L_SUBFR )
102 : {
103 996907 : pit_flag = 0;
104 : }
105 :
106 : /*-----------------------------------------------------------------*
107 : * Limit range of pitch search
108 : * Fractional pitch search
109 : * Pitch quantization
110 : *-----------------------------------------------------------------*/
111 :
112 4482947 : mult_Top = 1;
113 :
114 4482947 : if ( !Opt_AMR_WB )
115 : {
116 : /*----------------------------------------------------------------*
117 : * Set limit_flag to 0 for restrained limits, and 1 for extended limits
118 : *----------------------------------------------------------------*/
119 :
120 4473499 : if ( i_subfr == 0 )
121 : {
122 1027159 : *limit_flag = 1;
123 1027159 : if ( coder_type == VOICED )
124 : {
125 63336 : *limit_flag = 2; /* double-extended limits */
126 : }
127 :
128 1027159 : if ( coder_type == GENERIC && core_brate == ACELP_7k20 )
129 : {
130 3787 : *limit_flag = 0;
131 : }
132 : }
133 3446340 : else if ( i_subfr == 2 * L_SUBFR && coder_type == GENERIC && core_brate <= ACELP_13k20 )
134 : {
135 266621 : if ( *T0 > ( PIT_FR1_EXTEND_8b + PIT_MIN ) >> 1 )
136 : {
137 198500 : *limit_flag = 0;
138 : }
139 : }
140 :
141 : /* check the minimum pitch value */
142 4473499 : if ( *limit_flag == 0 )
143 : {
144 403772 : if ( ( i_subfr == 0 && T_op[0] < PIT_MIN ) || ( i_subfr == 2 * L_SUBFR && T_op[1] < PIT_MIN ) )
145 : {
146 7636 : mult_Top = 2;
147 : }
148 : }
149 :
150 : /*-------------------------------------------------------*
151 : * Retrieve the number of Q bits
152 : *-------------------------------------------------------*/
153 :
154 4473499 : nBits = 0;
155 4473499 : if ( coder_type != AUDIO )
156 : {
157 4282126 : nBits = pitch_bits[i_subfr / L_subfr];
158 : }
159 :
160 4473499 : if ( coder_type == AUDIO )
161 : {
162 : /*-------------------------------------------------------*
163 : * Pitch encoding in AUDIO coder type
164 : * (both ACELP@12k8 and ACELP@16k cores)
165 : *-------------------------------------------------------*/
166 :
167 191373 : delta = 4;
168 :
169 191373 : if ( L_subfr == L_frame / 2 && i_subfr != 0 )
170 : {
171 24781 : pit_flag = L_SUBFR;
172 : }
173 :
174 191373 : if ( pit_flag == 0 )
175 : {
176 103348 : nBits = 10;
177 : }
178 : else
179 : {
180 88025 : nBits = 6;
181 : }
182 :
183 : /* pitch lag search limitation */
184 191373 : if ( i_subfr == 0 )
185 : {
186 71726 : limit_T0( L_FRAME, delta, pit_flag, *limit_flag, mult_Top * T_op[0], 0, T0_min, T0_max );
187 : }
188 119647 : else if ( i_subfr == 2 * L_SUBFR && pit_flag == 0 )
189 : {
190 31622 : limit_T0( L_FRAME, delta, pit_flag, *limit_flag, mult_Top * T_op[1], 0, T0_min, T0_max );
191 : }
192 :
193 : /* search and encode the closed loop pitch period */
194 191373 : *T0 = pitch_fr4( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, pit_flag, *limit_flag, PIT_MAX, PIT_MAX, L_FRAME, L_subfr );
195 :
196 191373 : pit_Q_enc( hBstr, 0, nBits, delta, pit_flag, *limit_flag, *T0, *T0_frac, T0_min, T0_max );
197 : }
198 4282126 : else if ( coder_type == VOICED )
199 : {
200 : /*-------------------------------------------------------*
201 : * Pitch encoding in VOICED coder type (ACELP@12k8 core only)
202 : *-------------------------------------------------------*/
203 :
204 253344 : delta = 4;
205 :
206 253344 : if ( i_subfr == 2 * L_SUBFR )
207 : {
208 63336 : pit_flag = i_subfr;
209 : }
210 :
211 : /* pitch lag search limitation */
212 253344 : if ( i_subfr == 0 )
213 : {
214 63336 : limit_T0( L_FRAME, delta, pit_flag, *limit_flag, mult_Top * T_op[0], 0, T0_min, T0_max );
215 : }
216 :
217 : /* search and encode the closed loop pitch period */
218 253344 : if ( nBits == 9 || nBits == 5 )
219 : {
220 192526 : *T0 = pitch_fr4( &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 );
221 : }
222 60818 : else if ( nBits == 10 )
223 : {
224 60818 : *T0 = pitch_fr4( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, pit_flag, *limit_flag, PIT_MAX, PIT_MAX, L_FRAME, L_SUBFR );
225 : }
226 :
227 253344 : pit_Q_enc( hBstr, 0, nBits, delta, pit_flag, *limit_flag, *T0, *T0_frac, T0_min, T0_max );
228 : }
229 4028782 : else if ( tdm_Pitch_reuse_flag == 1 || nBits == 4 )
230 276 : {
231 : /*-------------------------------------------------------*
232 : * Pitch encoding with reusing primary channel information
233 : *-------------------------------------------------------*/
234 : int16_t loc_T0, loc_frac;
235 :
236 276 : delta = 4;
237 :
238 276 : pit_flag = L_SUBFR;
239 :
240 276 : if ( L_subfr == 2 * L_SUBFR )
241 : {
242 0 : loc_T0 = (int16_t) ( 0.5f * tdm_Pri_pitch_buf[i_subfr / L_SUBFR] + 0.5f * tdm_Pri_pitch_buf[( i_subfr + L_SUBFR ) / L_SUBFR] );
243 0 : loc_frac = (int16_t) ( ( ( 0.5f * tdm_Pri_pitch_buf[i_subfr / L_SUBFR] + 0.5f * tdm_Pri_pitch_buf[( i_subfr + L_SUBFR ) / L_SUBFR] ) - loc_T0 ) * 4.0f );
244 : }
245 : else
246 : {
247 276 : loc_T0 = (int16_t) tdm_Pri_pitch_buf[i_subfr / L_SUBFR];
248 276 : loc_frac = (int16_t) ( ( tdm_Pri_pitch_buf[i_subfr / L_SUBFR] - loc_T0 ) * 4.0f );
249 : }
250 :
251 : /* pitch lag search limitation */
252 276 : limit_T0( L_FRAME, delta, pit_flag, *limit_flag, loc_T0, loc_frac, T0_min, T0_max );
253 276 : if ( nBits > 0 )
254 : {
255 : /* search and encode the closed loop pitch period */
256 0 : *T0 = pitch_fr4( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, pit_flag, *limit_flag, PIT_MIN, PIT_FR1_8b, L_FRAME, L_SUBFR );
257 0 : if ( delta == 8 )
258 : {
259 0 : *T0_frac = 0;
260 : }
261 0 : pit_Q_enc( hBstr, 0, nBits, delta, pit_flag, *limit_flag, *T0, *T0_frac, T0_min, T0_max );
262 : }
263 : else
264 : {
265 276 : *T0 = loc_T0;
266 276 : *T0_frac = loc_frac;
267 : }
268 : }
269 : else
270 : {
271 : /*-------------------------------------------------------*
272 : * Pitch encoding in GENERIC coder type
273 : * (both ACELP@12k8 and ACELP@16k cores)
274 : *-------------------------------------------------------*/
275 :
276 4028506 : delta = 8;
277 :
278 : /* pitch lag search limitation */
279 4028506 : if ( i_subfr == 0 )
280 : {
281 892028 : limit_T0( L_frame, delta, pit_flag, *limit_flag, mult_Top * T_op[0], 0, T0_min, T0_max );
282 : }
283 3136478 : else if ( i_subfr == 2 * L_SUBFR )
284 : {
285 874737 : limit_T0( L_frame, delta, pit_flag, *limit_flag, mult_Top * T_op[1], 0, T0_min, T0_max );
286 : }
287 :
288 : /* search and encode the closed loop pitch period */
289 4028506 : if ( L_frame == L_FRAME )
290 : {
291 1415684 : if ( nBits == 8 || nBits == 5 )
292 : {
293 40241 : if ( *limit_flag == 0 )
294 : {
295 26693 : *T0 = pitch_fr4( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, pit_flag, *limit_flag, PIT_MIN, PIT_FR1_8b, L_FRAME, L_SUBFR );
296 : }
297 : else
298 : {
299 13548 : *T0 = pitch_fr4( &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 );
300 : }
301 : }
302 1375443 : else if ( nBits == 9 || nBits == 6 )
303 : {
304 945832 : if ( *limit_flag == 0 )
305 : {
306 376941 : *T0 = pitch_fr4( &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 );
307 : }
308 : else
309 : {
310 568891 : *T0 = pitch_fr4( &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 );
311 : }
312 : }
313 429611 : else if ( nBits == 10 )
314 : {
315 429611 : *T0 = pitch_fr4( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, pit_flag, *limit_flag, PIT_MAX, PIT_MAX, L_FRAME, L_SUBFR );
316 : }
317 :
318 1415684 : pit_Q_enc( hBstr, 0, nBits, delta, pit_flag, *limit_flag, *T0, *T0_frac, T0_min, T0_max );
319 : }
320 : else /* L_frame == L_FRAME16k */
321 : {
322 2612822 : if ( nBits == 9 || nBits == 6 )
323 : {
324 1557357 : *T0 = pitch_fr4( &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 );
325 : }
326 1055465 : else if ( nBits == 10 )
327 : {
328 1055465 : *T0 = pitch_fr4( &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 );
329 : }
330 :
331 2612822 : pit16k_Q_enc( hBstr, nBits, *limit_flag, *T0, *T0_frac, T0_min, T0_max );
332 : }
333 : }
334 : }
335 :
336 : /*-------------------------------------------------------*
337 : * Pitch encoding in AMR-WB IO mode
338 : *-------------------------------------------------------*/
339 :
340 : else
341 : {
342 9448 : delta = 8;
343 9448 : *limit_flag = 0;
344 :
345 9448 : if ( core_brate == ACELP_6k60 )
346 : {
347 520 : nBits = 5;
348 :
349 : /* pitch lag search limitation */
350 520 : if ( i_subfr == 0 )
351 : {
352 130 : limit_T0( L_FRAME, delta, pit_flag, *limit_flag, mult_Top * T_op[0], 0, T0_min, T0_max );
353 130 : nBits = 8;
354 : }
355 :
356 520 : if ( i_subfr == 2 * L_SUBFR )
357 : {
358 : /* rewrite pit_flag - it must not be zero */
359 130 : pit_flag = i_subfr;
360 : }
361 :
362 : /* search and encode the closed loop pitch period */
363 520 : *T0 = pitch_fr4( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, pit_flag, *limit_flag, PIT_MIN, PIT_FR1_8b, L_FRAME, L_SUBFR );
364 : }
365 8928 : else if ( core_brate == ACELP_8k85 )
366 : {
367 120 : nBits = 5;
368 :
369 : /* pitch lag search limitation */
370 120 : if ( i_subfr == 0 )
371 : {
372 30 : limit_T0( L_FRAME, delta, pit_flag, *limit_flag, mult_Top * T_op[0], 0, T0_min, T0_max );
373 30 : nBits = 8;
374 : }
375 90 : else if ( i_subfr == 2 * L_SUBFR )
376 : {
377 30 : limit_T0( L_FRAME, delta, pit_flag, *limit_flag, mult_Top * T_op[1], 0, T0_min, T0_max );
378 30 : nBits = 8;
379 : }
380 :
381 : /* search and encode the closed loop pitch period */
382 120 : *T0 = pitch_fr4( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, pit_flag, *limit_flag, PIT_MIN, PIT_FR1_8b, L_FRAME, L_SUBFR );
383 : }
384 : else
385 : {
386 8808 : nBits = 6;
387 :
388 : /* pitch lag search limitation */
389 8808 : if ( i_subfr == 0 )
390 : {
391 2202 : limit_T0( L_FRAME, delta, pit_flag, *limit_flag, mult_Top * T_op[0], 0, T0_min, T0_max );
392 2202 : nBits = 9;
393 : }
394 6606 : else if ( i_subfr == 2 * L_SUBFR )
395 : {
396 2202 : limit_T0( L_FRAME, delta, pit_flag, *limit_flag, mult_Top * T_op[1], 0, T0_min, T0_max );
397 2202 : nBits = 9;
398 : }
399 : else
400 : {
401 4404 : limit_T0( L_FRAME, delta, pit_flag, 0, *T0, 0, T0_min, T0_max ); /* T0_frac==0 to keep IO with AMR-WB */
402 : }
403 :
404 : /* search and encode the closed loop pitch period */
405 8808 : *T0 = pitch_fr4( &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 );
406 : }
407 :
408 9448 : pit_Q_enc( hBstr, 1, nBits, delta, pit_flag, *limit_flag, *T0, *T0_frac, T0_min, T0_max );
409 : }
410 :
411 : /*-------------------------------------------------------*
412 : * Compute floating pitch output
413 : *-------------------------------------------------------*/
414 :
415 4482947 : pitch_cl = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f; /* save subframe pitch values */
416 :
417 4482947 : return pitch_cl;
418 : }
419 :
420 : /*-------------------------------------------------------------------*
421 : * pitch_fr4()
422 : *
423 : * Find the closed loop pitch period with 1/4 subsample resolution.
424 : *-------------------------------------------------------------------*/
425 :
426 : /*! r: chosen integer pitch lag */
427 4879894 : int16_t pitch_fr4(
428 : const float exc[], /* i : excitation buffer */
429 : const float xn[], /* i : target signal */
430 : const float h[], /* i : weighted synthesis filter impulse response */
431 : const int16_t t0_min, /* i : minimum value in the searched range. */
432 : const int16_t t0_max, /* i : maximum value in the searched range. */
433 : int16_t *pit_frac, /* o : chosen fraction (0, 1, 2 or 3) */
434 : const int16_t i_subfr, /* i : flag to first subframe */
435 : const int16_t limit_flag, /* i : flag for limits (0=restrained, 1=extended) */
436 : const int16_t t0_fr2, /* i : minimum value for resolution 1/2 */
437 : const int16_t t0_fr1, /* i : minimum value for resolution 1 */
438 : const int16_t L_frame, /* i : length of the frame */
439 : const int16_t L_subfr /* i : size of subframe */
440 : )
441 : {
442 : int16_t i, fraction, step;
443 : int16_t t0, t1, t_min, t_max, pit_min;
444 : float cor_max, max_val, temp;
445 : float *corr, corr_v[15 + 2 * L_INTERPOL1 + 1];
446 : int16_t corr_off;
447 :
448 : /* initialization */
449 4879894 : if ( limit_flag == 0 )
450 : {
451 641005 : if ( L_frame == L_FRAME )
452 : {
453 574353 : pit_min = PIT_MIN;
454 : }
455 : else /* L_frame == L_FRAME16k */
456 : {
457 66652 : pit_min = PIT16k_MIN;
458 : }
459 : }
460 : else
461 : {
462 4238889 : if ( L_frame == L_FRAME )
463 : {
464 1456767 : pit_min = PIT_MIN_EXTEND;
465 1456767 : if ( limit_flag == 2 )
466 : {
467 253344 : pit_min = PIT_MIN_DOUBLEEXTEND;
468 : }
469 : }
470 : else /* L_frame == L_FRAME16k */
471 : {
472 2782122 : pit_min = PIT16k_MIN_EXTEND;
473 : }
474 : }
475 :
476 :
477 : /*-----------------------------------------------------------------*
478 : * - Find interval to compute normalized correlation
479 : * - allocate memory to normalized correlation vector
480 : * - Compute normalized correlation between target and filtered
481 : * excitation
482 : *-----------------------------------------------------------------*/
483 :
484 4879894 : t_min = t0_min - L_INTERPOL1;
485 4879894 : t_max = t0_max + L_INTERPOL1;
486 :
487 4879894 : corr = corr_v;
488 4879894 : corr_off = -t_min;
489 :
490 4879894 : norm_corr( exc, xn, h, t_min, t_max, corr + corr_off, L_subfr );
491 :
492 : /*-----------------------------------------------------------------*
493 : * Find integer pitch
494 : *-----------------------------------------------------------------*/
495 :
496 4879894 : max_val = corr[t0_min + corr_off];
497 4879894 : t0 = t0_min;
498 :
499 74520568 : for ( i = t0_min + 1; i <= t0_max; i++ )
500 : {
501 69640674 : if ( corr[i + corr_off] >= max_val )
502 : {
503 26966439 : max_val = corr[i + corr_off];
504 26966439 : t0 = i;
505 : }
506 : }
507 :
508 4879894 : if ( t0_fr1 == pit_min )
509 : {
510 : /* don't search fraction (for 7b/4b quant) */
511 5972 : if ( ( i_subfr == 0 ) && ( t0 >= t0_fr2 ) )
512 : {
513 0 : i = ( t0 >> 1 ) * 2; /* 2 samples resolution */
514 0 : if ( ( i + 2 ) > PIT_MAX )
515 : {
516 0 : i -= 2;
517 : }
518 0 : if ( corr[i + corr_off] > corr[i + 2 + corr_off] )
519 : {
520 0 : t0 = i;
521 : }
522 : else
523 : {
524 0 : t0 = i + 2;
525 : }
526 : }
527 :
528 5972 : *pit_frac = 0;
529 :
530 5972 : return ( t0 );
531 : }
532 4873922 : if ( ( i_subfr == 0 ) && ( t0 >= t0_fr1 ) )
533 : {
534 89137 : *pit_frac = 0;
535 :
536 89137 : return ( t0 );
537 : }
538 :
539 : /*------------------------------------------------------------------*
540 : * Search fractionnal pitch with 1/4 subsample resolution.
541 : * search the fractions around t0 and choose the one which maximizes
542 : * the interpolated normalized correlation.
543 : *-----------------------------------------------------------------*/
544 :
545 4784785 : t1 = t0;
546 4784785 : step = 1; /* 1/4 subsample resolution */
547 4784785 : fraction = 1;
548 4784785 : if ( ( ( i_subfr == 0 ) && ( t0 >= t0_fr2 ) ) || ( t0_fr2 == pit_min ) )
549 : {
550 143274 : step = 2; /* 1/2 subsample resolution */
551 143274 : fraction = 2;
552 : }
553 :
554 4784785 : if ( t0 == t0_min ) /* Limit case */
555 : {
556 300639 : fraction = 0;
557 300639 : cor_max = interpolation( &corr[t0 + corr_off], E_ROM_inter4_1, fraction, PIT_UP_SAMP, 4 );
558 : }
559 : else /* Process negative fractions */
560 : {
561 4484146 : t0--;
562 4484146 : cor_max = interpolation( &corr[t0 + corr_off], E_ROM_inter4_1, fraction, PIT_UP_SAMP, 4 );
563 13194004 : for ( i = ( fraction + step ); i <= 3; i = i + step )
564 : {
565 8709858 : temp = interpolation( &corr[t0 + corr_off], E_ROM_inter4_1, i, PIT_UP_SAMP, 4 );
566 8709858 : if ( temp > cor_max )
567 : {
568 8113534 : cor_max = temp;
569 8113534 : fraction = i;
570 : }
571 : }
572 : }
573 :
574 23637377 : for ( i = 0; i <= 3; i = i + step ) /* Process positive fractions */
575 : {
576 18852592 : temp = interpolation( &corr[t1 + corr_off], E_ROM_inter4_1, i, PIT_UP_SAMP, 4 );
577 18852592 : if ( temp > cor_max )
578 : {
579 5547970 : cor_max = temp;
580 5547970 : fraction = i;
581 5547970 : t0 = t1;
582 : }
583 : }
584 :
585 4784785 : *pit_frac = fraction;
586 :
587 4784785 : return ( t0 );
588 : }
589 :
590 : /*-------------------------------------------------------------------*
591 : * norm_corr()
592 : *
593 : * Find the normalized correlation between the target vector and the
594 : * filtered past excitation (correlation between target and filtered
595 : * excitation divided by the square root of energy of filtered
596 : * excitation)
597 : *---------------------------------------------------------------------*/
598 :
599 4934260 : void norm_corr(
600 : const float exc[], /* i : excitation buffer */
601 : const float xn[], /* i : target signal */
602 : const float h[], /* i : weighted synthesis filter impulse response */
603 : const int16_t t_min, /* i : minimum value of searched range */
604 : const int16_t t_max, /* i : maximum value of searched range */
605 : float corr_norm[], /* o : normalized correlation */
606 : const int16_t L_subfr /* i : subframe size */
607 : )
608 : {
609 : int16_t t, j, k;
610 : float excf[L_FRAME16k]; /* filtered past excitation */ /* length up to L_FRAME in GSC */
611 : float alp, ps, norm;
612 :
613 4934260 : k = -t_min;
614 :
615 : /*-----------------------------------------------------------------*
616 : * compute the filtered excitation for the first delay t_min
617 : *-----------------------------------------------------------------*/
618 :
619 4934260 : conv( &exc[k], h, excf, L_subfr );
620 :
621 : /*-----------------------------------------------------------------*
622 : * loop for every possible period
623 : *-----------------------------------------------------------------*/
624 :
625 119932482 : for ( t = t_min; t <= t_max; t++ )
626 : {
627 : /* Compute correlation between xn[] and excf[] */
628 114998222 : ps = 0.0f;
629 7572708174 : for ( j = 0; j < L_subfr; ++j )
630 : {
631 7457709952 : ps += xn[j] * excf[j];
632 : }
633 :
634 : /* Compute 1/sqrt(energie of excf[]) */
635 114998222 : alp = 0.01f;
636 7572708174 : for ( j = 0; j < L_subfr; ++j )
637 : {
638 7457709952 : alp += excf[j] * excf[j];
639 : }
640 114998222 : norm = inv_sqrt( alp );
641 :
642 : /* Normalize correlation = correlation * (1/sqrt(energie)) */
643 114998222 : corr_norm[t] = ps * norm;
644 :
645 : /* update the filtered excitation excf[] for the next iteration */
646 114998222 : if ( t != t_max )
647 : {
648 110063962 : k--;
649 7135803328 : for ( j = L_subfr - 1; j > 0; j-- )
650 : {
651 7025739366 : excf[j] = excf[j - 1] + exc[k] * h[j];
652 : }
653 110063962 : excf[0] = exc[k];
654 : }
655 : }
656 4934260 : return;
657 : }
658 :
659 : /*-------------------------------------------------------------------*
660 : * abs_pit_enc()
661 : *
662 : * Encode pitch lag absolutely with resolution for shortest pitches
663 : * depending on parameter 'fr_step':
664 : * fr_step = 2: pitch range encoded with 8 bits
665 : * fr_step = 4: pitch range encoded with 9 bits
666 : *-------------------------------------------------------------------*/
667 :
668 : /*! r: pitch index */
669 322043 : int16_t abs_pit_enc(
670 : const int16_t fr_steps, /* i : fractional resolution step */
671 : const int16_t limit_flag, /* i : restrained(0) or extended(1) limits */
672 : const int16_t T0, /* i : integer pitch lag */
673 : const int16_t T0_frac /* i : pitch fraction */
674 : )
675 : {
676 : int16_t pitch_index;
677 :
678 322043 : if ( limit_flag == 0 )
679 : {
680 240947 : if ( fr_steps == 2 )
681 : {
682 : /*-----------------------------------------------------------------*
683 : * The pitch range is encoded absolutely with 8 bits
684 : * and is divided as follows:
685 : * PIT_MIN to PIT_FR1_8b-1 resolution 1/2 (frac = 0 or 2)
686 : * PIT_FR1_8b to PIT_MAX resolution 1 (frac = 0)
687 : *-----------------------------------------------------------------*/
688 13645 : if ( T0 < PIT_FR1_8b )
689 : {
690 7063 : pitch_index = T0 * 2 + ( T0_frac >> 1 ) - ( PIT_MIN * 2 );
691 : }
692 : else
693 : {
694 6582 : pitch_index = T0 - PIT_FR1_8b + ( ( PIT_FR1_8b - PIT_MIN ) * 2 );
695 : }
696 : }
697 227302 : else if ( fr_steps == 4 )
698 : {
699 : /*-------------------------------------------------------------------*
700 : * The pitch range is encoded absolutely with 9 bits
701 : * and is divided as follows:
702 : * PIT_MIN to PIT_FR2_9b-1 resolution 1/4 (frac = 0,1,2 or 3)
703 : * PIT_FR2_9b to PIT_FR1_9b-1 resolution 1/2 (frac = 0 or 2)
704 : * PIT_FR1_9b to PIT_MAX resolution 1 (frac = 0)
705 : *-------------------------------------------------------------------*/
706 227302 : if ( T0 < PIT_FR2_9b )
707 : {
708 175998 : pitch_index = T0 * 4 + T0_frac - ( PIT_MIN * 4 );
709 : }
710 51304 : else if ( T0 < PIT_FR1_9b )
711 : {
712 27827 : pitch_index = T0 * 2 + ( T0_frac >> 1 ) - ( PIT_FR2_9b * 2 ) + ( ( PIT_FR2_9b - PIT_MIN ) * 4 );
713 : }
714 : else
715 : {
716 23477 : pitch_index = T0 - PIT_FR1_9b + ( ( PIT_FR2_9b - PIT_MIN ) * 4 ) + ( ( PIT_FR1_9b - PIT_FR2_9b ) * 2 );
717 : }
718 : }
719 : else /* fr_step == 0 */
720 : {
721 : /* not used in the codec */
722 0 : pitch_index = 0;
723 : }
724 : }
725 81096 : else if ( limit_flag == 1 ) /* extended Q range */
726 : {
727 78578 : if ( fr_steps == 2 )
728 : {
729 : /*-----------------------------------------------------------------*
730 : * The pitch range is encoded absolutely with 8 bits
731 : * and is divided as follows:
732 : * PIT_MIN_EXTEND to PIT_FR1_EXTEND_8b-1 resolution 1/2 (frac = 0 or 2)
733 : * PIT_FR1_EXTEND_8b to PIT_MAX resolution 1 (frac = 0)
734 : *-----------------------------------------------------------------*/
735 :
736 3477 : if ( T0 < PIT_FR1_EXTEND_8b )
737 : {
738 2113 : pitch_index = T0 * 2 + ( T0_frac >> 1 ) - ( PIT_MIN_EXTEND * 2 );
739 : }
740 : else
741 : {
742 1364 : pitch_index = T0 - PIT_FR1_EXTEND_8b + ( ( PIT_FR1_EXTEND_8b - PIT_MIN_EXTEND ) * 2 );
743 : }
744 : }
745 75101 : else if ( fr_steps == 4 )
746 : {
747 : /*-------------------------------------------------------------------*
748 : * The pitch range is encoded absolutely with 9 bits
749 : * and is divided as follows:
750 : * PIT_MIN_EXTEND to PIT_FR2__EXTEND9b-1 resolution 1/4 (frac = 0,1,2 or 3)
751 : * PIT_FR2_EXTEND_9b to PIT_FR1__EXTEND9b-1 resolution 1/2 (frac = 0 or 2)
752 : * PIT_FR1_EXTEND_9b to PIT_MAX resolution 1 (frac = 0)
753 : *-------------------------------------------------------------------*/
754 :
755 75101 : if ( T0 < PIT_FR2_EXTEND_9b )
756 : {
757 64417 : pitch_index = T0 * 4 + T0_frac - ( PIT_MIN_EXTEND * 4 );
758 : }
759 10684 : else if ( T0 < PIT_FR1_EXTEND_9b )
760 : {
761 2030 : pitch_index = T0 * 2 + ( T0_frac >> 1 ) - ( PIT_FR2_EXTEND_9b * 2 ) + ( ( PIT_FR2_EXTEND_9b - PIT_MIN_EXTEND ) * 4 );
762 : }
763 : else
764 : {
765 8654 : pitch_index = T0 - PIT_FR1_EXTEND_9b + ( ( PIT_FR2_EXTEND_9b - PIT_MIN_EXTEND ) * 4 ) + ( ( PIT_FR1_EXTEND_9b - PIT_FR2_EXTEND_9b ) * 2 );
766 : }
767 : }
768 : else /* fr_step == 0 */
769 : {
770 : /* not used in the codec */
771 0 : pitch_index = 0;
772 : }
773 : }
774 : else /* double-extended Q range */
775 : {
776 2518 : if ( fr_steps == 2 )
777 : {
778 : /*-----------------------------------------------------------------*
779 : * The pitch range is encoded absolutely with 8 bits
780 : * and is divided as follows:
781 : * PIT_MIN_DOUBLEEXTEND to PIT_FR1_DOUBLEEXTEND_8b-1 resolution 1/2 (frac = 0 or 2)
782 : * PIT_FR1_DOUBLEEXTEND_8b to PIT_MAX resolution 1 (frac = 0)
783 : *-----------------------------------------------------------------*/
784 :
785 0 : if ( T0 < PIT_FR1_DOUBLEEXTEND_8b )
786 : {
787 0 : pitch_index = T0 * 2 + ( T0_frac >> 1 ) - ( PIT_MIN_DOUBLEEXTEND * 2 );
788 : }
789 : else
790 : {
791 0 : pitch_index = T0 - PIT_FR1_DOUBLEEXTEND_8b + ( ( PIT_FR1_DOUBLEEXTEND_8b - PIT_MIN_DOUBLEEXTEND ) * 2 );
792 : }
793 : }
794 2518 : else if ( fr_steps == 4 )
795 : {
796 : /*-------------------------------------------------------------------*
797 : * The pitch range is encoded absolutely with 9 bits
798 : * and is divided as follows:
799 : * PIT_MIN_DOUBLEEXTEND to PIT_FR2_DOUBLEEXTEND9b-1 resolution 1/4 (frac = 0,1,2 or 3)
800 : * PIT_FR2_DOUBLEEXTEND_9b to PIT_FR1_DOOBLEEXTEND9b-1 resolution 1/2 (frac = 0 or 2)
801 : * PIT_FR1_DOUBLEEXTEND_9b to PIT_MAX resolution 1 (frac = 0)
802 : *-------------------------------------------------------------------*/
803 :
804 2518 : if ( T0 < PIT_FR2_DOUBLEEXTEND_9b )
805 : {
806 2343 : pitch_index = T0 * 4 + T0_frac - ( PIT_MIN_DOUBLEEXTEND * 4 );
807 : }
808 175 : else if ( T0 < PIT_FR1_DOUBLEEXTEND_9b )
809 : {
810 60 : pitch_index = T0 * 2 + ( T0_frac >> 1 ) - ( PIT_FR2_DOUBLEEXTEND_9b * 2 ) + ( ( PIT_FR2_DOUBLEEXTEND_9b - PIT_MIN_DOUBLEEXTEND ) * 4 );
811 : }
812 : else
813 : {
814 115 : pitch_index = T0 - PIT_FR1_DOUBLEEXTEND_9b + ( ( PIT_FR2_DOUBLEEXTEND_9b - PIT_MIN_DOUBLEEXTEND ) * 4 ) + ( ( PIT_FR1_DOUBLEEXTEND_9b - PIT_FR2_DOUBLEEXTEND_9b ) * 2 );
815 : }
816 : }
817 : else /* fr_step == 0 */
818 : {
819 : /* not used in the codec */
820 0 : pitch_index = 0;
821 : }
822 : }
823 :
824 322043 : return pitch_index;
825 : }
826 :
827 : /*-------------------------------------------------------------------*
828 : * delta_pit_enc()
829 : *
830 : * Encode pitch lag differentially from T0_min to T0_max
831 : * with resolution depending on parameter 'fr_step':
832 : * fr_step = 0: resolution 1 (frac = 0), or
833 : * fr_step = 2: resolution 1/2 (frac = 0 or 2), or
834 : * fr_step = 4: resolution 1/4 (frac = 0, 1, 2, or 3)
835 : *-------------------------------------------------------------------*/
836 :
837 : /*! r: pitch index */
838 1186893 : int16_t delta_pit_enc(
839 : const int16_t fr_steps, /* i : fractional resolution step */
840 : const int16_t T0, /* i : integer pitch lag */
841 : const int16_t T0_frac, /* i : pitch fraction */
842 : const int16_t T0_min /* i : delta search min */
843 : )
844 : {
845 : int16_t pitch_index;
846 :
847 1186893 : if ( fr_steps == 0 )
848 : {
849 2137 : pitch_index = T0 - T0_min;
850 : }
851 1184756 : else if ( fr_steps == 2 )
852 : {
853 56355 : pitch_index = ( T0 - T0_min ) * 2 + ( T0_frac >> 1 );
854 : }
855 : else /* fr_steps == 4 */
856 : {
857 1128401 : pitch_index = ( T0 - T0_min ) * 4 + T0_frac;
858 : }
859 :
860 1186893 : return pitch_index;
861 : }
862 :
863 : /*-------------------------------------------------------------------*
864 : * pit_Q_enc()
865 : *
866 : * Encode subframe pitch lag
867 : *-------------------------------------------------------------------*/
868 :
869 1960109 : void pit_Q_enc(
870 : BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle */
871 : const int16_t Opt_AMR_WB, /* i : flag indicating AMR-WB IO mode */
872 : const int16_t nBits, /* i : # of Q bits */
873 : const int16_t delta, /* i : Half the CL searched interval */
874 : const int16_t pit_flag, /* i : absolute(0) or delta(1) pitch Q */
875 : const int16_t limit_flag, /* i : restrained(0) or extended(1) Q limits */
876 : const int16_t T0, /* i : integer pitch lag */
877 : const int16_t T0_frac, /* i : pitch fraction */
878 : int16_t *T0_min, /* i/o: delta search min */
879 : int16_t *T0_max /* o : delta search max */
880 : )
881 : {
882 : int16_t pitch_index;
883 :
884 1960109 : if ( nBits == 10 ) /* absolute encoding with 10 bits */
885 : {
886 593777 : if ( limit_flag == 0 )
887 : {
888 0 : pitch_index = T0 * 4 + T0_frac - ( PIT_MIN * 4 );
889 : }
890 593777 : else if ( limit_flag == 1 )
891 : {
892 532959 : pitch_index = T0 * 4 + T0_frac - ( PIT_MIN_EXTEND * 4 );
893 : }
894 : else /* limit_flag == 2 */
895 : {
896 60818 : pitch_index = T0 * 4 + T0_frac - ( PIT_MIN_DOUBLEEXTEND * 4 );
897 : }
898 : }
899 1366332 : else if ( nBits == 9 ) /* absolute encoding with 9 bits */
900 : {
901 285900 : pitch_index = abs_pit_enc( 4, limit_flag, T0, T0_frac );
902 :
903 : /* find T0_min and T0_max for delta search */
904 285900 : if ( Opt_AMR_WB )
905 : {
906 4404 : limit_T0( L_FRAME, delta, pit_flag, 0, T0, 0, T0_min, T0_max ); /* T0_frac==0 to keep IO with AMR-WB */
907 : }
908 : }
909 1080432 : else if ( nBits == 8 ) /* absolute encoding with 8 bits */
910 : {
911 17021 : pitch_index = abs_pit_enc( 2, limit_flag, T0, T0_frac );
912 :
913 : /* find T0_min and T0_max for delta search */
914 17021 : if ( Opt_AMR_WB )
915 : {
916 190 : limit_T0( L_FRAME, delta, pit_flag, 0, T0, 0, T0_min, T0_max ); /* T0_frac==0 to keep IO with AMR-WB */
917 : }
918 : }
919 1063411 : else if ( nBits == 6 ) /* relative encoding with 6 bits */
920 : {
921 839482 : pitch_index = delta_pit_enc( 4, T0, T0_frac, *T0_min );
922 : }
923 223929 : else if ( nBits == 5 ) /* relative encoding with 5 bits */
924 : {
925 223929 : if ( delta == 8 )
926 : {
927 33921 : pitch_index = delta_pit_enc( 2, T0, T0_frac, *T0_min );
928 : }
929 : else /* delta == 4 */
930 : {
931 190008 : pitch_index = delta_pit_enc( 4, T0, T0_frac, *T0_min );
932 : }
933 : }
934 : else /* nBits == 4 ) */ /* relative encoding with 4 bits */
935 : {
936 0 : if ( delta == 8 )
937 : {
938 0 : pitch_index = delta_pit_enc( 0, T0, T0_frac, *T0_min );
939 : }
940 : else /* delta == 4 */
941 : {
942 0 : pitch_index = delta_pit_enc( 2, T0, T0_frac, *T0_min );
943 : }
944 : }
945 :
946 1960109 : if ( !Opt_AMR_WB )
947 : {
948 : /* find T0_min and T0_max for delta search */
949 1950661 : limit_T0( L_FRAME, delta, L_SUBFR, limit_flag, T0, T0_frac, T0_min, T0_max );
950 : }
951 :
952 1960109 : push_indice( hBstr, IND_PITCH, pitch_index, nBits );
953 :
954 1960109 : return;
955 : }
956 :
957 : /*-------------------------------------------------------------------*
958 : * pit16k_Q_enc()
959 : *
960 : * Encode subframe pitch lag @16kHz core
961 : *-------------------------------------------------------------------*/
962 :
963 2683211 : void pit16k_Q_enc(
964 : BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle */
965 : const int16_t nBits, /* i : # of Q bits */
966 : const int16_t limit_flag, /* i : restrained(0) or extended(1) Q limits */
967 : const int16_t T0, /* i : integer pitch lag */
968 : const int16_t T0_frac, /* i : pitch fraction */
969 : int16_t *T0_min, /* i/o: delta search min */
970 : int16_t *T0_max /* o : delta search max */
971 : )
972 : {
973 : int16_t pitch_index;
974 :
975 2683211 : if ( nBits == 10 ) /* absolute encoding with 10 bits */
976 : {
977 1125854 : if ( T0 < PIT16k_FR2_EXTEND_10b )
978 : {
979 1104012 : pitch_index = T0 * 4 + T0_frac - ( PIT16k_MIN_EXTEND * 4 );
980 : }
981 : else
982 : {
983 21842 : pitch_index = T0 * 2 + ( T0_frac >> 1 ) - ( PIT16k_FR2_EXTEND_10b * 2 ) + ( ( PIT16k_FR2_EXTEND_10b - PIT16k_MIN_EXTEND ) * 4 );
984 : }
985 :
986 1125854 : push_indice( hBstr, IND_PITCH, pitch_index, nBits );
987 : }
988 1557357 : else if ( nBits == 9 ) /* absolute encoding with 9 bits */
989 : {
990 : {
991 : /*-------------------------------------------------------------------*
992 : * The pitch range is encoded absolutely with 9 bits
993 : * and is divided as follows:
994 : * PIT16k_EXTEND_MIN to PIT16k_FR2_EXTEND_9b-1 resolution 1/4 (frac = 0,1,2 or 3)
995 : * PIT16k_FR2_EXTEND_9b to PIT16k_FR1_EXTEND_9b-1 resolution 1/2 (frac = 0 or 2)
996 : * PIT16k_FR1_EXTEND_9b to PIT16k_MAX resolution 1 (frac = 0)
997 : *-------------------------------------------------------------------*/
998 :
999 0 : if ( T0 < PIT16k_FR2_EXTEND_9b )
1000 : {
1001 0 : pitch_index = T0 * 4 + T0_frac - ( PIT16k_MIN_EXTEND * 4 );
1002 : }
1003 0 : else if ( T0 < PIT16k_FR1_EXTEND_9b )
1004 : {
1005 0 : pitch_index = T0 * 2 + ( T0_frac >> 1 ) - ( PIT16k_FR2_EXTEND_9b * 2 ) + ( ( PIT16k_FR2_EXTEND_9b - PIT16k_MIN_EXTEND ) * 4 );
1006 : }
1007 : else
1008 : {
1009 0 : pitch_index = T0 - PIT16k_FR1_EXTEND_9b + ( ( PIT16k_FR2_EXTEND_9b - PIT16k_MIN_EXTEND ) * 4 ) + ( ( PIT16k_FR1_EXTEND_9b - PIT16k_FR2_EXTEND_9b ) * 2 );
1010 : }
1011 : }
1012 :
1013 0 : push_indice( hBstr, IND_PITCH, pitch_index, 9 );
1014 : }
1015 : else /* nBits == 6 */ /* relative encoding with 6 bits */
1016 : {
1017 1557357 : pitch_index = ( T0 - *T0_min ) * 4 + T0_frac;
1018 :
1019 1557357 : push_indice( hBstr, IND_PITCH, pitch_index, nBits );
1020 : }
1021 :
1022 2683211 : limit_T0( L_FRAME16k, 8, L_SUBFR, limit_flag, T0, T0_frac, T0_min, T0_max );
1023 :
1024 2683211 : return;
1025 : }
1026 :
1027 :
1028 : /*------------------------------------------------------------------*
1029 : * limit_T0_voiced2:
1030 : *
1031 : *
1032 : *------------------------------------------------------------------*/
1033 :
1034 14354 : static void limit_T0_voiced2(
1035 : const int16_t res,
1036 : const int16_t *T_op,
1037 : int16_t *T0_min,
1038 : int16_t *T0_min_frac,
1039 : int16_t *T0_max,
1040 : int16_t *T0_max_frac,
1041 : const int16_t pit_min,
1042 : const int16_t pit_max,
1043 : const int16_t i_subfr )
1044 : {
1045 : int16_t t, temp1, temp2;
1046 :
1047 : /* Lower-bound */
1048 14354 : if ( i_subfr == 0 )
1049 : {
1050 9125 : temp1 = ( T_op[0] * res ) - 32;
1051 : }
1052 : else
1053 : {
1054 5229 : temp1 = ( T_op[1] * res ) - 32;
1055 : }
1056 :
1057 14354 : if ( T_op[0] < T_op[1] )
1058 : {
1059 4652 : t = ( T_op[0] * res ) - 16;
1060 : }
1061 : else
1062 : {
1063 9702 : t = ( T_op[1] * res ) - 16;
1064 : }
1065 :
1066 14354 : if ( temp1 < t )
1067 : {
1068 12050 : temp1 = t;
1069 : }
1070 :
1071 14354 : temp2 = temp1 / res;
1072 14354 : *T0_min = temp2;
1073 14354 : *T0_min_frac = temp1 - temp2 * res;
1074 :
1075 14354 : if ( *T0_min < pit_min )
1076 : {
1077 450 : *T0_min = pit_min;
1078 450 : *T0_min_frac = 0;
1079 : }
1080 :
1081 : /* Higher-bound */
1082 14354 : temp1 = ( *T0_min * res ) + *T0_min_frac + 64 - 1;
1083 :
1084 14354 : if ( T_op[0] < T_op[1] )
1085 : {
1086 4652 : t = ( T_op[1] * res ) + 16 + res - 1;
1087 : }
1088 : else
1089 : {
1090 9702 : t = ( T_op[0] * res ) + 16 + res - 1;
1091 : }
1092 :
1093 14354 : if ( temp1 > t )
1094 : {
1095 12320 : temp1 = t;
1096 : }
1097 :
1098 14354 : temp2 = temp1 / res;
1099 14354 : *T0_max = temp2;
1100 14354 : *T0_max_frac = temp1 - temp2 * res;
1101 :
1102 14354 : if ( *T0_max > pit_max )
1103 : {
1104 194 : *T0_max = pit_max;
1105 194 : *T0_max_frac = res - 1;
1106 194 : temp1 = ( *T0_max * res ) - 64 + res;
1107 194 : temp2 = temp1 / res;
1108 194 : *T0_min = temp2;
1109 194 : *T0_min_frac = temp1 - temp2 * res;
1110 : }
1111 :
1112 14354 : return;
1113 : }
1114 :
1115 :
1116 : /*------------------------------------------------------------------*
1117 : * Mode2_pit_encode:
1118 : *
1119 : * Close-loop pitch lag search and pitch lag quantization
1120 : * Adaptive excitation construction
1121 : *------------------------------------------------------------------*/
1122 :
1123 54366 : void Mode2_pit_encode(
1124 : const int16_t coder_type, /* i : coding model */
1125 : const int16_t i_subfr, /* i : subframe index */
1126 : int16_t **pt_indice, /* i/o: quantization indices pointer */
1127 : float *exc, /* i/o: pointer to excitation signal frame */
1128 : const int16_t *T_op, /* i : open loop pitch estimates in current frame */
1129 : int16_t *T0_min, /* i/o: lower limit for close-loop search */
1130 : int16_t *T0_min_frac, /* i/o: lower limit for close-loop search */
1131 : int16_t *T0_max, /* i/o: higher limit for close-loop search */
1132 : int16_t *T0_max_frac, /* i/o: higher limit for close-loop search */
1133 : int16_t *T0, /* i/o: close loop integer pitch */
1134 : int16_t *T0_frac, /* i/o: close loop fractional part of the pitch */
1135 : int16_t *T0_res, /* i/o: close loop pitch resolution */
1136 : float *h1, /* i : weighted filter impulse response */
1137 : float *xn, /* i : target vector */
1138 : const int16_t pit_min,
1139 : const int16_t pit_fr1,
1140 : const int16_t pit_fr1b,
1141 : const int16_t pit_fr2,
1142 : const int16_t pit_max,
1143 : const int16_t pit_res_max )
1144 : {
1145 : int16_t pit_flag;
1146 :
1147 : /* Pitch flag */
1148 54366 : pit_flag = i_subfr;
1149 :
1150 54366 : if ( i_subfr == ( 2 * L_SUBFR ) )
1151 : {
1152 11311 : pit_flag = 0;
1153 : }
1154 :
1155 : /*-----------------------------------------------------------------*
1156 : * - Limit range of pitch search
1157 : * - Fractional pitch search
1158 : * - Pitch quantization
1159 : *-----------------------------------------------------------------*/
1160 :
1161 54366 : if ( coder_type == 0 ) /*Unvoiced Coding do nothing*/
1162 : {
1163 0 : *T0 = L_SUBFR;
1164 0 : *T0_frac = 0;
1165 0 : *T0_res = 1;
1166 : }
1167 54366 : else if ( coder_type == 1 ) /* 8/4/4/4 (EVS) */
1168 : {
1169 1700 : if ( i_subfr == 0 )
1170 : {
1171 425 : limit_T0_voiced( 4, pit_res_max >> 1, T_op[0], 0, 1, T0_min, T0_min_frac, T0_max, T0_max_frac, pit_min, pit_max );
1172 : }
1173 : else
1174 : {
1175 1275 : limit_T0_voiced( 4, pit_res_max >> 1, *T0, *T0_frac, *T0_res, T0_min, T0_min_frac, T0_max, T0_max_frac, pit_min, pit_max );
1176 : }
1177 :
1178 1700 : *T0 = E_GAIN_closed_loop_search( exc, xn, h1, *T0_min, *T0_min_frac, *T0_max, *T0_max_frac, pit_res_max >> 1, T0_frac, T0_res, pit_res_max, i_subfr, pit_min, pit_min, pit_fr1b, L_SUBFR );
1179 :
1180 1700 : if ( i_subfr == 0 )
1181 : {
1182 425 : Mode2_abs_pit_enc( *T0, *T0_frac, pt_indice, pit_min, pit_fr1b, pit_min, pit_res_max );
1183 : }
1184 : else
1185 : {
1186 1275 : Mode2_delta_pit_enc( *T0, *T0_frac, ( pit_res_max >> 1 ), *T0_min, *T0_min_frac, pt_indice );
1187 : }
1188 : }
1189 52666 : else if ( coder_type == 2 ) /* 8/5/8/5 (EVS) */
1190 : {
1191 :
1192 7044 : if ( i_subfr == 0 )
1193 : {
1194 1761 : limit_T0_voiced( 5, pit_res_max >> 1, T_op[0], 0, 1, T0_min, T0_min_frac, T0_max, T0_max_frac, pit_min, pit_max );
1195 : }
1196 5283 : else if ( i_subfr == 2 * L_SUBFR )
1197 : {
1198 1761 : limit_T0_voiced( 5, pit_res_max >> 1, T_op[1], 0, 1, T0_min, T0_min_frac, T0_max, T0_max_frac, pit_min, pit_max );
1199 : }
1200 : else
1201 : {
1202 3522 : limit_T0_voiced( 5, pit_res_max >> 1, *T0, *T0_frac, *T0_res, T0_min, T0_min_frac, T0_max, T0_max_frac, pit_min, pit_max );
1203 : }
1204 :
1205 7044 : *T0 = E_GAIN_closed_loop_search( exc, xn, h1, *T0_min, *T0_min_frac, *T0_max, *T0_max_frac, pit_res_max >> 1, T0_frac, T0_res, pit_res_max, pit_flag, pit_min, pit_min, pit_fr1b, L_SUBFR );
1206 :
1207 7044 : if ( pit_flag == 0 )
1208 : {
1209 3522 : Mode2_abs_pit_enc( *T0, *T0_frac, pt_indice, pit_min, pit_fr1b, pit_min, pit_res_max );
1210 : }
1211 : else
1212 : {
1213 3522 : Mode2_delta_pit_enc( *T0, *T0_frac, ( pit_res_max >> 1 ), *T0_min, *T0_min_frac, pt_indice );
1214 : }
1215 : }
1216 45622 : else if ( coder_type == 3 ) /* 9/6/6/6 (HRs- VC) */
1217 : {
1218 19480 : int16_t pit_res_max2 = pit_res_max;
1219 :
1220 19480 : if ( pit_min == PIT_MIN_16k )
1221 : {
1222 :
1223 19480 : pit_res_max2 = pit_res_max >> 1;
1224 : }
1225 :
1226 19480 : if ( i_subfr == 0 )
1227 : {
1228 :
1229 3896 : limit_T0_voiced2( pit_res_max2, T_op, T0_min, T0_min_frac, T0_max, T0_max_frac, pit_min, pit_max, i_subfr );
1230 : }
1231 : else
1232 : {
1233 15584 : limit_T0_voiced( 6, pit_res_max2, *T0, 0, 1, T0_min, T0_min_frac, T0_max, T0_max_frac, pit_min, pit_max );
1234 : }
1235 :
1236 19480 : *T0 = E_GAIN_closed_loop_search( exc, xn, h1, *T0_min, *T0_min_frac, *T0_max, *T0_max_frac, pit_res_max2, T0_frac, T0_res, pit_res_max, i_subfr, pit_min, pit_fr2, pit_fr1, L_SUBFR );
1237 :
1238 19480 : if ( i_subfr == 0 ) /* if 1st subframe */
1239 : {
1240 3896 : Mode2_abs_pit_enc( *T0, *T0_frac, pt_indice, pit_min, pit_fr1, pit_fr2, pit_res_max );
1241 : }
1242 : else
1243 : {
1244 15584 : Mode2_delta_pit_enc( *T0, *T0_frac, pit_res_max2, *T0_min, *T0_min_frac, pt_indice );
1245 : }
1246 : }
1247 26142 : else if ( coder_type == 4 ) /* 9/6/9/6 (AMRWB) */
1248 : {
1249 26142 : int16_t pit_res_max2 = pit_res_max;
1250 :
1251 26142 : if ( pit_min == PIT_MIN_16k )
1252 : {
1253 :
1254 26130 : pit_res_max2 = pit_res_max >> 1;
1255 : }
1256 :
1257 :
1258 26142 : if ( ( i_subfr == 0 ) || ( i_subfr == 2 * L_SUBFR ) )
1259 : {
1260 :
1261 10458 : limit_T0_voiced2( pit_res_max2, T_op, T0_min, T0_min_frac, T0_max, T0_max_frac, pit_min, pit_max, i_subfr );
1262 : }
1263 : else
1264 : {
1265 15684 : limit_T0_voiced( 6, pit_res_max2, *T0, 0, 1, T0_min, T0_min_frac, T0_max, T0_max_frac, pit_min, pit_max );
1266 : }
1267 :
1268 26142 : *T0 = E_GAIN_closed_loop_search( exc, xn, h1, *T0_min, *T0_min_frac, *T0_max, *T0_max_frac, pit_res_max2, T0_frac, T0_res, pit_res_max, pit_flag, pit_min, pit_fr2, pit_fr1, L_SUBFR );
1269 :
1270 26142 : if ( pit_flag == 0 ) /* if 1st/3rd/5th subframe */
1271 : {
1272 10458 : Mode2_abs_pit_enc( *T0, *T0_frac, pt_indice, pit_min, pit_fr1, pit_fr2, pit_res_max );
1273 : }
1274 : else /* if subframe 2 or 4 */
1275 : {
1276 15684 : Mode2_delta_pit_enc( *T0, *T0_frac, pit_res_max2, *T0_min, *T0_min_frac, pt_indice );
1277 : }
1278 : }
1279 0 : else if ( coder_type == 8 ) /* 8/5/5/5 (RF all pred mode) */
1280 : {
1281 0 : if ( i_subfr == 0 )
1282 : {
1283 0 : limit_T0_voiced( 5, pit_res_max >> 1, T_op[0], 0, 1, T0_min, T0_min_frac, T0_max, T0_max_frac, pit_min, pit_max );
1284 : }
1285 : else
1286 : {
1287 0 : limit_T0_voiced( 5, pit_res_max >> 1, *T0, *T0_frac, *T0_res, T0_min, T0_min_frac, T0_max, T0_max_frac, pit_min, pit_max );
1288 : }
1289 0 : *T0 = E_GAIN_closed_loop_search( exc, xn, h1, *T0_min, *T0_min_frac, *T0_max, *T0_max_frac, pit_res_max >> 1, T0_frac, T0_res, pit_res_max, i_subfr, pit_min, pit_min, pit_fr1b, L_SUBFR );
1290 :
1291 0 : if ( i_subfr == 0 )
1292 : {
1293 0 : Mode2_abs_pit_enc( *T0, *T0_frac, pt_indice, pit_min, pit_fr1b, pit_min, pit_res_max );
1294 : }
1295 : else
1296 : {
1297 0 : Mode2_delta_pit_enc( *T0, *T0_frac, ( pit_res_max >> 1 ), *T0_min, *T0_min_frac, pt_indice );
1298 : }
1299 : }
1300 0 : else if ( coder_type == 9 ) /* 8/0/8/0 (RF mode Gen pred) */
1301 : {
1302 0 : if ( i_subfr == 0 )
1303 : {
1304 0 : limit_T0_voiced( 4, pit_res_max >> 1, T_op[0], 0, 1, T0_min, T0_min_frac, T0_max, T0_max_frac, pit_min, pit_max );
1305 : }
1306 : else
1307 : {
1308 0 : limit_T0_voiced( 4, pit_res_max >> 1, *T0, *T0_frac, *T0_res, T0_min, T0_min_frac, T0_max, T0_max_frac, pit_min, pit_max );
1309 : }
1310 0 : *T0 = E_GAIN_closed_loop_search( exc, xn, h1, *T0_min, *T0_min_frac, *T0_max, *T0_max_frac, pit_res_max >> 1, T0_frac, T0_res, pit_res_max, i_subfr, pit_min, pit_min, pit_fr1b, L_SUBFR );
1311 :
1312 0 : if ( i_subfr == 0 )
1313 : {
1314 0 : Mode2_abs_pit_enc( *T0, *T0_frac, pt_indice, pit_min, pit_fr1b, pit_min, pit_res_max );
1315 : }
1316 : else
1317 : {
1318 0 : Mode2_delta_pit_enc( *T0, *T0_frac, ( pit_res_max >> 1 ), *T0_min, *T0_min_frac, pt_indice );
1319 : }
1320 : }
1321 : else
1322 : {
1323 0 : assert( 0 );
1324 : }
1325 :
1326 54366 : return;
1327 : }
1328 :
1329 :
1330 : /*-------------------------------------------------------------------*
1331 : * Mode2_abs_pit_enc:
1332 : *
1333 : * Encode pitch lag absolutely
1334 : *-------------------------------------------------------------------*/
1335 :
1336 18301 : void Mode2_abs_pit_enc(
1337 : const int16_t T0, /* i : integer pitch lag */
1338 : const int16_t T0_frac, /* i : pitch fraction */
1339 : int16_t **pt_indice, /* i/o: pointer to Vector of Q indexes */
1340 : const int16_t pit_min,
1341 : const int16_t pit_fr1,
1342 : const int16_t pit_fr2,
1343 : const int16_t pit_res_max )
1344 : {
1345 : int16_t pit_res_max_half;
1346 :
1347 18301 : pit_res_max_half = pit_res_max >> 1;
1348 :
1349 18301 : if ( T0 < pit_fr2 )
1350 : {
1351 5 : **pt_indice = T0 * pit_res_max + T0_frac - ( pit_min * pit_res_max );
1352 : }
1353 18296 : else if ( T0 < pit_fr1 )
1354 : {
1355 13531 : **pt_indice = T0 * pit_res_max_half + T0_frac - ( pit_fr2 * pit_res_max_half ) + ( ( pit_fr2 - pit_min ) * pit_res_max );
1356 : }
1357 : else
1358 : {
1359 4765 : **pt_indice = T0 - pit_fr1 + ( ( pit_fr2 - pit_min ) * pit_res_max ) + ( ( pit_fr1 - pit_fr2 ) * pit_res_max_half );
1360 : }
1361 :
1362 18301 : ( *pt_indice )++;
1363 :
1364 18301 : return;
1365 : }
1366 :
1367 :
1368 : /*-------------------------------------------------------------------*
1369 : * Mode2_delta_pit_enc:
1370 : *
1371 : * Encode pitch lag differentially
1372 : *-------------------------------------------------------------------*/
1373 :
1374 36065 : void Mode2_delta_pit_enc(
1375 : const int16_t T0, /* i : integer pitch lag */
1376 : const int16_t T0_frac, /* i : pitch fraction */
1377 : const int16_t T0_res, /* i : pitch resolution */
1378 : const int16_t T0_min, /* i : delta search min */
1379 : const int16_t T0_min_frac, /* i : delta search min */
1380 : int16_t **pt_indice /* o : pointer to Vector of Q indexes */
1381 : )
1382 : {
1383 :
1384 36065 : **pt_indice = ( T0 - T0_min ) * T0_res + T0_frac - T0_min_frac;
1385 :
1386 36065 : ( *pt_indice )++;
1387 :
1388 36065 : return;
1389 : }
|