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 <stdint.h>
38 : #include "options.h"
39 : #include <math.h>
40 : #include "prot.h"
41 : #include "cnst.h"
42 : #include "rom_com.h"
43 : #include "rom_enc.h"
44 : #include "wmc_auto.h"
45 :
46 : /*---------------------------------------------------------------------*
47 : * Local constants
48 : *---------------------------------------------------------------------*/
49 :
50 : #define PIT_MIN2 20 /* pit_min for pitch tracking */
51 : #define PIT_MIN_1 44 /* pitch tracking */
52 : #define PIT_MIN2_1 24
53 : #define THR_relE -11.0f
54 : #define THRES0 1.17f /* Threshold to favor smaller pitch lags */
55 : #define DELTA0 2.0f /* initial range of multiples search */
56 : #define STEP 1.0f /* increment in range of multiples search */
57 : #define THRES1 0.4f /* Threshold to favor pitch lags coherence for neighbours */
58 : #define DELTA_COH 14 /* Maximum pitch lags difference for neighbours to be considered as coherent */
59 : #define THRES3 0.7f /* Threshold to favor pitch lags coherence with previous frames */
60 : #define CORR_TH0 0.4f /* Noise threshold for reinforcement with past frame pitch */
61 : #define CORR_TH1 0.5f /* Noise threshold for reinforcement with neighbourhood pitch correlations */
62 : #define LEN_X ( ( PIT_MAX / OPL_DECIM ) - ( PIT_MIN2 / OPL_DECIM ) + 1 ) /* Correlation buffer length */
63 : #define COH_FAC 1.4f /* Factor for measuring the pitch coherence */
64 : #define NSECT 4
65 : #define NHFR 3
66 : #define L_FIR_PO 5
67 : #define L_MEM ( L_FIR_PO - 2 )
68 :
69 : /*-----------------------------------------------------------------*
70 : * Local function prototypes
71 : *-----------------------------------------------------------------*/
72 :
73 : static void pitch_neighbour( const int16_t sect0, const int16_t pitch_tmp[], int16_t pitch[NHFR][2 * NSECT], const float corr_tmp[], float corr[3][2 * NSECT], const float thres1[2 * NHFR], const int16_t ind_tmp[2 * NHFR] );
74 :
75 : static void find_mult( float *fac, const int16_t pitch0, const int16_t pitch1, const int16_t pit_max0, float *corr, int16_t *old_pitch, float *old_corr, float delta, const float step );
76 :
77 : static int16_t pitch_coherence( const int16_t pitch0, const int16_t pitch1, const float fac_max, const int16_t diff_max );
78 :
79 : static void lp_decim2( const float x[], float y[], const int16_t l, float *mem );
80 :
81 :
82 : /*-----------------------------------------------------------------*
83 : * pitch_ol_init
84 : *
85 : * Open loop pitch variable initialization
86 : *-----------------------------------------------------------------*/
87 :
88 162692 : void pitch_ol_init(
89 : float *old_thres, /* o : threshold for reinforcement of past pitch influence */
90 : int16_t *old_pitch, /* o : pitch of the 1st half-frame of previous frame */
91 : int16_t *delta_pit, /* o : pitch evolution extrapolation */
92 : float *old_corr /* o : correlation */
93 : )
94 : {
95 162692 : *old_thres = 0.0f;
96 162692 : *old_pitch = 0;
97 162692 : *delta_pit = 0;
98 162692 : *old_corr = 0.0f;
99 :
100 162692 : return;
101 : }
102 :
103 : /*-----------------------------------------------------------------*
104 : * pitch_ol()
105 : *
106 : * Compute the open loop pitch lag.
107 : *
108 : * The pitch lag search is divided in three sections of two sets.
109 : * Each section cannot have a pitch multiple.
110 : * We find a maximum for each section.
111 : * We compare the maxima of each section.
112 : *
113 : * As there is a margin between section overlaps, especially for
114 : * longer delays, this section selection is more robust for not
115 : * to find multiples in the same section when pitch evolves rapidly
116 : *
117 : * For each section, the length of the vectors to correlate is
118 : * greater than or equal to the longest pitch delay
119 : *------------------------------------------------------------------*/
120 :
121 1153834 : void pitch_ol(
122 : int16_t pitch[3], /* o : open loop pitch lag for each half-frame */
123 : float voicing[3], /* o : maximum normalized correlation for each half-frame */
124 : int16_t *old_pitch, /* i/o: OL pitch of the 2nd half-frame of the last frame */
125 : float *old_corr, /* i/o: correlation */
126 : float corr_shift, /* i : normalized correlation correction */
127 : float *old_thres, /* i/o: maximum correlation weighting with respect to past frame pitch */
128 : int16_t *delta_pit, /* i/o: old pitch extrapolation correction (added to old pitch) */
129 : float *st_old_wsp2, /* i/o: weighted speech memory */
130 : const float *wsp, /* i : weighted speech for current frame and look-ahead */
131 : float mem_decim2[3], /* i/o: wsp decimation filter memory */
132 : const float relE, /* i : relative frame energy */
133 : const int16_t L_look, /* i : look-ahead length */
134 : const int16_t last_class, /* i : frame classification of last frame */
135 : const int16_t bwidth, /* i : audio bandwidth */
136 : const int16_t Opt_SC_VBR /* i : SC-VBR flag */
137 : )
138 : {
139 : float old_wsp2[( L_WSP - L_INTERPOL ) / OPL_DECIM], *wsp2;
140 : float tmp_mem[3], scale1[2 * DELTA_COH - 1];
141 : float scaled_buf[2 * LEN_X + 3 * ( DELTA_COH - 1 )];
142 1153834 : float cor_temp, cor_buf[2 * LEN_X], *pt1, *pt2, *pt3, *pt4, *pt5 = 0, *pt6 = 0, *pt_cor0, *pt_cor1, *pt_cor2, *pt_cor3, *pt_cor4;
143 : float thres1[2 * NHFR];
144 1153834 : int16_t diff, cnt, ind, ind1, offset, offset1, offset_la = 0, offset_la1 = 0, coh_flag, coh_flag1;
145 :
146 : int16_t i, j, k, m, pit_min, pit_min1, sect0, subsect0, old_tmp, old_tmp1, len_x, len_x1, len_temp, len_temp1;
147 : int16_t pitchX[NHFR][2 * NSECT], pitch_tmp[2 * NHFR], ind_tmp[2 * NHFR], tmp_buf[NHFR + 1];
148 1153834 : float enr, enr1 = 0.0f, enr_norm[NSECT], enr_norm1[NSECT], fac;
149 : float scaledX[NHFR][2 * NSECT], corX[NHFR][2 * NSECT], cor_tmp[2 * NHFR], cor_mean;
150 :
151 : const int16_t *nb_sect, *nb_subsect, *len, *len1, *sublen, *sublen1, *pit_max, *sec_length, *sec_length1;
152 : int16_t ind_corX, ind1_corX;
153 : float pit_min_coding;
154 :
155 : /*--------------------------------------------------------------*
156 : * Initialization
157 : *--------------------------------------------------------------*/
158 :
159 1153834 : nb_sect = nb_sect_12k8;
160 1153834 : nb_subsect = nb_subsect_12k8;
161 :
162 1153834 : len = len_12k8;
163 1153834 : len1 = len1_12k8;
164 1153834 : sublen = sublen_12k8;
165 1153834 : sublen1 = sublen1_12k8;
166 1153834 : pit_max = pit_max_12k8;
167 1153834 : sec_length = sec_length_12k8;
168 1153834 : sec_length1 = sec_length1_12k8;
169 :
170 1153834 : if ( last_class < VOICED_TRANSITION && bwidth != NB )
171 : {
172 : /* reset last pitch reinforcement in case of unvoiced or transitions: it avoids some pitch doublings */
173 663958 : *old_thres = 0.0f;
174 : }
175 :
176 1153834 : pit_min_coding = PIT_MIN_EXTEND;
177 1153834 : if ( ( bwidth != NB && *old_pitch > PIT_MIN ) ||
178 4011 : ( bwidth == NB && ( *old_pitch > PIT_MIN2_1 || *old_thres < 0.1 ) ) )
179 : {
180 514151 : pit_min = PIT_MIN / OPL_DECIM;
181 514151 : pit_min1 = PIT_MIN_1 / OPL_DECIM;
182 514151 : subsect0 = 2;
183 514151 : sect0 = 1;
184 : }
185 : else
186 : {
187 639683 : pit_min = PIT_MIN2 / OPL_DECIM;
188 639683 : pit_min1 = PIT_MIN2_1 / OPL_DECIM;
189 639683 : subsect0 = 0;
190 639683 : sect0 = 0;
191 : }
192 :
193 1153834 : len_x = PIT_MAX / OPL_DECIM - pit_min + 1;
194 1153834 : len_x1 = PIT_MAX / OPL_DECIM - pit_min1 + 1;
195 :
196 : /*--------------------------------------------------------------*
197 : * Find decimated weighted speech
198 : * Update wsp buffer with the memory
199 : * decimation of wsp[] to search pitch in LF and to reduce complexity
200 : * Extend the decimation of wsp to the end of the speech buffer
201 : * Update wsp memory
202 : *--------------------------------------------------------------*/
203 :
204 1153834 : mvr2r( st_old_wsp2, old_wsp2, ( L_WSP_MEM - L_INTERPOL ) / OPL_DECIM );
205 1153834 : wsp2 = old_wsp2 + ( ( L_WSP_MEM - L_INTERPOL ) / OPL_DECIM );
206 1153834 : lp_decim2( wsp, wsp2, L_FRAME, mem_decim2 );
207 :
208 1153834 : mvr2r( mem_decim2, tmp_mem, 3 );
209 1153834 : lp_decim2( &wsp[L_FRAME], &wsp2[L_FRAME / OPL_DECIM], L_look, tmp_mem );
210 :
211 1153834 : mvr2r( &old_wsp2[L_FRAME / OPL_DECIM], st_old_wsp2, ( L_WSP_MEM - L_INTERPOL ) / OPL_DECIM );
212 :
213 : /*-----------------------------------------------------------------*
214 : * attenuate the correlation correction factor due to noise
215 : * reset correlation buffer outside the useful range
216 : * Find the scaling functions for immediate neigbours and
217 : * further ones
218 : *-----------------------------------------------------------------*/
219 :
220 1153834 : corr_shift *= 0.5f;
221 1153834 : set_f( scaled_buf, 0, DELTA_COH - 1 );
222 1153834 : set_f( scaled_buf + ( DELTA_COH - 1 ) + len_x, 0, DELTA_COH - 1 );
223 1153834 : set_f( scaled_buf + 2 * ( DELTA_COH - 1 ) + len_x + len_x1, 0, DELTA_COH - 1 );
224 :
225 1153834 : pt1 = scale1 + DELTA_COH - 1;
226 1153834 : pt2 = pt1;
227 17307510 : for ( i = 0; i < DELTA_COH; i++ )
228 : {
229 16153676 : *pt1 = -( *old_thres ) / DELTA_COH * i + *old_thres + 1.0f;
230 16153676 : *pt2-- = *pt1++;
231 : }
232 :
233 : /*-----------------------------------------------------------------*
234 : * Estimate the new pitch by extrapolating the old pitch value
235 : * for 2 half-frames
236 : *-----------------------------------------------------------------*/
237 :
238 1153834 : old_tmp = *old_pitch + *delta_pit;
239 :
240 1153834 : if ( old_tmp > PIT_MAX / OPL_DECIM )
241 : {
242 3265 : old_tmp = PIT_MAX / OPL_DECIM;
243 : }
244 :
245 1153834 : if ( old_tmp < pit_min )
246 : {
247 166030 : old_tmp = pit_min;
248 : }
249 :
250 1153834 : old_tmp1 = old_tmp + *delta_pit;
251 1153834 : if ( old_tmp1 > PIT_MAX / OPL_DECIM )
252 : {
253 4651 : old_tmp1 = PIT_MAX / OPL_DECIM;
254 : }
255 :
256 1153834 : if ( old_tmp1 < pit_min )
257 : {
258 9940 : old_tmp1 = pit_min;
259 : }
260 :
261 :
262 : /*-----------------------------------------------------------------*
263 : * Loop for all three half-frames (current frame + look-ahead)
264 : *-----------------------------------------------------------------*/
265 1153834 : pt_cor0 = scaled_buf + DELTA_COH - 1;
266 :
267 1153834 : pt_cor2 = pt_cor0 - pit_min + old_tmp;
268 1153834 : pt_cor4 = pt_cor0 - pit_min1 + old_tmp + ( DELTA_COH - 1 ) + len_x;
269 :
270 4615336 : for ( i = 0; i < NHFR; i++ )
271 : {
272 3461502 : pt1 = wsp2 + i * 2 * ( L_SUBFR / OPL_DECIM );
273 3461502 : pt2 = pt1 - pit_min;
274 3461502 : enr = 0.01f;
275 3461502 : pt_cor1 = pt_cor0;
276 3461502 : pt4 = pt1 - pit_min1;
277 3461502 : pt_cor3 = pt_cor0 + ( DELTA_COH - 1 ) + len_x;
278 :
279 : /*-----------------------------------------------------------------*
280 : * First two half-frames (corresponding to current frame)
281 : *-----------------------------------------------------------------*/
282 3461502 : if ( i < NHFR - 1 )
283 : {
284 2307668 : pt3 = pt1;
285 2307668 : pt5 = pt1;
286 :
287 10510038 : for ( j = sect0; j < nb_sect[i]; j++ ) /* loop for each section */
288 : {
289 : /*-----------------------------------------------------------------*
290 : * Find fixed vector energy
291 : *-----------------------------------------------------------------*/
292 :
293 : /* 1st set */
294 8202370 : k = (int16_t) ( pt1 - pt3 );
295 273584190 : for ( k = k + len[j]; k > 0; k-- )
296 : {
297 265381820 : enr += *pt3 * *pt3;
298 265381820 : pt3++;
299 : }
300 8202370 : enr_norm[j] = enr;
301 :
302 : /* Reduce complexity (length of 'enr1' section is equal or larger than 'enr') */
303 8202370 : pt5 = pt3;
304 8202370 : enr1 = enr;
305 :
306 : /* 2nd set */
307 8202370 : k = (int16_t) ( pt1 - pt5 );
308 72817074 : for ( k = k + len1[j]; k > 0; k-- )
309 : {
310 64614704 : enr1 += *pt5 * *pt5;
311 64614704 : pt5++;
312 : }
313 8202370 : enr_norm1[j] = enr1;
314 : }
315 :
316 : /*-----------------------------------------------------------------*
317 : * Find correlation for the non-overlapping pitch lag values
318 : *-----------------------------------------------------------------*/
319 2307668 : k = (int16_t) ( pt2 - pt1 );
320 10007910 : for ( k = k + pit_max[subsect0]; k >= 0; k-- )
321 : {
322 7700242 : *pt_cor1++ = dotp( pt1, pt2--, sublen[0] );
323 : }
324 :
325 : /*-----------------------------------------------------------------*
326 : * For each subsection, find the correlation
327 : *-----------------------------------------------------------------*/
328 16404740 : for ( j = subsect0; j < nb_subsect[i]; j++ )
329 : {
330 14097072 : len_temp = sublen[j];
331 14097072 : len_temp1 = sublen1[j];
332 :
333 14097072 : k = (int16_t) ( pt2 - pt1 );
334 14097072 : if ( len_temp < len_temp1 )
335 : {
336 76153044 : for ( k = k + pit_max[j + 1]; k >= 0; k-- )
337 : {
338 71537708 : cor_temp = pt1[0] * pt2[0];
339 3927650936 : for ( m = 1; m < len_temp; m++ )
340 : {
341 3856113228 : cor_temp += pt1[m] * pt2[m];
342 : }
343 71537708 : *pt_cor1++ = cor_temp;
344 1174603012 : for ( ; m < len_temp1; m++ )
345 : {
346 1103065304 : cor_temp += pt1[m] * pt2[m];
347 : }
348 71537708 : *pt_cor3++ = cor_temp;
349 71537708 : pt2--;
350 : }
351 : }
352 : else
353 : {
354 167658480 : for ( k = k + pit_max[j + 1]; k >= 0; k-- )
355 : {
356 158176744 : cor_temp = pt1[0] * pt2[0];
357 14588521200 : for ( m = 1; m < len_temp1; m++ )
358 : {
359 14430344456 : cor_temp += pt1[m] * pt2[m];
360 : }
361 158176744 : *pt_cor3++ = cor_temp;
362 1699698968 : for ( ; m < len_temp; m++ )
363 : {
364 1541522224 : cor_temp += pt1[m] * pt2[m];
365 : }
366 158176744 : *pt_cor1++ = cor_temp;
367 158176744 : pt2--;
368 : }
369 : }
370 : }
371 : }
372 :
373 : /*-----------------------------------------------------------------*
374 : * Third half-frame (look-ahead)
375 : *-----------------------------------------------------------------*/
376 :
377 : else
378 : {
379 : /*-----------------------------------------------------------------*
380 : * For each section in both sets, find fixed vector energy
381 : *-----------------------------------------------------------------*/
382 :
383 1153834 : pt6 = pt1 + L_look / OPL_DECIM - 1;
384 1153834 : pt3 = pt6;
385 1153834 : pt5 = pt6;
386 :
387 5255019 : for ( j = sect0; j < nb_sect[i]; j++ ) /* loop for each section */
388 : {
389 : /* 1st set */
390 4101185 : k = (int16_t) ( pt3 - pt6 );
391 136792095 : for ( k = k + len[j]; k > 0; k-- )
392 : {
393 132690910 : enr += *pt3 * *pt3;
394 132690910 : pt3--;
395 : }
396 :
397 4101185 : enr_norm[j] = enr;
398 :
399 4101185 : pt5 = pt3;
400 4101185 : enr1 = enr;
401 :
402 : /* 2nd set */
403 4101185 : k = (int16_t) ( pt5 - pt6 );
404 36408537 : for ( k = k + len1[j]; k > 0; k-- )
405 : {
406 32307352 : enr1 += *pt5 * *pt5;
407 32307352 : pt5--;
408 : }
409 :
410 4101185 : enr_norm1[j] = enr1;
411 : }
412 :
413 : /* Set pointers */
414 1153834 : if ( sect0 == 0 )
415 : {
416 639683 : pt2 = pt6 - pit_min;
417 639683 : k = 2;
418 : }
419 : else
420 : {
421 514151 : pt2 = pt6 - pit_max[1] - 1;
422 514151 : k = pit_max[2] - pit_max[1];
423 : }
424 :
425 : /*-----------------------------------------------------------------*
426 : * Find correlation for the non-overlapping pitch lag values
427 : *-----------------------------------------------------------------*/
428 :
429 5003955 : for ( ; k > 0; k-- )
430 : {
431 3850121 : *pt_cor1 = 0;
432 157854961 : for ( m = 0; m < sublen[0]; m++ )
433 : {
434 154004840 : *pt_cor1 += pt6[-m] * pt2[-m];
435 : }
436 3850121 : pt_cor1++;
437 3850121 : pt2--;
438 : }
439 :
440 : /*-----------------------------------------------------------------*
441 : * For each subsection, find the correlation (overlapping pitch lag values)
442 : *-----------------------------------------------------------------*/
443 :
444 8202370 : for ( j = subsect0; j < nb_subsect[i]; j++ )
445 : {
446 7048536 : len_temp = sublen[j];
447 7048536 : len_temp1 = sublen1[j];
448 :
449 7048536 : k = pit_max[j + 1] - pit_max[j];
450 7048536 : if ( len_temp < len_temp1 )
451 : {
452 38076522 : for ( ; k > 0; k-- )
453 : {
454 35768854 : cor_temp = pt6[0] * pt2[0];
455 1963825468 : for ( m = 1; m < len_temp; m++ )
456 : {
457 1928056614 : cor_temp += pt6[-m] * pt2[-m];
458 : }
459 35768854 : *pt_cor1++ = cor_temp;
460 587301506 : for ( ; m < len_temp1; m++ )
461 : {
462 551532652 : cor_temp += pt6[-m] * pt2[-m];
463 : }
464 35768854 : *pt_cor3++ = cor_temp;
465 35768854 : pt2--;
466 : }
467 : }
468 : else
469 : {
470 83829240 : for ( ; k > 0; k-- )
471 : {
472 79088372 : cor_temp = pt6[0] * pt2[0];
473 7294260600 : for ( m = 1; m < len_temp1; m++ )
474 : {
475 7215172228 : cor_temp += pt6[-m] * pt2[-m];
476 : }
477 79088372 : *pt_cor3++ = cor_temp;
478 849849484 : for ( ; m < len_temp; m++ )
479 : {
480 770761112 : cor_temp += pt6[-m] * pt2[-m];
481 : }
482 79088372 : *pt_cor1++ = cor_temp;
483 79088372 : pt2--;
484 : }
485 : }
486 : }
487 : }
488 :
489 : /* Save unscaled correlation vector */
490 3461502 : mvr2r( pt_cor0, cor_buf, len_x );
491 3461502 : mvr2r( pt_cor0 + ( DELTA_COH - 1 ) + len_x, cor_buf + len_x, len_x1 );
492 :
493 : /*-----------------------------------------------------------------*
494 : * scale correlation function in the neighbourhood of
495 : * the extrapolated pitch
496 : *-----------------------------------------------------------------*/
497 3461502 : pt_cor1 = pt_cor2 - ( DELTA_COH - 1 );
498 3461502 : pt_cor3 = pt_cor4 - ( DELTA_COH - 1 );
499 3461502 : pt2 = scale1;
500 :
501 96922056 : for ( k = 0; k < 2 * DELTA_COH - 1; k++ )
502 : {
503 93460554 : *pt_cor1++ *= ( *pt2 );
504 93460554 : *pt_cor3++ *= ( *pt2++ );
505 : }
506 :
507 : /* update for next half-frame */
508 3461502 : pt_cor2 = pt_cor0 - pit_min + old_tmp1;
509 3461502 : pt_cor4 = pt_cor0 - pit_min1 + old_tmp1 + ( DELTA_COH - 1 ) + len_x;
510 : /*-----------------------------------------------------------------*
511 : * For each section, find maximum correlation and compute
512 : * normalized correlation
513 : *-----------------------------------------------------------------*/
514 :
515 3461502 : pt_cor1 = pt_cor0;
516 3461502 : offset = 0;
517 3461502 : pt_cor3 = pt_cor0 + ( DELTA_COH - 1 ) + len_x;
518 3461502 : offset1 = 0;
519 15765057 : for ( j = sect0; j < nb_sect[i]; j++ ) /* loop for each section */
520 : {
521 : /* 1st set */
522 12303555 : if ( i == 2 )
523 : {
524 4101185 : offset_la = L_look / OPL_DECIM - 1 - ( len[j] - 1 );
525 : }
526 : else
527 : {
528 8202370 : offset_la = 0;
529 : }
530 :
531 : /* 2nd set */
532 12303555 : if ( i == 2 )
533 : {
534 4101185 : offset_la1 = L_look / OPL_DECIM - 1 - ( len1[j] - 1 );
535 : }
536 : else
537 : {
538 8202370 : offset_la1 = 0;
539 : }
540 :
541 : /* 1st set of candidates */
542 12303555 : ind = maximum( pt_cor1, sec_length[j], 0 ) + offset;
543 12303555 : pitchX[i][j] = ind + pit_min;
544 12303555 : pt2 = pt1 - pitchX[i][j] + offset_la; /* selected moving vector */
545 12303555 : enr1 = dotp( pt2, pt2, len[j] ) + 0.01f;
546 12303555 : enr1 = inv_sqrt( enr_norm[j] * enr1 ); /* 1/sqrt(energy) */
547 12303555 : corX[i][j] = cor_buf[ind] * enr1; /* find best normalized correlation per section */
548 12303555 : scaledX[i][j] = pt_cor0[ind] * enr1; /* find best scaled normalized correlation per section */
549 :
550 12303555 : pt_cor1 += sec_length[j];
551 12303555 : offset = offset + sec_length[j];
552 :
553 : /* 2nd set of candidates */
554 12303555 : ind1 = maximum( pt_cor3, sec_length1[j], 0 ) + offset1;
555 12303555 : pitchX[i][j + NSECT] = ind1 + pit_min1;
556 12303555 : pt4 = pt1 - pitchX[i][j + NSECT] + offset_la1; /* selected moving vector */
557 12303555 : enr1 = dotp( pt4, pt4, len1[j] ) + 0.01f;
558 12303555 : enr1 = inv_sqrt( enr_norm1[j] * enr1 ); /* 1/sqrt(energy) */
559 12303555 : corX[i][j + NSECT] = cor_buf[ind1 + len_x] * enr1; /* find best normalized correlation per section */
560 12303555 : scaledX[i][j + NSECT] = pt_cor0[ind1 + ( DELTA_COH - 1 ) + len_x] * enr1; /* find best scaled normalized correlation per section */
561 :
562 12303555 : pt_cor3 += sec_length1[j];
563 12303555 : offset1 = offset1 + sec_length1[j];
564 : }
565 : }
566 : /*-----------------------------------------------------------------*
567 : * Favor a smaller delay if it happens that it has its multiple
568 : * in the longer-delay sections (harmonics check)
569 : *-----------------------------------------------------------------*/
570 :
571 3461502 : for ( i = 0; i < 2; i++ ) /* loop for the 2 half-frames */
572 : {
573 2307668 : fac = THRES0;
574 2307668 : find_mult( &fac, pitchX[i][2], pitchX[i][3], pit_max[7], &scaledX[i][2], old_pitch, old_corr, DELTA0, STEP ); /* Multiples in longest-delay section */
575 2307668 : find_mult( &fac, pitchX[i][1], pitchX[i][2], pit_max[5], &scaledX[i][1], old_pitch, old_corr, DELTA0, STEP ); /* Multiples in 3rd section */
576 :
577 2307668 : if ( ( sect0 == 0 ) && ( ( pitchX[i][0] * 2 ) >= pit_min_coding ) )
578 : {
579 1279366 : find_mult( &fac, pitchX[i][0], pitchX[i][1], pit_max[3], &scaledX[i][0], old_pitch, old_corr, DELTA0, STEP ); /* Multiples in 2nd section */
580 : }
581 2307668 : fac = THRES0;
582 2307668 : find_mult( &fac, pitchX[i][NSECT + 2], pitchX[i][NSECT + 3], pit_max[7], &scaledX[i][NSECT + 2], old_pitch, old_corr, DELTA0, STEP ); /* Multiples in longest-delay section */
583 2307668 : find_mult( &fac, pitchX[i][NSECT + 1], pitchX[i][NSECT + 2], pit_max[6], &scaledX[i][NSECT + 1], old_pitch, old_corr, DELTA0, STEP ); /* Multiples in 3rd section */
584 :
585 2307668 : if ( ( sect0 == 0 ) && ( pitchX[i][NSECT + 0] * 2 >= pit_min_coding ) )
586 : {
587 1279366 : find_mult( &fac, pitchX[i][NSECT + 0], pitchX[i][NSECT + 1], pit_max[4], &scaledX[i][NSECT + 0], old_pitch, old_corr, DELTA0, STEP ); /* Multiples in 2nd section */
588 : }
589 : }
590 :
591 1153834 : fac = THRES0; /* the look-ahead */
592 1153834 : find_mult( &fac, pitchX[i][2], pitchX[i][3], pit_max[7], &scaledX[i][2], old_pitch, old_corr, 2.0f, 2.0f ); /* Multiples in longest-delay section */
593 1153834 : find_mult( &fac, pitchX[i][1], pitchX[i][2], pit_max[5], &scaledX[i][1], old_pitch, old_corr, DELTA0, STEP ); /* Multiples in 3rd section */
594 :
595 1153834 : if ( ( sect0 == 0 ) && ( pitchX[i][0] * 2 >= pit_min_coding ) )
596 : {
597 639683 : find_mult( &fac, pitchX[i][0], pitchX[i][1], pit_max[3], &scaledX[i][0], old_pitch, old_corr, DELTA0, STEP ); /* Multiples in 2nd section */
598 : }
599 :
600 1153834 : fac = THRES0; /* the look-ahead */
601 1153834 : find_mult( &fac, pitchX[i][NSECT + 2], pitchX[i][NSECT + 3], pit_max[7], &scaledX[i][NSECT + 2], old_pitch, old_corr, 2.0f, 2.0f ); /* Multiples in longest-delay section */
602 1153834 : find_mult( &fac, pitchX[i][NSECT + 1], pitchX[i][NSECT + 2], pit_max[6], &scaledX[i][NSECT + 1], old_pitch, old_corr, DELTA0, STEP ); /* Multiples in 3rd section */
603 :
604 1153834 : if ( ( sect0 == 0 ) && ( pitchX[i][NSECT + 0] * 2 >= pit_min_coding ) )
605 : {
606 639683 : find_mult( &fac, pitchX[i][NSECT + 0], pitchX[i][NSECT + 1], pit_max[4], &scaledX[i][NSECT + 0], old_pitch, old_corr, DELTA0, STEP ); /* Multiples in 2nd section */
607 : }
608 :
609 : /*-----------------------------------------------------------------*
610 : * Do 1st estimate for pitch values
611 : * Adjust the normalized correlation using estimated noise level
612 : * Compute the maximum scaling for the neighbour correlation
613 : * reinforcement
614 : *-----------------------------------------------------------------*/
615 :
616 4615336 : for ( i = 0; i < NHFR; i++ )
617 : {
618 3461502 : ind = maximum( scaledX[i] + sect0, (int16_t) ( NSECT - sect0 ), 0 );
619 3461502 : ind = ind + sect0;
620 3461502 : ind_tmp[i] = ind;
621 3461502 : pitch_tmp[i] = pitchX[i][ind];
622 3461502 : cor_tmp[i] = corX[i][ind];
623 3461502 : cor_tmp[i] += corr_shift;
624 :
625 3461502 : if ( cor_tmp[i] > 1.0f )
626 : {
627 3514 : cor_tmp[i] = 1.0f;
628 : }
629 3461502 : thres1[i] = THRES1 * cor_tmp[i]; /* Higher is the neighbour's correlation, higher is the weighting */
630 :
631 : /* 2nd set of pitch candidates */
632 3461502 : ind1 = maximum( scaledX[i] + sect0 + NSECT, (int16_t) ( NSECT - sect0 ), 0 );
633 3461502 : ind1 += ( sect0 + NSECT );
634 3461502 : ind_tmp[i + NHFR] = ind1;
635 3461502 : pitch_tmp[i + NHFR] = pitchX[i][ind1];
636 3461502 : cor_tmp[i + NHFR] = corX[i][ind1];
637 3461502 : cor_tmp[i + NHFR] += corr_shift;
638 :
639 3461502 : if ( cor_tmp[i + NHFR] > 1.0f )
640 : {
641 3431 : cor_tmp[i + NHFR] = 1.0f;
642 : }
643 3461502 : thres1[i + NHFR] = THRES1 * cor_tmp[i + NHFR]; /* Higher is the neighbour's correlation, higher is the weighting */
644 : }
645 : /*-----------------------------------------------------------------*
646 : * Take into account previous and next pitch values of the present
647 : * frame and look-ahead. Choose the pitch lags and normalize
648 : * correlations for each half-frame & look-ahead
649 : *-----------------------------------------------------------------*/
650 :
651 1153834 : pitch_neighbour( sect0, pitch_tmp, pitchX, cor_tmp, scaledX, thres1, ind_tmp );
652 4615336 : for ( i = 0; i < NHFR; i++ )
653 : {
654 3461502 : ind = maximum( scaledX[i] + sect0, (int16_t) ( NSECT - sect0 ), 0 );
655 3461502 : ind = ind + sect0;
656 3461502 : ind_corX = maximum( corX[i] + sect0, (int16_t) ( NSECT - sect0 ), 0 );
657 3461502 : ind_corX = ind_corX + sect0;
658 :
659 3461502 : ind1 = maximum( scaledX[i] + sect0 + NSECT, (int16_t) ( NSECT - sect0 ), 0 );
660 3461502 : ind1 += ( sect0 + NSECT );
661 3461502 : ind1_corX = maximum( corX[i] + sect0 + NSECT, (int16_t) ( NSECT - sect0 ), 0 );
662 3461502 : ind1_corX += ( sect0 + NSECT );
663 :
664 3461502 : if ( scaledX[i][ind1] > scaledX[i][ind] )
665 : {
666 1387258 : ind = ind1;
667 : }
668 :
669 3461502 : if ( Opt_SC_VBR && corX[i][ind1_corX] > corX[i][ind_corX] )
670 : {
671 0 : ind_corX = ind1_corX;
672 : }
673 :
674 3461502 : if ( Opt_SC_VBR && ( pitchX[i][ind] * 0.4 < pitchX[i][ind_corX] ) && ( pitchX[i][ind] * 0.6 > pitchX[i][ind_corX] ) && ( corX[i][ind_corX] >= 0.9 ) ) /* && (pitchX[i][ind]>50)) */
675 : {
676 0 : pitch[i] = pitchX[i][ind_corX];
677 0 : voicing[i] = corX[i][ind_corX];
678 : }
679 : else
680 : {
681 3461502 : pitch[i] = pitchX[i][ind];
682 3461502 : voicing[i] = corX[i][ind];
683 : }
684 : }
685 :
686 : /*-----------------------------------------------------------------*
687 : * Increase the threshold for correlation reinforcement with
688 : * the past if correlation is high and pitch is stable
689 : *-----------------------------------------------------------------*/
690 :
691 1153834 : cor_mean = 0.5f * ( voicing[0] + voicing[1] ) + corr_shift;
692 1153834 : if ( cor_mean > 1.0f )
693 : {
694 850 : cor_mean = 1.0f;
695 : }
696 :
697 : /* pitch unstable in present frame or from previous frame or normalized correlation too low */
698 1153834 : coh_flag = pitch_coherence( (int16_t) pitch[0], (int16_t) pitch[1], COH_FAC, DELTA_COH );
699 1153834 : coh_flag1 = pitch_coherence( (int16_t) pitch[0], (int16_t) *old_pitch, COH_FAC, DELTA_COH );
700 1153834 : if ( ( coh_flag == 0 ) || ( coh_flag1 == 0 ) || ( cor_mean < CORR_TH0 ) || ( relE < THR_relE ) )
701 : {
702 633961 : *old_thres = 0.0f; /* Reset the threshold */
703 : }
704 : else
705 : {
706 519873 : *old_thres += ( 0.16f * cor_mean ); /* The threshold increase is directly dependent on normalized correlation */
707 : }
708 :
709 1153834 : if ( *old_thres > THRES3 )
710 : {
711 213328 : *old_thres = THRES3;
712 : }
713 :
714 1153834 : if ( voicing[1] > voicing[0] )
715 : {
716 554747 : *old_corr = voicing[1];
717 : }
718 : else
719 : {
720 599087 : *old_corr = cor_mean;
721 : }
722 :
723 : /*-----------------------------------------------------------------*
724 : * Extrapolate the pitch value for the next frame by estimating
725 : * the pitch evolution. This value is added to the old_pitch
726 : * in the next frame and is then used when the normalized
727 : * correlation is reinforced by the past estimate
728 : *-----------------------------------------------------------------*/
729 :
730 1153834 : tmp_buf[0] = *old_pitch;
731 4615336 : for ( i = 0; i < NHFR; i++ )
732 : {
733 3461502 : tmp_buf[i + 1] = pitch[i];
734 : }
735 :
736 1153834 : *delta_pit = 0;
737 1153834 : cnt = 0;
738 4615336 : for ( i = 0; i < NHFR; i++ )
739 : {
740 3461502 : diff = tmp_buf[i + 1] - tmp_buf[i];
741 3461502 : coh_flag = pitch_coherence( (int16_t) tmp_buf[i], (int16_t) tmp_buf[i + 1], COH_FAC, DELTA_COH );
742 3461502 : if ( coh_flag != 0 )
743 : {
744 2526142 : *delta_pit = *delta_pit + diff;
745 2526142 : cnt++;
746 : }
747 : }
748 :
749 1153834 : if ( cnt == 2 )
750 : {
751 269834 : *delta_pit /= 2;
752 : }
753 :
754 1153834 : if ( cnt == 3 )
755 : {
756 599873 : *delta_pit /= 3;
757 : }
758 :
759 : /*-----------------------------------------------------------------*
760 : * update old pitch, upsample pitch
761 : *-----------------------------------------------------------------*/
762 :
763 1153834 : *old_pitch = pitch[1];
764 :
765 4615336 : for ( i = 0; i < NHFR; i++ )
766 : {
767 3461502 : pitch[i] *= OPL_DECIM;
768 : }
769 :
770 :
771 1153834 : return;
772 : }
773 :
774 : /*-----------------------------------------------------------------*
775 : * find_mult
776 : *
777 : * Verifies whether max pitch delays in higher sections have multiples
778 : * in lower sections
779 : *-----------------------------------------------------------------*/
780 :
781 17684106 : static void find_mult(
782 : float *fac, /* i/o: correlation scaling factor */
783 : const int16_t pitch0, /* i : pitch of max correlation in the c section */
784 : const int16_t pitch1, /* i : pitch of max correlation in the longer-delay section */
785 : const int16_t pit_max0, /* i : max pitch delay in the longer-delay section */
786 : float *corr, /* i/o: max correlation in the shorter-delay section */
787 : int16_t *old_pitch, /* i : pitch from previous frame */
788 : float *old_corr, /* i : max correlation from previous frame */
789 : float delta, /* i : initial multiples search range */
790 : const float step /* i : increment in range of multiples search */
791 : )
792 : {
793 : int16_t pit_min;
794 :
795 17684106 : pit_min = 2 * pitch0; /* double the shorter-delay section pitch */
796 38755232 : while ( pit_min <= pit_max0 + (int16_t) delta ) /* check for section boundary */
797 : {
798 21071126 : if ( abs( pit_min - pitch1 ) <= (int16_t) delta ) /* if multiple in the allowed range */
799 : {
800 6333211 : if ( *old_corr < 0.6f || (float) pitch0 > (float) *old_pitch * 0.4f )
801 : {
802 : /* reinforce the normalized correlation */
803 5835866 : *corr *= *fac;
804 : }
805 6333211 : *fac *= THRES0;
806 : }
807 21071126 : pit_min = pit_min + pitch0; /* next multiple */
808 21071126 : delta += step; /* add the incertitude to the allowed range */
809 : }
810 17684106 : return;
811 : }
812 :
813 : /*---------------------------------------------------------------------------*
814 : * pitch_neighbour
815 : *
816 : * Verifies if the maximum correlation pitch lag is coherent with neighbour
817 : * values
818 : *---------------------------------------------------------------------------*/
819 1153834 : static void pitch_neighbour(
820 : const int16_t sect0, /* i : indicates whether section 0 (below PIT_MIN) is used */
821 : const int16_t pitch_tmp[], /* i : estimated pitch values for each half-frame & look-ahead */
822 : int16_t pitch[NHFR][2 * NSECT], /* i : tested pitch values for each half-frame & look-ahead */
823 : const float corr_tmp[], /* i : raw normalized correlation (before different scalings) */
824 : float corr[NHFR][2 * NSECT], /* i/o: normalized correlation for each half-frame & look-ahead */
825 : const float thres1[2 * NHFR], /* i : maximum scaling for the immediate neighbours */
826 : const int16_t ind_tmp[2 * NHFR] /* i : maximum section indices */
827 : )
828 : {
829 : int16_t delta, i, j, k, K, coh_flag;
830 :
831 5255019 : for ( k = sect0; k < NSECT; k++ ) /* loop for each section */
832 : {
833 4101185 : if ( k == ( NSECT - 1 ) )
834 : {
835 1153834 : K = 2; /* the number of tests depends on the section */
836 : }
837 : else
838 : {
839 2947351 : K = 3;
840 : }
841 15250906 : for ( i = 0; i < K; i++ ) /* loop for the 2 half-frames and the look-ahead */
842 : {
843 : /* Compare pitch values of the present frame */
844 42291216 : for ( j = 0; j < K; j++ ) /* Verify pitch coherence with neighbours (including past pitch) */
845 : {
846 31141495 : if ( j != i ) /* Exclude itself */
847 : {
848 19991774 : if ( corr_tmp[j] >= CORR_TH1 ) /* reinforcement can happen only if the correlation is high enough */
849 : {
850 15079744 : delta = (int16_t) abs( pitch[i][k] - pitch_tmp[j] ); /* Find difference of pitch values */
851 15079744 : coh_flag = pitch_coherence( (int16_t) pitch[i][k], (int16_t) pitch_tmp[j], COH_FAC, DELTA_COH );
852 15079744 : if ( coh_flag != 0 )
853 : {
854 : /* Favour section-wise stability */
855 5437398 : if ( ind_tmp[j] == k )
856 : {
857 4109292 : corr[i][k] *= ( -thres1[j] / DELTA_COH * delta + thres1[j] + 1.0f ); /* Favour closer values */
858 : }
859 : else
860 : {
861 1328106 : corr[i][k] *= ( -thres1[j] / DELTA_COH * 0.625f * delta + thres1[j] * 0.625f + 1.0f );
862 : }
863 : }
864 : }
865 : }
866 : }
867 : }
868 : }
869 : /*---------------------*
870 : * 2nd set of sections
871 : *---------------------*/
872 5255019 : for ( k = sect0; k < NSECT; k++ ) /* loop for each section */
873 : {
874 4101185 : if ( k == ( NSECT - 1 ) )
875 : {
876 1153834 : K = 2; /* the number of tests depends on the section */
877 : }
878 : else
879 : {
880 2947351 : K = 3;
881 : }
882 15250906 : for ( i = 0; i < K; i++ ) /* loop for the 2 half-frames and the look-ahead */
883 : {
884 : /* Compare pitch values of the present frame */
885 42291216 : for ( j = 0; j < K; j++ ) /* Verify pitch coherence with neighbours (including past pitch) */
886 : {
887 31141495 : if ( j != i ) /* Exclude itself */
888 : {
889 19991774 : if ( corr_tmp[j + NHFR] >= CORR_TH1 ) /* reinforcement can happen only if the correlation is high enough */
890 : {
891 14753576 : delta = (int16_t) abs( pitch[i][NSECT + k] - pitch_tmp[j + NHFR] ); /* Find difference of pitch values */
892 14753576 : coh_flag = pitch_coherence( (int16_t) pitch[i][NSECT + k], (int16_t) pitch_tmp[j + NHFR], COH_FAC, DELTA_COH );
893 14753576 : if ( coh_flag != 0 )
894 : {
895 : /* Favour section-wise stability */
896 5557007 : if ( ind_tmp[j + NHFR] == ( NSECT + k ) )
897 : {
898 4212975 : corr[i][NSECT + k] *= ( -thres1[j + NHFR] / DELTA_COH * delta + thres1[j + NHFR] + 1.0f ); /* Favour closer values */
899 : }
900 : else
901 : {
902 1344032 : corr[i][NSECT + k] *= ( -thres1[j + NHFR] / DELTA_COH * 0.625f * delta + thres1[j + NHFR] * 0.625f + 1.0f );
903 : }
904 : }
905 : }
906 : }
907 : }
908 : }
909 : }
910 1153834 : return;
911 : }
912 :
913 : /*-----------------------------------------------------------------*
914 : * pitch_coherence
915 : *
916 : * Verify if pitch evolution is smooth
917 : *-----------------------------------------------------------------*/
918 :
919 35602490 : static int16_t pitch_coherence(
920 : const int16_t pitch0, /* i : first pitch to compare */
921 : const int16_t pitch1, /* i : 2nd pitch to compare */
922 : const float fac_max, /* i : max ratio of both values */
923 : const int16_t diff_max /* i : max difference of both values */
924 : )
925 : {
926 : int16_t smaller, larger;
927 35602490 : if ( pitch1 < pitch0 ) /* Finds smaller and larger of 2 short values */
928 : {
929 14086481 : smaller = pitch1;
930 14086481 : larger = pitch0;
931 : }
932 : else
933 : {
934 21516009 : smaller = pitch0;
935 21516009 : larger = pitch1;
936 : }
937 35602490 : if ( ( (float) larger < fac_max * (float) smaller ) && ( ( larger - smaller ) < diff_max ) )
938 : {
939 15141802 : return 1;
940 : }
941 : else
942 : {
943 20460688 : return 0;
944 : }
945 : }
946 :
947 :
948 : /*-----------------------------------------------------------------*
949 : * lp_decim2:
950 : *
951 : * Decimate a vector by 2 with 2nd order fir filter.
952 : *-----------------------------------------------------------------*/
953 :
954 2307668 : static void lp_decim2(
955 : const float x[], /* i : signal to process */
956 : float y[], /* o : processed signals */
957 : const int16_t l, /* i : size of filtering */
958 : float *mem /* i/o: memory (size=3) */
959 : )
960 : {
961 : const float *p_h;
962 : float temp, *p_x, x_buf[L_FRAME_PLUS + L_MEM];
963 : int16_t i, j, k;
964 :
965 : /* copy initial filter states into buffer */
966 2307668 : p_x = x_buf;
967 9230672 : for ( i = 0; i < L_MEM; i++ )
968 : {
969 6923004 : *p_x++ = mem[i];
970 : }
971 426918580 : for ( i = 0; i < l; i++ )
972 : {
973 424610912 : *p_x++ = x[i];
974 : }
975 9230672 : for ( i = 0; i < L_MEM; i++ )
976 : {
977 6923004 : mem[i] = x[l - L_MEM + i];
978 : }
979 214613124 : for ( i = 0, j = 0; i < l; i += 2, j++ )
980 : {
981 212305456 : p_x = &x_buf[i];
982 212305456 : p_h = h_fir;
983 212305456 : temp = 0.0f;
984 :
985 1273832736 : for ( k = 0; k < L_FIR_PO; k++ )
986 : {
987 1061527280 : temp += *p_x++ * *p_h++;
988 : }
989 212305456 : y[j] = temp;
990 : }
991 2307668 : return;
992 : }
|