Line data Source code
1 : /******************************************************************************************************
2 :
3 : (C) 2022-2025 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 : #ifdef FIX_2273_OOB_INDEXING_IN_PIT_FR4
447 : int16_t corr_off;
448 : #endif
449 :
450 : /* initialization */
451 4879894 : if ( limit_flag == 0 )
452 : {
453 641005 : if ( L_frame == L_FRAME )
454 : {
455 574353 : pit_min = PIT_MIN;
456 : }
457 : else /* L_frame == L_FRAME16k */
458 : {
459 66652 : pit_min = PIT16k_MIN;
460 : }
461 : }
462 : else
463 : {
464 4238889 : if ( L_frame == L_FRAME )
465 : {
466 1456767 : pit_min = PIT_MIN_EXTEND;
467 1456767 : if ( limit_flag == 2 )
468 : {
469 253344 : pit_min = PIT_MIN_DOUBLEEXTEND;
470 : }
471 : }
472 : else /* L_frame == L_FRAME16k */
473 : {
474 2782122 : pit_min = PIT16k_MIN_EXTEND;
475 : }
476 : }
477 :
478 :
479 : /*-----------------------------------------------------------------*
480 : * - Find interval to compute normalized correlation
481 : * - allocate memory to normalized correlation vector
482 : * - Compute normalized correlation between target and filtered
483 : * excitation
484 : *-----------------------------------------------------------------*/
485 :
486 4879894 : t_min = t0_min - L_INTERPOL1;
487 4879894 : t_max = t0_max + L_INTERPOL1;
488 :
489 : #ifndef FIX_2273_OOB_INDEXING_IN_PIT_FR4
490 : corr = &corr_v[0] - t_min; /* corr[t_min..t_max] */
491 : #else
492 4879894 : corr = corr_v;
493 4879894 : corr_off = -t_min;
494 : #endif
495 :
496 : #ifndef FIX_2273_OOB_INDEXING_IN_PIT_FR4
497 : norm_corr( exc, xn, h, t_min, t_max, corr, L_subfr );
498 : #else
499 4879894 : norm_corr( exc, xn, h, t_min, t_max, corr + corr_off, L_subfr );
500 : #endif
501 :
502 : /*-----------------------------------------------------------------*
503 : * Find integer pitch
504 : *-----------------------------------------------------------------*/
505 :
506 : #ifndef FIX_2273_OOB_INDEXING_IN_PIT_FR4
507 : max_val = corr[t0_min];
508 : #else
509 4879894 : max_val = corr[t0_min + corr_off];
510 : #endif
511 4879894 : t0 = t0_min;
512 :
513 74520568 : for ( i = t0_min + 1; i <= t0_max; i++ )
514 : {
515 : #ifndef FIX_2273_OOB_INDEXING_IN_PIT_FR4
516 : if ( corr[i] >= max_val )
517 : {
518 : max_val = corr[i];
519 : t0 = i;
520 : }
521 : #else
522 69640674 : if ( corr[i + corr_off] >= max_val )
523 : {
524 26966446 : max_val = corr[i + corr_off];
525 26966446 : t0 = i;
526 : }
527 : #endif
528 : }
529 :
530 4879894 : if ( t0_fr1 == pit_min )
531 : {
532 : /* don't search fraction (for 7b/4b quant) */
533 5972 : if ( ( i_subfr == 0 ) && ( t0 >= t0_fr2 ) )
534 : {
535 0 : i = ( t0 >> 1 ) * 2; /* 2 samples resolution */
536 0 : if ( ( i + 2 ) > PIT_MAX )
537 : {
538 0 : i -= 2;
539 : }
540 : #ifndef FIX_2273_OOB_INDEXING_IN_PIT_FR4
541 : if ( corr[i] > corr[i + 2] )
542 : #else
543 0 : if ( corr[i + corr_off] > corr[i + 2 + corr_off] )
544 : #endif
545 : {
546 0 : t0 = i;
547 : }
548 : else
549 : {
550 0 : t0 = i + 2;
551 : }
552 : }
553 :
554 5972 : *pit_frac = 0;
555 :
556 5972 : return ( t0 );
557 : }
558 4873922 : if ( ( i_subfr == 0 ) && ( t0 >= t0_fr1 ) )
559 : {
560 89137 : *pit_frac = 0;
561 :
562 89137 : return ( t0 );
563 : }
564 :
565 : /*------------------------------------------------------------------*
566 : * Search fractionnal pitch with 1/4 subsample resolution.
567 : * search the fractions around t0 and choose the one which maximizes
568 : * the interpolated normalized correlation.
569 : *-----------------------------------------------------------------*/
570 :
571 4784785 : t1 = t0;
572 4784785 : step = 1; /* 1/4 subsample resolution */
573 4784785 : fraction = 1;
574 4784785 : if ( ( ( i_subfr == 0 ) && ( t0 >= t0_fr2 ) ) || ( t0_fr2 == pit_min ) )
575 : {
576 143267 : step = 2; /* 1/2 subsample resolution */
577 143267 : fraction = 2;
578 : }
579 :
580 4784785 : if ( t0 == t0_min ) /* Limit case */
581 : {
582 300625 : fraction = 0;
583 : #ifndef FIX_2273_OOB_INDEXING_IN_PIT_FR4
584 : cor_max = interpolation( &corr[t0], E_ROM_inter4_1, fraction, PIT_UP_SAMP, 4 );
585 : #else
586 300625 : cor_max = interpolation( &corr[t0 + corr_off], E_ROM_inter4_1, fraction, PIT_UP_SAMP, 4 );
587 : #endif
588 : }
589 : else /* Process negative fractions */
590 : {
591 4484160 : t0--;
592 : #ifndef FIX_2273_OOB_INDEXING_IN_PIT_FR4
593 : cor_max = interpolation( &corr[t0], E_ROM_inter4_1, fraction, PIT_UP_SAMP, 4 );
594 : #else
595 4484160 : cor_max = interpolation( &corr[t0 + corr_off], E_ROM_inter4_1, fraction, PIT_UP_SAMP, 4 );
596 : #endif
597 13194046 : for ( i = ( fraction + step ); i <= 3; i = i + step )
598 : {
599 : #ifndef FIX_2273_OOB_INDEXING_IN_PIT_FR4
600 : temp = interpolation( &corr[t0], E_ROM_inter4_1, i, PIT_UP_SAMP, 4 );
601 : #else
602 8709886 : temp = interpolation( &corr[t0 + corr_off], E_ROM_inter4_1, i, PIT_UP_SAMP, 4 );
603 : #endif
604 8709886 : if ( temp > cor_max )
605 : {
606 8113534 : cor_max = temp;
607 8113534 : fraction = i;
608 : }
609 : }
610 : }
611 :
612 23637391 : for ( i = 0; i <= 3; i = i + step ) /* Process positive fractions */
613 : {
614 : #ifndef FIX_2273_OOB_INDEXING_IN_PIT_FR4
615 : temp = interpolation( &corr[t1], E_ROM_inter4_1, i, PIT_UP_SAMP, 4 );
616 : #else
617 18852606 : temp = interpolation( &corr[t1 + corr_off], E_ROM_inter4_1, i, PIT_UP_SAMP, 4 );
618 : #endif
619 18852606 : if ( temp > cor_max )
620 : {
621 5548082 : cor_max = temp;
622 5548082 : fraction = i;
623 5548082 : t0 = t1;
624 : }
625 : }
626 :
627 4784785 : *pit_frac = fraction;
628 :
629 4784785 : return ( t0 );
630 : }
631 :
632 : /*-------------------------------------------------------------------*
633 : * norm_corr()
634 : *
635 : * Find the normalized correlation between the target vector and the
636 : * filtered past excitation (correlation between target and filtered
637 : * excitation divided by the square root of energy of filtered
638 : * excitation)
639 : *---------------------------------------------------------------------*/
640 :
641 4934260 : void norm_corr(
642 : const float exc[], /* i : excitation buffer */
643 : const float xn[], /* i : target signal */
644 : const float h[], /* i : weighted synthesis filter impulse response */
645 : const int16_t t_min, /* i : minimum value of searched range */
646 : const int16_t t_max, /* i : maximum value of searched range */
647 : float corr_norm[], /* o : normalized correlation */
648 : const int16_t L_subfr /* i : subframe size */
649 : )
650 : {
651 : int16_t t, j, k;
652 : float excf[L_FRAME16k]; /* filtered past excitation */ /* length up to L_FRAME in GSC */
653 : float alp, ps, norm;
654 :
655 4934260 : k = -t_min;
656 :
657 : /*-----------------------------------------------------------------*
658 : * compute the filtered excitation for the first delay t_min
659 : *-----------------------------------------------------------------*/
660 :
661 4934260 : conv( &exc[k], h, excf, L_subfr );
662 :
663 : /*-----------------------------------------------------------------*
664 : * loop for every possible period
665 : *-----------------------------------------------------------------*/
666 :
667 119932482 : for ( t = t_min; t <= t_max; t++ )
668 : {
669 : /* Compute correlation between xn[] and excf[] */
670 114998222 : ps = 0.0f;
671 7572708174 : for ( j = 0; j < L_subfr; ++j )
672 : {
673 7457709952 : ps += xn[j] * excf[j];
674 : }
675 :
676 : /* Compute 1/sqrt(energie of excf[]) */
677 114998222 : alp = 0.01f;
678 7572708174 : for ( j = 0; j < L_subfr; ++j )
679 : {
680 7457709952 : alp += excf[j] * excf[j];
681 : }
682 114998222 : norm = inv_sqrt( alp );
683 :
684 : /* Normalize correlation = correlation * (1/sqrt(energie)) */
685 114998222 : corr_norm[t] = ps * norm;
686 :
687 : /* update the filtered excitation excf[] for the next iteration */
688 114998222 : if ( t != t_max )
689 : {
690 110063962 : k--;
691 7135803328 : for ( j = L_subfr - 1; j > 0; j-- )
692 : {
693 7025739366 : excf[j] = excf[j - 1] + exc[k] * h[j];
694 : }
695 110063962 : excf[0] = exc[k];
696 : }
697 : }
698 4934260 : return;
699 : }
700 :
701 : /*-------------------------------------------------------------------*
702 : * abs_pit_enc()
703 : *
704 : * Encode pitch lag absolutely with resolution for shortest pitches
705 : * depending on parameter 'fr_step':
706 : * fr_step = 2: pitch range encoded with 8 bits
707 : * fr_step = 4: pitch range encoded with 9 bits
708 : *-------------------------------------------------------------------*/
709 :
710 : /*! r: pitch index */
711 322043 : int16_t abs_pit_enc(
712 : const int16_t fr_steps, /* i : fractional resolution step */
713 : const int16_t limit_flag, /* i : restrained(0) or extended(1) limits */
714 : const int16_t T0, /* i : integer pitch lag */
715 : const int16_t T0_frac /* i : pitch fraction */
716 : )
717 : {
718 : int16_t pitch_index;
719 :
720 322043 : if ( limit_flag == 0 )
721 : {
722 240947 : if ( fr_steps == 2 )
723 : {
724 : /*-----------------------------------------------------------------*
725 : * The pitch range is encoded absolutely with 8 bits
726 : * and is divided as follows:
727 : * PIT_MIN to PIT_FR1_8b-1 resolution 1/2 (frac = 0 or 2)
728 : * PIT_FR1_8b to PIT_MAX resolution 1 (frac = 0)
729 : *-----------------------------------------------------------------*/
730 13645 : if ( T0 < PIT_FR1_8b )
731 : {
732 7063 : pitch_index = T0 * 2 + ( T0_frac >> 1 ) - ( PIT_MIN * 2 );
733 : }
734 : else
735 : {
736 6582 : pitch_index = T0 - PIT_FR1_8b + ( ( PIT_FR1_8b - PIT_MIN ) * 2 );
737 : }
738 : }
739 227302 : else if ( fr_steps == 4 )
740 : {
741 : /*-------------------------------------------------------------------*
742 : * The pitch range is encoded absolutely with 9 bits
743 : * and is divided as follows:
744 : * PIT_MIN to PIT_FR2_9b-1 resolution 1/4 (frac = 0,1,2 or 3)
745 : * PIT_FR2_9b to PIT_FR1_9b-1 resolution 1/2 (frac = 0 or 2)
746 : * PIT_FR1_9b to PIT_MAX resolution 1 (frac = 0)
747 : *-------------------------------------------------------------------*/
748 227302 : if ( T0 < PIT_FR2_9b )
749 : {
750 176005 : pitch_index = T0 * 4 + T0_frac - ( PIT_MIN * 4 );
751 : }
752 51297 : else if ( T0 < PIT_FR1_9b )
753 : {
754 27820 : pitch_index = T0 * 2 + ( T0_frac >> 1 ) - ( PIT_FR2_9b * 2 ) + ( ( PIT_FR2_9b - PIT_MIN ) * 4 );
755 : }
756 : else
757 : {
758 23477 : pitch_index = T0 - PIT_FR1_9b + ( ( PIT_FR2_9b - PIT_MIN ) * 4 ) + ( ( PIT_FR1_9b - PIT_FR2_9b ) * 2 );
759 : }
760 : }
761 : else /* fr_step == 0 */
762 : {
763 : /* not used in the codec */
764 0 : pitch_index = 0;
765 : }
766 : }
767 81096 : else if ( limit_flag == 1 ) /* extended Q range */
768 : {
769 78578 : if ( fr_steps == 2 )
770 : {
771 : /*-----------------------------------------------------------------*
772 : * The pitch range is encoded absolutely with 8 bits
773 : * and is divided as follows:
774 : * PIT_MIN_EXTEND to PIT_FR1_EXTEND_8b-1 resolution 1/2 (frac = 0 or 2)
775 : * PIT_FR1_EXTEND_8b to PIT_MAX resolution 1 (frac = 0)
776 : *-----------------------------------------------------------------*/
777 :
778 3477 : if ( T0 < PIT_FR1_EXTEND_8b )
779 : {
780 2113 : pitch_index = T0 * 2 + ( T0_frac >> 1 ) - ( PIT_MIN_EXTEND * 2 );
781 : }
782 : else
783 : {
784 1364 : pitch_index = T0 - PIT_FR1_EXTEND_8b + ( ( PIT_FR1_EXTEND_8b - PIT_MIN_EXTEND ) * 2 );
785 : }
786 : }
787 75101 : else if ( fr_steps == 4 )
788 : {
789 : /*-------------------------------------------------------------------*
790 : * The pitch range is encoded absolutely with 9 bits
791 : * and is divided as follows:
792 : * PIT_MIN_EXTEND to PIT_FR2__EXTEND9b-1 resolution 1/4 (frac = 0,1,2 or 3)
793 : * PIT_FR2_EXTEND_9b to PIT_FR1__EXTEND9b-1 resolution 1/2 (frac = 0 or 2)
794 : * PIT_FR1_EXTEND_9b to PIT_MAX resolution 1 (frac = 0)
795 : *-------------------------------------------------------------------*/
796 :
797 75101 : if ( T0 < PIT_FR2_EXTEND_9b )
798 : {
799 64417 : pitch_index = T0 * 4 + T0_frac - ( PIT_MIN_EXTEND * 4 );
800 : }
801 10684 : else if ( T0 < PIT_FR1_EXTEND_9b )
802 : {
803 2030 : pitch_index = T0 * 2 + ( T0_frac >> 1 ) - ( PIT_FR2_EXTEND_9b * 2 ) + ( ( PIT_FR2_EXTEND_9b - PIT_MIN_EXTEND ) * 4 );
804 : }
805 : else
806 : {
807 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 );
808 : }
809 : }
810 : else /* fr_step == 0 */
811 : {
812 : /* not used in the codec */
813 0 : pitch_index = 0;
814 : }
815 : }
816 : else /* double-extended Q range */
817 : {
818 2518 : if ( fr_steps == 2 )
819 : {
820 : /*-----------------------------------------------------------------*
821 : * The pitch range is encoded absolutely with 8 bits
822 : * and is divided as follows:
823 : * PIT_MIN_DOUBLEEXTEND to PIT_FR1_DOUBLEEXTEND_8b-1 resolution 1/2 (frac = 0 or 2)
824 : * PIT_FR1_DOUBLEEXTEND_8b to PIT_MAX resolution 1 (frac = 0)
825 : *-----------------------------------------------------------------*/
826 :
827 0 : if ( T0 < PIT_FR1_DOUBLEEXTEND_8b )
828 : {
829 0 : pitch_index = T0 * 2 + ( T0_frac >> 1 ) - ( PIT_MIN_DOUBLEEXTEND * 2 );
830 : }
831 : else
832 : {
833 0 : pitch_index = T0 - PIT_FR1_DOUBLEEXTEND_8b + ( ( PIT_FR1_DOUBLEEXTEND_8b - PIT_MIN_DOUBLEEXTEND ) * 2 );
834 : }
835 : }
836 2518 : else if ( fr_steps == 4 )
837 : {
838 : /*-------------------------------------------------------------------*
839 : * The pitch range is encoded absolutely with 9 bits
840 : * and is divided as follows:
841 : * PIT_MIN_DOUBLEEXTEND to PIT_FR2_DOUBLEEXTEND9b-1 resolution 1/4 (frac = 0,1,2 or 3)
842 : * PIT_FR2_DOUBLEEXTEND_9b to PIT_FR1_DOOBLEEXTEND9b-1 resolution 1/2 (frac = 0 or 2)
843 : * PIT_FR1_DOUBLEEXTEND_9b to PIT_MAX resolution 1 (frac = 0)
844 : *-------------------------------------------------------------------*/
845 :
846 2518 : if ( T0 < PIT_FR2_DOUBLEEXTEND_9b )
847 : {
848 2343 : pitch_index = T0 * 4 + T0_frac - ( PIT_MIN_DOUBLEEXTEND * 4 );
849 : }
850 175 : else if ( T0 < PIT_FR1_DOUBLEEXTEND_9b )
851 : {
852 60 : pitch_index = T0 * 2 + ( T0_frac >> 1 ) - ( PIT_FR2_DOUBLEEXTEND_9b * 2 ) + ( ( PIT_FR2_DOUBLEEXTEND_9b - PIT_MIN_DOUBLEEXTEND ) * 4 );
853 : }
854 : else
855 : {
856 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 );
857 : }
858 : }
859 : else /* fr_step == 0 */
860 : {
861 : /* not used in the codec */
862 0 : pitch_index = 0;
863 : }
864 : }
865 :
866 322043 : return pitch_index;
867 : }
868 :
869 : /*-------------------------------------------------------------------*
870 : * delta_pit_enc()
871 : *
872 : * Encode pitch lag differentially from T0_min to T0_max
873 : * with resolution depending on parameter 'fr_step':
874 : * fr_step = 0: resolution 1 (frac = 0), or
875 : * fr_step = 2: resolution 1/2 (frac = 0 or 2), or
876 : * fr_step = 4: resolution 1/4 (frac = 0, 1, 2, or 3)
877 : *-------------------------------------------------------------------*/
878 :
879 : /*! r: pitch index */
880 1186893 : int16_t delta_pit_enc(
881 : const int16_t fr_steps, /* i : fractional resolution step */
882 : const int16_t T0, /* i : integer pitch lag */
883 : const int16_t T0_frac, /* i : pitch fraction */
884 : const int16_t T0_min /* i : delta search min */
885 : )
886 : {
887 : int16_t pitch_index;
888 :
889 1186893 : if ( fr_steps == 0 )
890 : {
891 2137 : pitch_index = T0 - T0_min;
892 : }
893 1184756 : else if ( fr_steps == 2 )
894 : {
895 56355 : pitch_index = ( T0 - T0_min ) * 2 + ( T0_frac >> 1 );
896 : }
897 : else /* fr_steps == 4 */
898 : {
899 1128401 : pitch_index = ( T0 - T0_min ) * 4 + T0_frac;
900 : }
901 :
902 1186893 : return pitch_index;
903 : }
904 :
905 : /*-------------------------------------------------------------------*
906 : * pit_Q_enc()
907 : *
908 : * Encode subframe pitch lag
909 : *-------------------------------------------------------------------*/
910 :
911 1960109 : void pit_Q_enc(
912 : BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle */
913 : const int16_t Opt_AMR_WB, /* i : flag indicating AMR-WB IO mode */
914 : const int16_t nBits, /* i : # of Q bits */
915 : const int16_t delta, /* i : Half the CL searched interval */
916 : const int16_t pit_flag, /* i : absolute(0) or delta(1) pitch Q */
917 : const int16_t limit_flag, /* i : restrained(0) or extended(1) Q limits */
918 : const int16_t T0, /* i : integer pitch lag */
919 : const int16_t T0_frac, /* i : pitch fraction */
920 : int16_t *T0_min, /* i/o: delta search min */
921 : int16_t *T0_max /* o : delta search max */
922 : )
923 : {
924 : int16_t pitch_index;
925 :
926 1960109 : if ( nBits == 10 ) /* absolute encoding with 10 bits */
927 : {
928 593777 : if ( limit_flag == 0 )
929 : {
930 0 : pitch_index = T0 * 4 + T0_frac - ( PIT_MIN * 4 );
931 : }
932 593777 : else if ( limit_flag == 1 )
933 : {
934 532959 : pitch_index = T0 * 4 + T0_frac - ( PIT_MIN_EXTEND * 4 );
935 : }
936 : else /* limit_flag == 2 */
937 : {
938 60818 : pitch_index = T0 * 4 + T0_frac - ( PIT_MIN_DOUBLEEXTEND * 4 );
939 : }
940 : }
941 1366332 : else if ( nBits == 9 ) /* absolute encoding with 9 bits */
942 : {
943 285900 : pitch_index = abs_pit_enc( 4, limit_flag, T0, T0_frac );
944 :
945 : /* find T0_min and T0_max for delta search */
946 285900 : if ( Opt_AMR_WB )
947 : {
948 4404 : limit_T0( L_FRAME, delta, pit_flag, 0, T0, 0, T0_min, T0_max ); /* T0_frac==0 to keep IO with AMR-WB */
949 : }
950 : }
951 1080432 : else if ( nBits == 8 ) /* absolute encoding with 8 bits */
952 : {
953 17021 : pitch_index = abs_pit_enc( 2, limit_flag, T0, T0_frac );
954 :
955 : /* find T0_min and T0_max for delta search */
956 17021 : if ( Opt_AMR_WB )
957 : {
958 190 : limit_T0( L_FRAME, delta, pit_flag, 0, T0, 0, T0_min, T0_max ); /* T0_frac==0 to keep IO with AMR-WB */
959 : }
960 : }
961 1063411 : else if ( nBits == 6 ) /* relative encoding with 6 bits */
962 : {
963 839482 : pitch_index = delta_pit_enc( 4, T0, T0_frac, *T0_min );
964 : }
965 223929 : else if ( nBits == 5 ) /* relative encoding with 5 bits */
966 : {
967 223929 : if ( delta == 8 )
968 : {
969 33921 : pitch_index = delta_pit_enc( 2, T0, T0_frac, *T0_min );
970 : }
971 : else /* delta == 4 */
972 : {
973 190008 : pitch_index = delta_pit_enc( 4, T0, T0_frac, *T0_min );
974 : }
975 : }
976 : else /* nBits == 4 ) */ /* relative encoding with 4 bits */
977 : {
978 0 : if ( delta == 8 )
979 : {
980 0 : pitch_index = delta_pit_enc( 0, T0, T0_frac, *T0_min );
981 : }
982 : else /* delta == 4 */
983 : {
984 0 : pitch_index = delta_pit_enc( 2, T0, T0_frac, *T0_min );
985 : }
986 : }
987 :
988 1960109 : if ( !Opt_AMR_WB )
989 : {
990 : /* find T0_min and T0_max for delta search */
991 1950661 : limit_T0( L_FRAME, delta, L_SUBFR, limit_flag, T0, T0_frac, T0_min, T0_max );
992 : }
993 :
994 1960109 : push_indice( hBstr, IND_PITCH, pitch_index, nBits );
995 :
996 1960109 : return;
997 : }
998 :
999 : /*-------------------------------------------------------------------*
1000 : * pit16k_Q_enc()
1001 : *
1002 : * Encode subframe pitch lag @16kHz core
1003 : *-------------------------------------------------------------------*/
1004 :
1005 2683211 : void pit16k_Q_enc(
1006 : BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle */
1007 : const int16_t nBits, /* i : # of Q bits */
1008 : const int16_t limit_flag, /* i : restrained(0) or extended(1) Q limits */
1009 : const int16_t T0, /* i : integer pitch lag */
1010 : const int16_t T0_frac, /* i : pitch fraction */
1011 : int16_t *T0_min, /* i/o: delta search min */
1012 : int16_t *T0_max /* o : delta search max */
1013 : )
1014 : {
1015 : int16_t pitch_index;
1016 :
1017 2683211 : if ( nBits == 10 ) /* absolute encoding with 10 bits */
1018 : {
1019 1125854 : if ( T0 < PIT16k_FR2_EXTEND_10b )
1020 : {
1021 1104012 : pitch_index = T0 * 4 + T0_frac - ( PIT16k_MIN_EXTEND * 4 );
1022 : }
1023 : else
1024 : {
1025 21842 : pitch_index = T0 * 2 + ( T0_frac >> 1 ) - ( PIT16k_FR2_EXTEND_10b * 2 ) + ( ( PIT16k_FR2_EXTEND_10b - PIT16k_MIN_EXTEND ) * 4 );
1026 : }
1027 :
1028 1125854 : push_indice( hBstr, IND_PITCH, pitch_index, nBits );
1029 : }
1030 1557357 : else if ( nBits == 9 ) /* absolute encoding with 9 bits */
1031 : {
1032 : {
1033 : /*-------------------------------------------------------------------*
1034 : * The pitch range is encoded absolutely with 9 bits
1035 : * and is divided as follows:
1036 : * PIT16k_EXTEND_MIN to PIT16k_FR2_EXTEND_9b-1 resolution 1/4 (frac = 0,1,2 or 3)
1037 : * PIT16k_FR2_EXTEND_9b to PIT16k_FR1_EXTEND_9b-1 resolution 1/2 (frac = 0 or 2)
1038 : * PIT16k_FR1_EXTEND_9b to PIT16k_MAX resolution 1 (frac = 0)
1039 : *-------------------------------------------------------------------*/
1040 :
1041 0 : if ( T0 < PIT16k_FR2_EXTEND_9b )
1042 : {
1043 0 : pitch_index = T0 * 4 + T0_frac - ( PIT16k_MIN_EXTEND * 4 );
1044 : }
1045 0 : else if ( T0 < PIT16k_FR1_EXTEND_9b )
1046 : {
1047 0 : pitch_index = T0 * 2 + ( T0_frac >> 1 ) - ( PIT16k_FR2_EXTEND_9b * 2 ) + ( ( PIT16k_FR2_EXTEND_9b - PIT16k_MIN_EXTEND ) * 4 );
1048 : }
1049 : else
1050 : {
1051 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 );
1052 : }
1053 : }
1054 :
1055 0 : push_indice( hBstr, IND_PITCH, pitch_index, 9 );
1056 : }
1057 : else /* nBits == 6 */ /* relative encoding with 6 bits */
1058 : {
1059 1557357 : pitch_index = ( T0 - *T0_min ) * 4 + T0_frac;
1060 :
1061 1557357 : push_indice( hBstr, IND_PITCH, pitch_index, nBits );
1062 : }
1063 :
1064 2683211 : limit_T0( L_FRAME16k, 8, L_SUBFR, limit_flag, T0, T0_frac, T0_min, T0_max );
1065 :
1066 2683211 : return;
1067 : }
1068 :
1069 :
1070 : /*------------------------------------------------------------------*
1071 : * limit_T0_voiced2:
1072 : *
1073 : *
1074 : *------------------------------------------------------------------*/
1075 :
1076 14354 : static void limit_T0_voiced2(
1077 : const int16_t res,
1078 : const int16_t *T_op,
1079 : int16_t *T0_min,
1080 : int16_t *T0_min_frac,
1081 : int16_t *T0_max,
1082 : int16_t *T0_max_frac,
1083 : const int16_t pit_min,
1084 : const int16_t pit_max,
1085 : const int16_t i_subfr )
1086 : {
1087 : int16_t t, temp1, temp2;
1088 :
1089 : /* Lower-bound */
1090 14354 : if ( i_subfr == 0 )
1091 : {
1092 9125 : temp1 = ( T_op[0] * res ) - 32;
1093 : }
1094 : else
1095 : {
1096 5229 : temp1 = ( T_op[1] * res ) - 32;
1097 : }
1098 :
1099 14354 : if ( T_op[0] < T_op[1] )
1100 : {
1101 4652 : t = ( T_op[0] * res ) - 16;
1102 : }
1103 : else
1104 : {
1105 9702 : t = ( T_op[1] * res ) - 16;
1106 : }
1107 :
1108 14354 : if ( temp1 < t )
1109 : {
1110 12050 : temp1 = t;
1111 : }
1112 :
1113 14354 : temp2 = temp1 / res;
1114 14354 : *T0_min = temp2;
1115 14354 : *T0_min_frac = temp1 - temp2 * res;
1116 :
1117 14354 : if ( *T0_min < pit_min )
1118 : {
1119 450 : *T0_min = pit_min;
1120 450 : *T0_min_frac = 0;
1121 : }
1122 :
1123 : /* Higher-bound */
1124 14354 : temp1 = ( *T0_min * res ) + *T0_min_frac + 64 - 1;
1125 :
1126 14354 : if ( T_op[0] < T_op[1] )
1127 : {
1128 4652 : t = ( T_op[1] * res ) + 16 + res - 1;
1129 : }
1130 : else
1131 : {
1132 9702 : t = ( T_op[0] * res ) + 16 + res - 1;
1133 : }
1134 :
1135 14354 : if ( temp1 > t )
1136 : {
1137 12320 : temp1 = t;
1138 : }
1139 :
1140 14354 : temp2 = temp1 / res;
1141 14354 : *T0_max = temp2;
1142 14354 : *T0_max_frac = temp1 - temp2 * res;
1143 :
1144 14354 : if ( *T0_max > pit_max )
1145 : {
1146 194 : *T0_max = pit_max;
1147 194 : *T0_max_frac = res - 1;
1148 194 : temp1 = ( *T0_max * res ) - 64 + res;
1149 194 : temp2 = temp1 / res;
1150 194 : *T0_min = temp2;
1151 194 : *T0_min_frac = temp1 - temp2 * res;
1152 : }
1153 :
1154 14354 : return;
1155 : }
1156 :
1157 :
1158 : /*------------------------------------------------------------------*
1159 : * Mode2_pit_encode:
1160 : *
1161 : * Close-loop pitch lag search and pitch lag quantization
1162 : * Adaptive excitation construction
1163 : *------------------------------------------------------------------*/
1164 :
1165 54366 : void Mode2_pit_encode(
1166 : const int16_t coder_type, /* i : coding model */
1167 : const int16_t i_subfr, /* i : subframe index */
1168 : int16_t **pt_indice, /* i/o: quantization indices pointer */
1169 : float *exc, /* i/o: pointer to excitation signal frame */
1170 : const int16_t *T_op, /* i : open loop pitch estimates in current frame */
1171 : int16_t *T0_min, /* i/o: lower limit for close-loop search */
1172 : int16_t *T0_min_frac, /* i/o: lower limit for close-loop search */
1173 : int16_t *T0_max, /* i/o: higher limit for close-loop search */
1174 : int16_t *T0_max_frac, /* i/o: higher limit for close-loop search */
1175 : int16_t *T0, /* i/o: close loop integer pitch */
1176 : int16_t *T0_frac, /* i/o: close loop fractional part of the pitch */
1177 : int16_t *T0_res, /* i/o: close loop pitch resolution */
1178 : float *h1, /* i : weighted filter impulse response */
1179 : float *xn, /* i : target vector */
1180 : const int16_t pit_min,
1181 : const int16_t pit_fr1,
1182 : const int16_t pit_fr1b,
1183 : const int16_t pit_fr2,
1184 : const int16_t pit_max,
1185 : const int16_t pit_res_max )
1186 : {
1187 : int16_t pit_flag;
1188 :
1189 : /* Pitch flag */
1190 54366 : pit_flag = i_subfr;
1191 :
1192 54366 : if ( i_subfr == ( 2 * L_SUBFR ) )
1193 : {
1194 11311 : pit_flag = 0;
1195 : }
1196 :
1197 : /*-----------------------------------------------------------------*
1198 : * - Limit range of pitch search
1199 : * - Fractional pitch search
1200 : * - Pitch quantization
1201 : *-----------------------------------------------------------------*/
1202 :
1203 54366 : if ( coder_type == 0 ) /*Unvoiced Coding do nothing*/
1204 : {
1205 0 : *T0 = L_SUBFR;
1206 0 : *T0_frac = 0;
1207 0 : *T0_res = 1;
1208 : }
1209 54366 : else if ( coder_type == 1 ) /* 8/4/4/4 (EVS) */
1210 : {
1211 1700 : if ( i_subfr == 0 )
1212 : {
1213 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 );
1214 : }
1215 : else
1216 : {
1217 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 );
1218 : }
1219 :
1220 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 );
1221 :
1222 1700 : if ( i_subfr == 0 )
1223 : {
1224 425 : Mode2_abs_pit_enc( *T0, *T0_frac, pt_indice, pit_min, pit_fr1b, pit_min, pit_res_max );
1225 : }
1226 : else
1227 : {
1228 1275 : Mode2_delta_pit_enc( *T0, *T0_frac, ( pit_res_max >> 1 ), *T0_min, *T0_min_frac, pt_indice );
1229 : }
1230 : }
1231 52666 : else if ( coder_type == 2 ) /* 8/5/8/5 (EVS) */
1232 : {
1233 :
1234 7044 : if ( i_subfr == 0 )
1235 : {
1236 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 );
1237 : }
1238 5283 : else if ( i_subfr == 2 * L_SUBFR )
1239 : {
1240 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 );
1241 : }
1242 : else
1243 : {
1244 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 );
1245 : }
1246 :
1247 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 );
1248 :
1249 7044 : if ( pit_flag == 0 )
1250 : {
1251 3522 : Mode2_abs_pit_enc( *T0, *T0_frac, pt_indice, pit_min, pit_fr1b, pit_min, pit_res_max );
1252 : }
1253 : else
1254 : {
1255 3522 : Mode2_delta_pit_enc( *T0, *T0_frac, ( pit_res_max >> 1 ), *T0_min, *T0_min_frac, pt_indice );
1256 : }
1257 : }
1258 45622 : else if ( coder_type == 3 ) /* 9/6/6/6 (HRs- VC) */
1259 : {
1260 19480 : int16_t pit_res_max2 = pit_res_max;
1261 :
1262 19480 : if ( pit_min == PIT_MIN_16k )
1263 : {
1264 :
1265 19480 : pit_res_max2 = pit_res_max >> 1;
1266 : }
1267 :
1268 19480 : if ( i_subfr == 0 )
1269 : {
1270 :
1271 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 );
1272 : }
1273 : else
1274 : {
1275 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 );
1276 : }
1277 :
1278 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 );
1279 :
1280 19480 : if ( i_subfr == 0 ) /* if 1st subframe */
1281 : {
1282 3896 : Mode2_abs_pit_enc( *T0, *T0_frac, pt_indice, pit_min, pit_fr1, pit_fr2, pit_res_max );
1283 : }
1284 : else
1285 : {
1286 15584 : Mode2_delta_pit_enc( *T0, *T0_frac, pit_res_max2, *T0_min, *T0_min_frac, pt_indice );
1287 : }
1288 : }
1289 26142 : else if ( coder_type == 4 ) /* 9/6/9/6 (AMRWB) */
1290 : {
1291 26142 : int16_t pit_res_max2 = pit_res_max;
1292 :
1293 26142 : if ( pit_min == PIT_MIN_16k )
1294 : {
1295 :
1296 26130 : pit_res_max2 = pit_res_max >> 1;
1297 : }
1298 :
1299 :
1300 26142 : if ( ( i_subfr == 0 ) || ( i_subfr == 2 * L_SUBFR ) )
1301 : {
1302 :
1303 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 );
1304 : }
1305 : else
1306 : {
1307 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 );
1308 : }
1309 :
1310 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 );
1311 :
1312 26142 : if ( pit_flag == 0 ) /* if 1st/3rd/5th subframe */
1313 : {
1314 10458 : Mode2_abs_pit_enc( *T0, *T0_frac, pt_indice, pit_min, pit_fr1, pit_fr2, pit_res_max );
1315 : }
1316 : else /* if subframe 2 or 4 */
1317 : {
1318 15684 : Mode2_delta_pit_enc( *T0, *T0_frac, pit_res_max2, *T0_min, *T0_min_frac, pt_indice );
1319 : }
1320 : }
1321 0 : else if ( coder_type == 8 ) /* 8/5/5/5 (RF all pred mode) */
1322 : {
1323 0 : if ( i_subfr == 0 )
1324 : {
1325 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 );
1326 : }
1327 : else
1328 : {
1329 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 );
1330 : }
1331 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 );
1332 :
1333 0 : if ( i_subfr == 0 )
1334 : {
1335 0 : Mode2_abs_pit_enc( *T0, *T0_frac, pt_indice, pit_min, pit_fr1b, pit_min, pit_res_max );
1336 : }
1337 : else
1338 : {
1339 0 : Mode2_delta_pit_enc( *T0, *T0_frac, ( pit_res_max >> 1 ), *T0_min, *T0_min_frac, pt_indice );
1340 : }
1341 : }
1342 0 : else if ( coder_type == 9 ) /* 8/0/8/0 (RF mode Gen pred) */
1343 : {
1344 0 : if ( i_subfr == 0 )
1345 : {
1346 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 );
1347 : }
1348 : else
1349 : {
1350 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 );
1351 : }
1352 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 );
1353 :
1354 0 : if ( i_subfr == 0 )
1355 : {
1356 0 : Mode2_abs_pit_enc( *T0, *T0_frac, pt_indice, pit_min, pit_fr1b, pit_min, pit_res_max );
1357 : }
1358 : else
1359 : {
1360 0 : Mode2_delta_pit_enc( *T0, *T0_frac, ( pit_res_max >> 1 ), *T0_min, *T0_min_frac, pt_indice );
1361 : }
1362 : }
1363 : else
1364 : {
1365 0 : assert( 0 );
1366 : }
1367 :
1368 54366 : return;
1369 : }
1370 :
1371 :
1372 : /*-------------------------------------------------------------------*
1373 : * Mode2_abs_pit_enc:
1374 : *
1375 : * Encode pitch lag absolutely
1376 : *-------------------------------------------------------------------*/
1377 :
1378 18301 : void Mode2_abs_pit_enc(
1379 : const int16_t T0, /* i : integer pitch lag */
1380 : const int16_t T0_frac, /* i : pitch fraction */
1381 : int16_t **pt_indice, /* i/o: pointer to Vector of Q indexes */
1382 : const int16_t pit_min,
1383 : const int16_t pit_fr1,
1384 : const int16_t pit_fr2,
1385 : const int16_t pit_res_max )
1386 : {
1387 : int16_t pit_res_max_half;
1388 :
1389 18301 : pit_res_max_half = pit_res_max >> 1;
1390 :
1391 18301 : if ( T0 < pit_fr2 )
1392 : {
1393 5 : **pt_indice = T0 * pit_res_max + T0_frac - ( pit_min * pit_res_max );
1394 : }
1395 18296 : else if ( T0 < pit_fr1 )
1396 : {
1397 13531 : **pt_indice = T0 * pit_res_max_half + T0_frac - ( pit_fr2 * pit_res_max_half ) + ( ( pit_fr2 - pit_min ) * pit_res_max );
1398 : }
1399 : else
1400 : {
1401 4765 : **pt_indice = T0 - pit_fr1 + ( ( pit_fr2 - pit_min ) * pit_res_max ) + ( ( pit_fr1 - pit_fr2 ) * pit_res_max_half );
1402 : }
1403 :
1404 18301 : ( *pt_indice )++;
1405 :
1406 18301 : return;
1407 : }
1408 :
1409 :
1410 : /*-------------------------------------------------------------------*
1411 : * Mode2_delta_pit_enc:
1412 : *
1413 : * Encode pitch lag differentially
1414 : *-------------------------------------------------------------------*/
1415 :
1416 36065 : void Mode2_delta_pit_enc(
1417 : const int16_t T0, /* i : integer pitch lag */
1418 : const int16_t T0_frac, /* i : pitch fraction */
1419 : const int16_t T0_res, /* i : pitch resolution */
1420 : const int16_t T0_min, /* i : delta search min */
1421 : const int16_t T0_min_frac, /* i : delta search min */
1422 : int16_t **pt_indice /* o : pointer to Vector of Q indexes */
1423 : )
1424 : {
1425 :
1426 36065 : **pt_indice = ( T0 - T0_min ) * T0_res + T0_frac - T0_min_frac;
1427 :
1428 36065 : ( *pt_indice )++;
1429 :
1430 36065 : return;
1431 : }
|