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 3031773 : 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 3031773 : *old_thres = 0.0f;
96 3031773 : *old_pitch = 0;
97 3031773 : *delta_pit = 0;
98 3031773 : *old_corr = 0.0f;
99 :
100 3031773 : 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 16214896 : 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 16214896 : 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 16214896 : 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 16214896 : 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 16214896 : nb_sect = nb_sect_12k8;
160 16214896 : nb_subsect = nb_subsect_12k8;
161 :
162 16214896 : len = len_12k8;
163 16214896 : len1 = len1_12k8;
164 16214896 : sublen = sublen_12k8;
165 16214896 : sublen1 = sublen1_12k8;
166 16214896 : pit_max = pit_max_12k8;
167 16214896 : sec_length = sec_length_12k8;
168 16214896 : sec_length1 = sec_length1_12k8;
169 :
170 16214896 : 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 10700843 : *old_thres = 0.0f;
174 : }
175 :
176 16214896 : pit_min_coding = PIT_MIN_EXTEND;
177 16214896 : if ( ( bwidth != NB && *old_pitch > PIT_MIN ) ||
178 178549 : ( bwidth == NB && ( *old_pitch > PIT_MIN2_1 || *old_thres < 0.1 ) ) )
179 : {
180 6851943 : pit_min = PIT_MIN / OPL_DECIM;
181 6851943 : pit_min1 = PIT_MIN_1 / OPL_DECIM;
182 6851943 : subsect0 = 2;
183 6851943 : sect0 = 1;
184 : }
185 : else
186 : {
187 9362953 : pit_min = PIT_MIN2 / OPL_DECIM;
188 9362953 : pit_min1 = PIT_MIN2_1 / OPL_DECIM;
189 9362953 : subsect0 = 0;
190 9362953 : sect0 = 0;
191 : }
192 :
193 16214896 : len_x = PIT_MAX / OPL_DECIM - pit_min + 1;
194 16214896 : 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 16214896 : mvr2r( st_old_wsp2, old_wsp2, ( L_WSP_MEM - L_INTERPOL ) / OPL_DECIM );
205 16214896 : wsp2 = old_wsp2 + ( ( L_WSP_MEM - L_INTERPOL ) / OPL_DECIM );
206 16214896 : lp_decim2( wsp, wsp2, L_FRAME, mem_decim2 );
207 :
208 16214896 : mvr2r( mem_decim2, tmp_mem, 3 );
209 16214896 : lp_decim2( &wsp[L_FRAME], &wsp2[L_FRAME / OPL_DECIM], L_look, tmp_mem );
210 :
211 16214896 : 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 16214896 : corr_shift *= 0.5f;
221 16214896 : set_f( scaled_buf, 0, DELTA_COH - 1 );
222 16214896 : set_f( scaled_buf + ( DELTA_COH - 1 ) + len_x, 0, DELTA_COH - 1 );
223 16214896 : set_f( scaled_buf + 2 * ( DELTA_COH - 1 ) + len_x + len_x1, 0, DELTA_COH - 1 );
224 :
225 16214896 : pt1 = scale1 + DELTA_COH - 1;
226 16214896 : pt2 = pt1;
227 243223440 : for ( i = 0; i < DELTA_COH; i++ )
228 : {
229 227008544 : *pt1 = -( *old_thres ) / DELTA_COH * i + *old_thres + 1.0f;
230 227008544 : *pt2-- = *pt1++;
231 : }
232 :
233 : /*-----------------------------------------------------------------*
234 : * Estimate the new pitch by extrapolating the old pitch value
235 : * for 2 half-frames
236 : *-----------------------------------------------------------------*/
237 :
238 16214896 : old_tmp = *old_pitch + *delta_pit;
239 :
240 16214896 : if ( old_tmp > PIT_MAX / OPL_DECIM )
241 : {
242 35405 : old_tmp = PIT_MAX / OPL_DECIM;
243 : }
244 :
245 16214896 : if ( old_tmp < pit_min )
246 : {
247 3110003 : old_tmp = pit_min;
248 : }
249 :
250 16214896 : old_tmp1 = old_tmp + *delta_pit;
251 16214896 : if ( old_tmp1 > PIT_MAX / OPL_DECIM )
252 : {
253 57162 : old_tmp1 = PIT_MAX / OPL_DECIM;
254 : }
255 :
256 16214896 : if ( old_tmp1 < pit_min )
257 : {
258 198883 : old_tmp1 = pit_min;
259 : }
260 :
261 :
262 : /*-----------------------------------------------------------------*
263 : * Loop for all three half-frames (current frame + look-ahead)
264 : *-----------------------------------------------------------------*/
265 16214896 : pt_cor0 = scaled_buf + DELTA_COH - 1;
266 :
267 16214896 : pt_cor2 = pt_cor0 - pit_min + old_tmp;
268 16214896 : pt_cor4 = pt_cor0 - pit_min1 + old_tmp + ( DELTA_COH - 1 ) + len_x;
269 :
270 64859584 : for ( i = 0; i < NHFR; i++ )
271 : {
272 48644688 : pt1 = wsp2 + i * 2 * ( L_SUBFR / OPL_DECIM );
273 48644688 : pt2 = pt1 - pit_min;
274 48644688 : enr = 0.01f;
275 48644688 : pt_cor1 = pt_cor0;
276 48644688 : pt4 = pt1 - pit_min1;
277 48644688 : pt_cor3 = pt_cor0 + ( DELTA_COH - 1 ) + len_x;
278 :
279 : /*-----------------------------------------------------------------*
280 : * First two half-frames (corresponding to current frame)
281 : *-----------------------------------------------------------------*/
282 48644688 : if ( i < NHFR - 1 )
283 : {
284 32429792 : pt3 = pt1;
285 32429792 : pt5 = pt1;
286 :
287 148445074 : 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 116015282 : k = (int16_t) ( pt1 - pt3 );
295 3845441362 : for ( k = k + len[j]; k > 0; k-- )
296 : {
297 3729426080 : enr += *pt3 * *pt3;
298 3729426080 : pt3++;
299 : }
300 116015282 : enr_norm[j] = enr;
301 :
302 : /* Reduce complexity (length of 'enr1' section is equal or larger than 'enr') */
303 116015282 : pt5 = pt3;
304 116015282 : enr1 = enr;
305 :
306 : /* 2nd set */
307 116015282 : k = (int16_t) ( pt1 - pt5 );
308 1024049458 : for ( k = k + len1[j]; k > 0; k-- )
309 : {
310 908034176 : enr1 += *pt5 * *pt5;
311 908034176 : pt5++;
312 : }
313 116015282 : enr_norm1[j] = enr1;
314 : }
315 :
316 : /*-----------------------------------------------------------------*
317 : * Find correlation for the non-overlapping pitch lag values
318 : *-----------------------------------------------------------------*/
319 32429792 : k = (int16_t) ( pt2 - pt1 );
320 138401034 : for ( k = k + pit_max[subsect0]; k >= 0; k-- )
321 : {
322 105971242 : *pt_cor1++ = dotp( pt1, pt2--, sublen[0] );
323 : }
324 :
325 : /*-----------------------------------------------------------------*
326 : * For each subsection, find the correlation
327 : *-----------------------------------------------------------------*/
328 232030564 : for ( j = subsect0; j < nb_subsect[i]; j++ )
329 : {
330 199600772 : len_temp = sublen[j];
331 199600772 : len_temp1 = sublen1[j];
332 :
333 199600772 : k = (int16_t) ( pt2 - pt1 );
334 199600772 : if ( len_temp < len_temp1 )
335 : {
336 1070183136 : for ( k = k + pit_max[j + 1]; k >= 0; k-- )
337 : {
338 1005323552 : cor_temp = pt1[0] * pt2[0];
339 55195505984 : for ( m = 1; m < len_temp; m++ )
340 : {
341 54190182432 : cor_temp += pt1[m] * pt2[m];
342 : }
343 1005323552 : *pt_cor1++ = cor_temp;
344 16506764128 : for ( ; m < len_temp1; m++ )
345 : {
346 15501440576 : cor_temp += pt1[m] * pt2[m];
347 : }
348 1005323552 : *pt_cor3++ = cor_temp;
349 1005323552 : pt2--;
350 : }
351 : }
352 : else
353 : {
354 2365077144 : for ( k = k + pit_max[j + 1]; k >= 0; k-- )
355 : {
356 2230335956 : cor_temp = pt1[0] * pt2[0];
357 >20531*10^7 : for ( m = 1; m < len_temp1; m++ )
358 : {
359 >20308*10^7 : cor_temp += pt1[m] * pt2[m];
360 : }
361 2230335956 : *pt_cor3++ = cor_temp;
362 23893437012 : for ( ; m < len_temp; m++ )
363 : {
364 21663101056 : cor_temp += pt1[m] * pt2[m];
365 : }
366 2230335956 : *pt_cor1++ = cor_temp;
367 2230335956 : 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 16214896 : pt6 = pt1 + L_look / OPL_DECIM - 1;
384 16214896 : pt3 = pt6;
385 16214896 : pt5 = pt6;
386 :
387 74222537 : for ( j = sect0; j < nb_sect[i]; j++ ) /* loop for each section */
388 : {
389 : /* 1st set */
390 58007641 : k = (int16_t) ( pt3 - pt6 );
391 1922720681 : for ( k = k + len[j]; k > 0; k-- )
392 : {
393 1864713040 : enr += *pt3 * *pt3;
394 1864713040 : pt3--;
395 : }
396 :
397 58007641 : enr_norm[j] = enr;
398 :
399 58007641 : pt5 = pt3;
400 58007641 : enr1 = enr;
401 :
402 : /* 2nd set */
403 58007641 : k = (int16_t) ( pt5 - pt6 );
404 512024729 : for ( k = k + len1[j]; k > 0; k-- )
405 : {
406 454017088 : enr1 += *pt5 * *pt5;
407 454017088 : pt5--;
408 : }
409 :
410 58007641 : enr_norm1[j] = enr1;
411 : }
412 :
413 : /* Set pointers */
414 16214896 : if ( sect0 == 0 )
415 : {
416 9362953 : pt2 = pt6 - pit_min;
417 9362953 : k = 2;
418 : }
419 : else
420 : {
421 6851943 : pt2 = pt6 - pit_max[1] - 1;
422 6851943 : k = pit_max[2] - pit_max[1];
423 : }
424 :
425 : /*-----------------------------------------------------------------*
426 : * Find correlation for the non-overlapping pitch lag values
427 : *-----------------------------------------------------------------*/
428 :
429 69200517 : for ( ; k > 0; k-- )
430 : {
431 52985621 : *pt_cor1 = 0;
432 2172410461 : for ( m = 0; m < sublen[0]; m++ )
433 : {
434 2119424840 : *pt_cor1 += pt6[-m] * pt2[-m];
435 : }
436 52985621 : pt_cor1++;
437 52985621 : pt2--;
438 : }
439 :
440 : /*-----------------------------------------------------------------*
441 : * For each subsection, find the correlation (overlapping pitch lag values)
442 : *-----------------------------------------------------------------*/
443 :
444 116015282 : for ( j = subsect0; j < nb_subsect[i]; j++ )
445 : {
446 99800386 : len_temp = sublen[j];
447 99800386 : len_temp1 = sublen1[j];
448 :
449 99800386 : k = pit_max[j + 1] - pit_max[j];
450 99800386 : if ( len_temp < len_temp1 )
451 : {
452 535091568 : for ( ; k > 0; k-- )
453 : {
454 502661776 : cor_temp = pt6[0] * pt2[0];
455 27597752992 : for ( m = 1; m < len_temp; m++ )
456 : {
457 27095091216 : cor_temp += pt6[-m] * pt2[-m];
458 : }
459 502661776 : *pt_cor1++ = cor_temp;
460 8253382064 : for ( ; m < len_temp1; m++ )
461 : {
462 7750720288 : cor_temp += pt6[-m] * pt2[-m];
463 : }
464 502661776 : *pt_cor3++ = cor_temp;
465 502661776 : pt2--;
466 : }
467 : }
468 : else
469 : {
470 1182538572 : for ( ; k > 0; k-- )
471 : {
472 1115167978 : cor_temp = pt6[0] * pt2[0];
473 >10265*10^7 : for ( m = 1; m < len_temp1; m++ )
474 : {
475 >10154*10^7 : cor_temp += pt6[-m] * pt2[-m];
476 : }
477 1115167978 : *pt_cor3++ = cor_temp;
478 11946718506 : for ( ; m < len_temp; m++ )
479 : {
480 10831550528 : cor_temp += pt6[-m] * pt2[-m];
481 : }
482 1115167978 : *pt_cor1++ = cor_temp;
483 1115167978 : pt2--;
484 : }
485 : }
486 : }
487 : }
488 :
489 : /* Save unscaled correlation vector */
490 48644688 : mvr2r( pt_cor0, cor_buf, len_x );
491 48644688 : 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 48644688 : pt_cor1 = pt_cor2 - ( DELTA_COH - 1 );
498 48644688 : pt_cor3 = pt_cor4 - ( DELTA_COH - 1 );
499 48644688 : pt2 = scale1;
500 :
501 1362051264 : for ( k = 0; k < 2 * DELTA_COH - 1; k++ )
502 : {
503 1313406576 : *pt_cor1++ *= ( *pt2 );
504 1313406576 : *pt_cor3++ *= ( *pt2++ );
505 : }
506 :
507 : /* update for next half-frame */
508 48644688 : pt_cor2 = pt_cor0 - pit_min + old_tmp1;
509 48644688 : 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 48644688 : pt_cor1 = pt_cor0;
516 48644688 : offset = 0;
517 48644688 : pt_cor3 = pt_cor0 + ( DELTA_COH - 1 ) + len_x;
518 48644688 : offset1 = 0;
519 222667611 : for ( j = sect0; j < nb_sect[i]; j++ ) /* loop for each section */
520 : {
521 : /* 1st set */
522 174022923 : if ( i == 2 )
523 : {
524 58007641 : offset_la = L_look / OPL_DECIM - 1 - ( len[j] - 1 );
525 : }
526 : else
527 : {
528 116015282 : offset_la = 0;
529 : }
530 :
531 : /* 2nd set */
532 174022923 : if ( i == 2 )
533 : {
534 58007641 : offset_la1 = L_look / OPL_DECIM - 1 - ( len1[j] - 1 );
535 : }
536 : else
537 : {
538 116015282 : offset_la1 = 0;
539 : }
540 :
541 : /* 1st set of candidates */
542 174022923 : ind = maximum( pt_cor1, sec_length[j], 0 ) + offset;
543 174022923 : pitchX[i][j] = ind + pit_min;
544 174022923 : pt2 = pt1 - pitchX[i][j] + offset_la; /* selected moving vector */
545 174022923 : enr1 = dotp( pt2, pt2, len[j] ) + 0.01f;
546 174022923 : enr1 = inv_sqrt( enr_norm[j] * enr1 ); /* 1/sqrt(energy) */
547 174022923 : corX[i][j] = cor_buf[ind] * enr1; /* find best normalized correlation per section */
548 174022923 : scaledX[i][j] = pt_cor0[ind] * enr1; /* find best scaled normalized correlation per section */
549 :
550 174022923 : pt_cor1 += sec_length[j];
551 174022923 : offset = offset + sec_length[j];
552 :
553 : /* 2nd set of candidates */
554 174022923 : ind1 = maximum( pt_cor3, sec_length1[j], 0 ) + offset1;
555 174022923 : pitchX[i][j + NSECT] = ind1 + pit_min1;
556 174022923 : pt4 = pt1 - pitchX[i][j + NSECT] + offset_la1; /* selected moving vector */
557 174022923 : enr1 = dotp( pt4, pt4, len1[j] ) + 0.01f;
558 174022923 : enr1 = inv_sqrt( enr_norm1[j] * enr1 ); /* 1/sqrt(energy) */
559 174022923 : corX[i][j + NSECT] = cor_buf[ind1 + len_x] * enr1; /* find best normalized correlation per section */
560 174022923 : scaledX[i][j + NSECT] = pt_cor0[ind1 + ( DELTA_COH - 1 ) + len_x] * enr1; /* find best scaled normalized correlation per section */
561 :
562 174022923 : pt_cor3 += sec_length1[j];
563 174022923 : 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 48644688 : for ( i = 0; i < 2; i++ ) /* loop for the 2 half-frames */
572 : {
573 32429792 : fac = THRES0;
574 32429792 : 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 32429792 : 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 32429792 : if ( ( sect0 == 0 ) && ( ( pitchX[i][0] * 2 ) >= pit_min_coding ) )
578 : {
579 18725906 : 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 32429792 : fac = THRES0;
582 32429792 : 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 32429792 : 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 32429792 : if ( ( sect0 == 0 ) && ( pitchX[i][NSECT + 0] * 2 >= pit_min_coding ) )
586 : {
587 18725906 : 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 16214896 : fac = THRES0; /* the look-ahead */
592 16214896 : 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 16214896 : 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 16214896 : if ( ( sect0 == 0 ) && ( pitchX[i][0] * 2 >= pit_min_coding ) )
596 : {
597 9362953 : 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 16214896 : fac = THRES0; /* the look-ahead */
601 16214896 : 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 16214896 : 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 16214896 : if ( ( sect0 == 0 ) && ( pitchX[i][NSECT + 0] * 2 >= pit_min_coding ) )
605 : {
606 9362953 : 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 64859584 : for ( i = 0; i < NHFR; i++ )
617 : {
618 48644688 : ind = maximum( scaledX[i] + sect0, (int16_t) ( NSECT - sect0 ), 0 );
619 48644688 : ind = ind + sect0;
620 48644688 : ind_tmp[i] = ind;
621 48644688 : pitch_tmp[i] = pitchX[i][ind];
622 48644688 : cor_tmp[i] = corX[i][ind];
623 48644688 : cor_tmp[i] += corr_shift;
624 :
625 48644688 : if ( cor_tmp[i] > 1.0f )
626 : {
627 16969 : cor_tmp[i] = 1.0f;
628 : }
629 48644688 : thres1[i] = THRES1 * cor_tmp[i]; /* Higher is the neighbour's correlation, higher is the weighting */
630 :
631 : /* 2nd set of pitch candidates */
632 48644688 : ind1 = maximum( scaledX[i] + sect0 + NSECT, (int16_t) ( NSECT - sect0 ), 0 );
633 48644688 : ind1 += ( sect0 + NSECT );
634 48644688 : ind_tmp[i + NHFR] = ind1;
635 48644688 : pitch_tmp[i + NHFR] = pitchX[i][ind1];
636 48644688 : cor_tmp[i + NHFR] = corX[i][ind1];
637 48644688 : cor_tmp[i + NHFR] += corr_shift;
638 :
639 48644688 : if ( cor_tmp[i + NHFR] > 1.0f )
640 : {
641 16499 : cor_tmp[i + NHFR] = 1.0f;
642 : }
643 48644688 : 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 16214896 : pitch_neighbour( sect0, pitch_tmp, pitchX, cor_tmp, scaledX, thres1, ind_tmp );
652 64859584 : for ( i = 0; i < NHFR; i++ )
653 : {
654 48644688 : ind = maximum( scaledX[i] + sect0, (int16_t) ( NSECT - sect0 ), 0 );
655 48644688 : ind = ind + sect0;
656 48644688 : ind_corX = maximum( corX[i] + sect0, (int16_t) ( NSECT - sect0 ), 0 );
657 48644688 : ind_corX = ind_corX + sect0;
658 :
659 48644688 : ind1 = maximum( scaledX[i] + sect0 + NSECT, (int16_t) ( NSECT - sect0 ), 0 );
660 48644688 : ind1 += ( sect0 + NSECT );
661 48644688 : ind1_corX = maximum( corX[i] + sect0 + NSECT, (int16_t) ( NSECT - sect0 ), 0 );
662 48644688 : ind1_corX += ( sect0 + NSECT );
663 :
664 48644688 : if ( scaledX[i][ind1] > scaledX[i][ind] )
665 : {
666 18571972 : ind = ind1;
667 : }
668 :
669 48644688 : if ( Opt_SC_VBR && corX[i][ind1_corX] > corX[i][ind_corX] )
670 : {
671 2239 : ind_corX = ind1_corX;
672 : }
673 :
674 48644688 : 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 18 : pitch[i] = pitchX[i][ind_corX];
677 18 : voicing[i] = corX[i][ind_corX];
678 : }
679 : else
680 : {
681 48644670 : pitch[i] = pitchX[i][ind];
682 48644670 : 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 16214896 : cor_mean = 0.5f * ( voicing[0] + voicing[1] ) + corr_shift;
692 16214896 : if ( cor_mean > 1.0f )
693 : {
694 3749 : cor_mean = 1.0f;
695 : }
696 :
697 : /* pitch unstable in present frame or from previous frame or normalized correlation too low */
698 16214896 : coh_flag = pitch_coherence( (int16_t) pitch[0], (int16_t) pitch[1], COH_FAC, DELTA_COH );
699 16214896 : coh_flag1 = pitch_coherence( (int16_t) pitch[0], (int16_t) *old_pitch, COH_FAC, DELTA_COH );
700 16214896 : if ( ( coh_flag == 0 ) || ( coh_flag1 == 0 ) || ( cor_mean < CORR_TH0 ) || ( relE < THR_relE ) )
701 : {
702 9733551 : *old_thres = 0.0f; /* Reset the threshold */
703 : }
704 : else
705 : {
706 6481345 : *old_thres += ( 0.16f * cor_mean ); /* The threshold increase is directly dependent on normalized correlation */
707 : }
708 :
709 16214896 : if ( *old_thres > THRES3 )
710 : {
711 2786252 : *old_thres = THRES3;
712 : }
713 :
714 16214896 : if ( voicing[1] > voicing[0] )
715 : {
716 7629385 : *old_corr = voicing[1];
717 : }
718 : else
719 : {
720 8585511 : *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 16214896 : tmp_buf[0] = *old_pitch;
731 64859584 : for ( i = 0; i < NHFR; i++ )
732 : {
733 48644688 : tmp_buf[i + 1] = pitch[i];
734 : }
735 :
736 16214896 : *delta_pit = 0;
737 16214896 : cnt = 0;
738 64859584 : for ( i = 0; i < NHFR; i++ )
739 : {
740 48644688 : diff = tmp_buf[i + 1] - tmp_buf[i];
741 48644688 : coh_flag = pitch_coherence( (int16_t) tmp_buf[i], (int16_t) tmp_buf[i + 1], COH_FAC, DELTA_COH );
742 48644688 : if ( coh_flag != 0 )
743 : {
744 31994797 : *delta_pit = *delta_pit + diff;
745 31994797 : cnt++;
746 : }
747 : }
748 :
749 16214896 : if ( cnt == 2 )
750 : {
751 3854767 : *delta_pit /= 2;
752 : }
753 :
754 16214896 : if ( cnt == 3 )
755 : {
756 7009651 : *delta_pit /= 3;
757 : }
758 :
759 : /*-----------------------------------------------------------------*
760 : * update old pitch, upsample pitch
761 : *-----------------------------------------------------------------*/
762 :
763 16214896 : *old_pitch = pitch[1];
764 :
765 64859584 : for ( i = 0; i < NHFR; i++ )
766 : {
767 48644688 : pitch[i] *= OPL_DECIM;
768 : }
769 :
770 :
771 16214896 : 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 250756470 : 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 250756470 : pit_min = 2 * pitch0; /* double the shorter-delay section pitch */
796 550366256 : while ( pit_min <= pit_max0 + (int16_t) delta ) /* check for section boundary */
797 : {
798 299609786 : if ( abs( pit_min - pitch1 ) <= (int16_t) delta ) /* if multiple in the allowed range */
799 : {
800 82500017 : if ( *old_corr < 0.6f || (float) pitch0 > (float) *old_pitch * 0.4f )
801 : {
802 : /* reinforce the normalized correlation */
803 77528260 : *corr *= *fac;
804 : }
805 82500017 : *fac *= THRES0;
806 : }
807 299609786 : pit_min = pit_min + pitch0; /* next multiple */
808 299609786 : delta += step; /* add the incertitude to the allowed range */
809 : }
810 250756470 : return;
811 : }
812 :
813 : /*---------------------------------------------------------------------------*
814 : * pitch_neighbour
815 : *
816 : * Verifies if the maximum correlation pitch lag is coherent with neighbour
817 : * values
818 : *---------------------------------------------------------------------------*/
819 16214896 : 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 74222537 : for ( k = sect0; k < NSECT; k++ ) /* loop for each section */
832 : {
833 58007641 : if ( k == ( NSECT - 1 ) )
834 : {
835 16214896 : K = 2; /* the number of tests depends on the section */
836 : }
837 : else
838 : {
839 41792745 : K = 3;
840 : }
841 215815668 : 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 598802316 : for ( j = 0; j < K; j++ ) /* Verify pitch coherence with neighbours (including past pitch) */
845 : {
846 440994289 : if ( j != i ) /* Exclude itself */
847 : {
848 283186262 : if ( corr_tmp[j] >= CORR_TH1 ) /* reinforcement can happen only if the correlation is high enough */
849 : {
850 176842811 : delta = (int16_t) abs( pitch[i][k] - pitch_tmp[j] ); /* Find difference of pitch values */
851 176842811 : coh_flag = pitch_coherence( (int16_t) pitch[i][k], (int16_t) pitch_tmp[j], COH_FAC, DELTA_COH );
852 176842811 : if ( coh_flag != 0 )
853 : {
854 : /* Favour section-wise stability */
855 63199340 : if ( ind_tmp[j] == k )
856 : {
857 47684060 : corr[i][k] *= ( -thres1[j] / DELTA_COH * delta + thres1[j] + 1.0f ); /* Favour closer values */
858 : }
859 : else
860 : {
861 15515280 : 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 74222537 : for ( k = sect0; k < NSECT; k++ ) /* loop for each section */
873 : {
874 58007641 : if ( k == ( NSECT - 1 ) )
875 : {
876 16214896 : K = 2; /* the number of tests depends on the section */
877 : }
878 : else
879 : {
880 41792745 : K = 3;
881 : }
882 215815668 : 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 598802316 : for ( j = 0; j < K; j++ ) /* Verify pitch coherence with neighbours (including past pitch) */
886 : {
887 440994289 : if ( j != i ) /* Exclude itself */
888 : {
889 283186262 : if ( corr_tmp[j + NHFR] >= CORR_TH1 ) /* reinforcement can happen only if the correlation is high enough */
890 : {
891 171179166 : delta = (int16_t) abs( pitch[i][NSECT + k] - pitch_tmp[j + NHFR] ); /* Find difference of pitch values */
892 171179166 : coh_flag = pitch_coherence( (int16_t) pitch[i][NSECT + k], (int16_t) pitch_tmp[j + NHFR], COH_FAC, DELTA_COH );
893 171179166 : if ( coh_flag != 0 )
894 : {
895 : /* Favour section-wise stability */
896 63672034 : if ( ind_tmp[j + NHFR] == ( NSECT + k ) )
897 : {
898 48662350 : corr[i][NSECT + k] *= ( -thres1[j + NHFR] / DELTA_COH * delta + thres1[j + NHFR] + 1.0f ); /* Favour closer values */
899 : }
900 : else
901 : {
902 15009684 : 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 16214896 : return;
911 : }
912 :
913 : /*-----------------------------------------------------------------*
914 : * pitch_coherence
915 : *
916 : * Verify if pitch evolution is smooth
917 : *-----------------------------------------------------------------*/
918 :
919 429096457 : 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 429096457 : if ( pitch1 < pitch0 ) /* Finds smaller and larger of 2 short values */
928 : {
929 176724928 : smaller = pitch1;
930 176724928 : larger = pitch0;
931 : }
932 : else
933 : {
934 252371529 : smaller = pitch0;
935 252371529 : larger = pitch1;
936 : }
937 429096457 : if ( ( (float) larger < fac_max * (float) smaller ) && ( ( larger - smaller ) < diff_max ) )
938 : {
939 179228509 : return 1;
940 : }
941 : else
942 : {
943 249867948 : 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 32429792 : 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 32429792 : p_x = x_buf;
967 129719168 : for ( i = 0; i < L_MEM; i++ )
968 : {
969 97289376 : *p_x++ = mem[i];
970 : }
971 5999511520 : for ( i = 0; i < l; i++ )
972 : {
973 5967081728 : *p_x++ = x[i];
974 : }
975 129719168 : for ( i = 0; i < L_MEM; i++ )
976 : {
977 97289376 : mem[i] = x[l - L_MEM + i];
978 : }
979 3015970656 : for ( i = 0, j = 0; i < l; i += 2, j++ )
980 : {
981 2983540864 : p_x = &x_buf[i];
982 2983540864 : p_h = h_fir;
983 2983540864 : temp = 0.0f;
984 :
985 17901245184 : for ( k = 0; k < L_FIR_PO; k++ )
986 : {
987 14917704320 : temp += *p_x++ * *p_h++;
988 : }
989 2983540864 : y[j] = temp;
990 : }
991 32429792 : return;
992 : }
|