1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
|
/***********************************************************************
Copyright (c) 2006-2011, Skype Limited. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, (subject to the limitations in the disclaimer below)
are permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
- Neither the name of Skype Limited, nor the names of specific
contributors, may be used to endorse or promote products derived from
this software without specific prior written permission.
NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED
BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
***********************************************************************/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
/***********************************************************
* Pitch analyser function
********************************************************** */
#include "silk_SigProc_FIX.h"
#include "silk_pitch_est_defines.h"
#include "silk_debug.h"
#define SCRATCH_SIZE 22
/************************************************************/
/* Internally used functions */
/************************************************************/
void silk_P_Ana_calc_corr_st3(
opus_int32 cross_corr_st3[ PE_MAX_NB_SUBFR ][ PE_NB_CBKS_STAGE3_MAX ][ PE_NB_STAGE3_LAGS ],/* (O) 3 DIM correlation array */
const opus_int16 frame[], /* I vector to correlate */
opus_int start_lag, /* I lag offset to search around */
opus_int sf_length, /* I length of a 5 ms subframe */
opus_int nb_subfr, /* I number of subframes */
opus_int complexity /* I Complexity setting */
);
void silk_P_Ana_calc_energy_st3(
opus_int32 energies_st3[ PE_MAX_NB_SUBFR ][ PE_NB_CBKS_STAGE3_MAX ][ PE_NB_STAGE3_LAGS ],/* (O) 3 DIM energy array */
const opus_int16 frame[], /* I vector to calc energy in */
opus_int start_lag, /* I lag offset to search around */
opus_int sf_length, /* I length of one 5 ms subframe */
opus_int nb_subfr, /* I number of subframes */
opus_int complexity /* I Complexity setting */
);
opus_int32 silk_P_Ana_find_scaling(
const opus_int16 *frame,
const opus_int frame_length,
const opus_int sum_sqr_len
);
/*************************************************************/
/* FIXED POINT CORE PITCH ANALYSIS FUNCTION */
/*************************************************************/
opus_int silk_pitch_analysis_core( /* O Voicing estimate: 0 voiced, 1 unvoiced */
const opus_int16 *frame, /* I Signal of length PE_FRAME_LENGTH_MS*Fs_kHz */
opus_int *pitch_out, /* O 4 pitch lag values */
opus_int16 *lagIndex, /* O Lag Index */
opus_int8 *contourIndex, /* O Pitch contour Index */
opus_int *LTPCorr_Q15, /* I/O Normalized correlation; input: value from previous frame */
opus_int prevLag, /* I Last lag of previous frame; set to zero is unvoiced */
const opus_int32 search_thres1_Q16, /* I First stage threshold for lag candidates 0 - 1 */
const opus_int search_thres2_Q15, /* I Final threshold for lag candidates 0 - 1 */
const opus_int Fs_kHz, /* I Sample frequency (kHz) */
const opus_int complexity, /* I Complexity setting, 0-2, where 2 is highest */
const opus_int nb_subfr /* I number of 5 ms subframes */
)
{
opus_int16 frame_8kHz[ PE_MAX_FRAME_LENGTH_ST_2 ];
opus_int16 frame_4kHz[ PE_MAX_FRAME_LENGTH_ST_1 ];
opus_int32 filt_state[ 6 ];
opus_int32 scratch_mem[ 3 * PE_MAX_FRAME_LENGTH ];
opus_int16 *input_frame_ptr;
opus_int i, k, d, j;
opus_int16 C[ PE_MAX_NB_SUBFR ][ ( PE_MAX_LAG >> 1 ) + 5 ];
const opus_int16 *target_ptr, *basis_ptr;
opus_int32 cross_corr, normalizer, energy, shift, energy_basis, energy_target;
opus_int d_srch[ PE_D_SRCH_LENGTH ], Cmax, length_d_srch, length_d_comp;
opus_int16 d_comp[ ( PE_MAX_LAG >> 1 ) + 5 ];
opus_int32 sum, threshold, temp32, lag_counter;
opus_int CBimax, CBimax_new, CBimax_old, lag, start_lag, end_lag, lag_new;
opus_int32 CC[ PE_NB_CBKS_STAGE2_EXT ], CCmax, CCmax_b, CCmax_new_b, CCmax_new;
opus_int32 energies_st3[ PE_MAX_NB_SUBFR ][ PE_NB_CBKS_STAGE3_MAX ][ PE_NB_STAGE3_LAGS ];
opus_int32 crosscorr_st3[ PE_MAX_NB_SUBFR ][ PE_NB_CBKS_STAGE3_MAX ][ PE_NB_STAGE3_LAGS ];
opus_int frame_length, frame_length_8kHz, frame_length_4kHz, max_sum_sq_length;
opus_int sf_length, sf_length_8kHz, sf_length_4kHz;
opus_int min_lag, min_lag_8kHz, min_lag_4kHz;
opus_int max_lag, max_lag_8kHz, max_lag_4kHz;
opus_int32 contour_bias_Q20, diff, lz, lshift;
opus_int nb_cbk_search, cbk_size;
opus_int32 delta_lag_log2_sqr_Q7, lag_log2_Q7, prevLag_log2_Q7, prev_lag_bias_Q15, corr_thres_Q15;
const opus_int8 *Lag_CB_ptr;
/* Check for valid sampling frequency */
SKP_assert( Fs_kHz == 8 || Fs_kHz == 12 || Fs_kHz == 16 );
/* Check for valid complexity setting */
SKP_assert( complexity >= SILK_PE_MIN_COMPLEX );
SKP_assert( complexity <= SILK_PE_MAX_COMPLEX );
SKP_assert( search_thres1_Q16 >= 0 && search_thres1_Q16 <= (1<<16) );
SKP_assert( search_thres2_Q15 >= 0 && search_thres2_Q15 <= (1<<15) );
/* Setup frame lengths max / min lag for the sampling frequency */
frame_length = ( PE_LTP_MEM_LENGTH_MS + nb_subfr * PE_SUBFR_LENGTH_MS ) * Fs_kHz;
frame_length_4kHz = ( PE_LTP_MEM_LENGTH_MS + nb_subfr * PE_SUBFR_LENGTH_MS ) * 4;
frame_length_8kHz = ( PE_LTP_MEM_LENGTH_MS + nb_subfr * PE_SUBFR_LENGTH_MS ) * 8;
sf_length = PE_SUBFR_LENGTH_MS * Fs_kHz;
sf_length_4kHz = PE_SUBFR_LENGTH_MS * 4;
sf_length_8kHz = PE_SUBFR_LENGTH_MS * 8;
min_lag = PE_MIN_LAG_MS * Fs_kHz;
min_lag_4kHz = PE_MIN_LAG_MS * 4;
min_lag_8kHz = PE_MIN_LAG_MS * 8;
max_lag = PE_MAX_LAG_MS * Fs_kHz - 1;
max_lag_4kHz = PE_MAX_LAG_MS * 4;
max_lag_8kHz = PE_MAX_LAG_MS * 8 - 1;
SKP_memset( C, 0, sizeof( opus_int16 ) * nb_subfr * ( ( PE_MAX_LAG >> 1 ) + 5) );
/* Resample from input sampled at Fs_kHz to 8 kHz */
if( Fs_kHz == 16 ) {
SKP_memset( filt_state, 0, 2 * sizeof( opus_int32 ) );
silk_resampler_down2( filt_state, frame_8kHz, frame, frame_length );
} else if ( Fs_kHz == 12 ) {
SKP_memset( filt_state, 0, 6 * sizeof( opus_int32 ) );
silk_resampler_down2_3( filt_state, frame_8kHz, frame, frame_length );
} else {
SKP_assert( Fs_kHz == 8 );
SKP_memcpy( frame_8kHz, frame, frame_length_8kHz * sizeof(opus_int16) );
}
/* Decimate again to 4 kHz */
SKP_memset( filt_state, 0, 2 * sizeof( opus_int32 ) );/* Set state to zero */
silk_resampler_down2( filt_state, frame_4kHz, frame_8kHz, frame_length_8kHz );
/* Low-pass filter */
for( i = frame_length_4kHz - 1; i > 0; i-- ) {
frame_4kHz[ i ] = SKP_ADD_SAT16( frame_4kHz[ i ], frame_4kHz[ i - 1 ] );
}
/*******************************************************************************
** Scale 4 kHz signal down to prevent correlations measures from overflowing
** find scaling as max scaling for each 8kHz(?) subframe
*******************************************************************************/
/* Inner product is calculated with different lengths, so scale for the worst case */
max_sum_sq_length = SKP_max_32( sf_length_8kHz, SKP_LSHIFT( sf_length_4kHz, 2 ) );
shift = silk_P_Ana_find_scaling( frame_4kHz, frame_length_4kHz, max_sum_sq_length );
if( shift > 0 ) {
for( i = 0; i < frame_length_4kHz; i++ ) {
frame_4kHz[ i ] = SKP_RSHIFT( frame_4kHz[ i ], shift );
}
}
/******************************************************************************
* FIRST STAGE, operating in 4 khz
******************************************************************************/
target_ptr = &frame_4kHz[ SKP_LSHIFT( sf_length_4kHz, 2 ) ];
for( k = 0; k < nb_subfr >> 1; k++ ) {
/* Check that we are within range of the array */
SKP_assert( target_ptr >= frame_4kHz );
SKP_assert( target_ptr + sf_length_8kHz <= frame_4kHz + frame_length_4kHz );
basis_ptr = target_ptr - min_lag_4kHz;
/* Check that we are within range of the array */
SKP_assert( basis_ptr >= frame_4kHz );
SKP_assert( basis_ptr + sf_length_8kHz <= frame_4kHz + frame_length_4kHz );
normalizer = 0;
cross_corr = 0;
/* Calculate first vector products before loop */
cross_corr = silk_inner_prod_aligned( target_ptr, basis_ptr, sf_length_8kHz );
normalizer = silk_inner_prod_aligned( basis_ptr, basis_ptr, sf_length_8kHz );
normalizer = SKP_ADD_SAT32( normalizer, SKP_SMULBB( sf_length_8kHz, 4000 ) );
temp32 = SKP_DIV32( cross_corr, silk_SQRT_APPROX( normalizer ) + 1 );
C[ k ][ min_lag_4kHz ] = (opus_int16)SKP_SAT16( temp32 ); /* Q0 */
/* From now on normalizer is computed recursively */
for( d = min_lag_4kHz + 1; d <= max_lag_4kHz; d++ ) {
basis_ptr--;
/* Check that we are within range of the array */
SKP_assert( basis_ptr >= frame_4kHz );
SKP_assert( basis_ptr + sf_length_8kHz <= frame_4kHz + frame_length_4kHz );
cross_corr = silk_inner_prod_aligned( target_ptr, basis_ptr, sf_length_8kHz );
/* Add contribution of new sample and remove contribution from oldest sample */
normalizer +=
SKP_SMULBB( basis_ptr[ 0 ], basis_ptr[ 0 ] ) -
SKP_SMULBB( basis_ptr[ sf_length_8kHz ], basis_ptr[ sf_length_8kHz ] );
temp32 = SKP_DIV32( cross_corr, silk_SQRT_APPROX( normalizer ) + 1 );
C[ k ][ d ] = (opus_int16)SKP_SAT16( temp32 ); /* Q0 */
}
/* Update target pointer */
target_ptr += sf_length_8kHz;
}
/* Combine two subframes into single correlation measure and apply short-lag bias */
if( nb_subfr == PE_MAX_NB_SUBFR ) {
for( i = max_lag_4kHz; i >= min_lag_4kHz; i-- ) {
sum = (opus_int32)C[ 0 ][ i ] + (opus_int32)C[ 1 ][ i ]; /* Q0 */
SKP_assert( SKP_RSHIFT( sum, 1 ) == SKP_SAT16( SKP_RSHIFT( sum, 1 ) ) );
sum = SKP_RSHIFT( sum, 1 ); /* Q-1 */
SKP_assert( SKP_LSHIFT( (opus_int32)-i, 4 ) == SKP_SAT16( SKP_LSHIFT( (opus_int32)-i, 4 ) ) );
sum = SKP_SMLAWB( sum, sum, SKP_LSHIFT( -i, 4 ) ); /* Q-1 */
SKP_assert( sum == SKP_SAT16( sum ) );
C[ 0 ][ i ] = (opus_int16)sum; /* Q-1 */
}
} else {
/* Only short-lag bias */
for( i = max_lag_4kHz; i >= min_lag_4kHz; i-- ) {
sum = (opus_int32)C[ 0 ][ i ];
sum = SKP_SMLAWB( sum, sum, SKP_LSHIFT( -i, 4 ) ); /* Q-1 */
C[ 0 ][ i ] = (opus_int16)sum; /* Q-1 */
}
}
/* Sort */
length_d_srch = SKP_ADD_LSHIFT32( 4, complexity, 1 );
SKP_assert( 3 * length_d_srch <= PE_D_SRCH_LENGTH );
silk_insertion_sort_decreasing_int16( &C[ 0 ][ min_lag_4kHz ], d_srch, max_lag_4kHz - min_lag_4kHz + 1, length_d_srch );
/* Escape if correlation is very low already here */
target_ptr = &frame_4kHz[ SKP_SMULBB( sf_length_4kHz, nb_subfr ) ];
energy = silk_inner_prod_aligned( target_ptr, target_ptr, SKP_LSHIFT( sf_length_4kHz, 2 ) );
energy = SKP_ADD_SAT32( energy, 1000 ); /* Q0 */
Cmax = (opus_int)C[ 0 ][ min_lag_4kHz ]; /* Q-1 */
threshold = SKP_SMULBB( Cmax, Cmax ); /* Q-2 */
/* Compare in Q-2 domain */
if( SKP_RSHIFT( energy, 4 + 2 ) > threshold ) {
SKP_memset( pitch_out, 0, nb_subfr * sizeof( opus_int ) );
*LTPCorr_Q15 = 0;
*lagIndex = 0;
*contourIndex = 0;
return 1;
}
threshold = SKP_SMULWB( search_thres1_Q16, Cmax );
for( i = 0; i < length_d_srch; i++ ) {
/* Convert to 8 kHz indices for the sorted correlation that exceeds the threshold */
if( C[ 0 ][ min_lag_4kHz + i ] > threshold ) {
d_srch[ i ] = SKP_LSHIFT( d_srch[ i ] + min_lag_4kHz, 1 );
} else {
length_d_srch = i;
break;
}
}
SKP_assert( length_d_srch > 0 );
for( i = min_lag_8kHz - 5; i < max_lag_8kHz + 5; i++ ) {
d_comp[ i ] = 0;
}
for( i = 0; i < length_d_srch; i++ ) {
d_comp[ d_srch[ i ] ] = 1;
}
/* Convolution */
for( i = max_lag_8kHz + 3; i >= min_lag_8kHz; i-- ) {
d_comp[ i ] += d_comp[ i - 1 ] + d_comp[ i - 2 ];
}
length_d_srch = 0;
for( i = min_lag_8kHz; i < max_lag_8kHz + 1; i++ ) {
if( d_comp[ i + 1 ] > 0 ) {
d_srch[ length_d_srch ] = i;
length_d_srch++;
}
}
/* Convolution */
for( i = max_lag_8kHz + 3; i >= min_lag_8kHz; i-- ) {
d_comp[ i ] += d_comp[ i - 1 ] + d_comp[ i - 2 ] + d_comp[ i - 3 ];
}
length_d_comp = 0;
for( i = min_lag_8kHz; i < max_lag_8kHz + 4; i++ ) {
if( d_comp[ i ] > 0 ) {
d_comp[ length_d_comp ] = i - 2;
length_d_comp++;
}
}
/**********************************************************************************
** SECOND STAGE, operating at 8 kHz, on lag sections with high correlation
*************************************************************************************/
/******************************************************************************
** Scale signal down to avoid correlations measures from overflowing
*******************************************************************************/
/* find scaling as max scaling for each subframe */
shift = silk_P_Ana_find_scaling( frame_8kHz, frame_length_8kHz, sf_length_8kHz );
if( shift > 0 ) {
for( i = 0; i < frame_length_8kHz; i++ ) {
frame_8kHz[ i ] = SKP_RSHIFT( frame_8kHz[ i ], shift );
}
}
/*********************************************************************************
* Find energy of each subframe projected onto its history, for a range of delays
*********************************************************************************/
SKP_memset( C, 0, PE_MAX_NB_SUBFR * ( ( PE_MAX_LAG >> 1 ) + 5 ) * sizeof( opus_int16 ) );
target_ptr = &frame_8kHz[ PE_LTP_MEM_LENGTH_MS * 8 ];
for( k = 0; k < nb_subfr; k++ ) {
/* Check that we are within range of the array */
SKP_assert( target_ptr >= frame_8kHz );
SKP_assert( target_ptr + sf_length_8kHz <= frame_8kHz + frame_length_8kHz );
energy_target = silk_inner_prod_aligned( target_ptr, target_ptr, sf_length_8kHz );
/* ToDo: Calculate 1 / energy_target here and save one division inside next for loop*/
for( j = 0; j < length_d_comp; j++ ) {
d = d_comp[ j ];
basis_ptr = target_ptr - d;
/* Check that we are within range of the array */
SKP_assert( basis_ptr >= frame_8kHz );
SKP_assert( basis_ptr + sf_length_8kHz <= frame_8kHz + frame_length_8kHz );
cross_corr = silk_inner_prod_aligned( target_ptr, basis_ptr, sf_length_8kHz );
energy_basis = silk_inner_prod_aligned( basis_ptr, basis_ptr, sf_length_8kHz );
if( cross_corr > 0 ) {
energy = SKP_max( energy_target, energy_basis ); /* Find max to make sure first division < 1.0 */
lz = silk_CLZ32( cross_corr );
lshift = SKP_LIMIT_32( lz - 1, 0, 15 );
temp32 = SKP_DIV32( SKP_LSHIFT( cross_corr, lshift ), SKP_RSHIFT( energy, 15 - lshift ) + 1 ); /* Q15 */
SKP_assert( temp32 == SKP_SAT16( temp32 ) );
temp32 = SKP_SMULWB( cross_corr, temp32 ); /* Q(-1), cc * ( cc / max(b, t) ) */
temp32 = SKP_ADD_SAT32( temp32, temp32 ); /* Q(0) */
lz = silk_CLZ32( temp32 );
lshift = SKP_LIMIT_32( lz - 1, 0, 15 );
energy = SKP_min( energy_target, energy_basis );
C[ k ][ d ] = SKP_DIV32( SKP_LSHIFT( temp32, lshift ), SKP_RSHIFT( energy, 15 - lshift ) + 1 ); /* Q15*/
} else {
C[ k ][ d ] = 0;
}
}
target_ptr += sf_length_8kHz;
}
/* search over lag range and lags codebook */
/* scale factor for lag codebook, as a function of center lag */
CCmax = SKP_int32_MIN;
CCmax_b = SKP_int32_MIN;
CBimax = 0; /* To avoid returning undefined lag values */
lag = -1; /* To check if lag with strong enough correlation has been found */
if( prevLag > 0 ) {
if( Fs_kHz == 12 ) {
prevLag = SKP_DIV32_16( SKP_LSHIFT( prevLag, 1 ), 3 );
} else if( Fs_kHz == 16 ) {
prevLag = SKP_RSHIFT( prevLag, 1 );
}
prevLag_log2_Q7 = silk_lin2log( (opus_int32)prevLag );
} else {
prevLag_log2_Q7 = 0;
}
SKP_assert( search_thres2_Q15 == SKP_SAT16( search_thres2_Q15 ) );
/* Setup stage 2 codebook based on number of subframes */
if( nb_subfr == PE_MAX_NB_SUBFR ) {
cbk_size = PE_NB_CBKS_STAGE2_EXT;
Lag_CB_ptr = &silk_CB_lags_stage2[ 0 ][ 0 ];
if( Fs_kHz == 8 && complexity > SILK_PE_MIN_COMPLEX ) {
/* If input is 8 khz use a larger codebook here because it is last stage */
nb_cbk_search = PE_NB_CBKS_STAGE2_EXT;
} else {
nb_cbk_search = PE_NB_CBKS_STAGE2;
}
corr_thres_Q15 = SKP_RSHIFT( SKP_SMULBB( search_thres2_Q15, search_thres2_Q15 ), 13 );
} else {
cbk_size = PE_NB_CBKS_STAGE2_10MS;
Lag_CB_ptr = &silk_CB_lags_stage2_10_ms[ 0 ][ 0 ];
nb_cbk_search = PE_NB_CBKS_STAGE2_10MS;
corr_thres_Q15 = SKP_RSHIFT( SKP_SMULBB( search_thres2_Q15, search_thres2_Q15 ), 14 );
}
for( k = 0; k < length_d_srch; k++ ) {
d = d_srch[ k ];
for( j = 0; j < nb_cbk_search; j++ ) {
CC[ j ] = 0;
for( i = 0; i < nb_subfr; i++ ) {
/* Try all codebooks */
CC[ j ] = CC[ j ] + (opus_int32)C[ i ][ d + matrix_ptr( Lag_CB_ptr, i, j, cbk_size )];
}
}
/* Find best codebook */
CCmax_new = SKP_int32_MIN;
CBimax_new = 0;
for( i = 0; i < nb_cbk_search; i++ ) {
if( CC[ i ] > CCmax_new ) {
CCmax_new = CC[ i ];
CBimax_new = i;
}
}
/* Bias towards shorter lags */
lag_log2_Q7 = silk_lin2log( (opus_int32)d ); /* Q7 */
SKP_assert( lag_log2_Q7 == SKP_SAT16( lag_log2_Q7 ) );
SKP_assert( nb_subfr * SILK_FIX_CONST( PE_SHORTLAG_BIAS, 15 ) == SKP_SAT16( nb_subfr * SILK_FIX_CONST( PE_SHORTLAG_BIAS, 15 ) ) );
CCmax_new_b = CCmax_new - SKP_RSHIFT( SKP_SMULBB( nb_subfr * SILK_FIX_CONST( PE_SHORTLAG_BIAS, 15 ), lag_log2_Q7 ), 7 ); /* Q15 */
/* Bias towards previous lag */
SKP_assert( nb_subfr * SILK_FIX_CONST( PE_PREVLAG_BIAS, 15 ) == SKP_SAT16( nb_subfr * SILK_FIX_CONST( PE_PREVLAG_BIAS, 15 ) ) );
if( prevLag > 0 ) {
delta_lag_log2_sqr_Q7 = lag_log2_Q7 - prevLag_log2_Q7;
SKP_assert( delta_lag_log2_sqr_Q7 == SKP_SAT16( delta_lag_log2_sqr_Q7 ) );
delta_lag_log2_sqr_Q7 = SKP_RSHIFT( SKP_SMULBB( delta_lag_log2_sqr_Q7, delta_lag_log2_sqr_Q7 ), 7 );
prev_lag_bias_Q15 = SKP_RSHIFT( SKP_SMULBB( nb_subfr * SILK_FIX_CONST( PE_PREVLAG_BIAS, 15 ), *LTPCorr_Q15 ), 15 ); /* Q15 */
prev_lag_bias_Q15 = SKP_DIV32( SKP_MUL( prev_lag_bias_Q15, delta_lag_log2_sqr_Q7 ), delta_lag_log2_sqr_Q7 + ( 1 << 6 ) );
CCmax_new_b -= prev_lag_bias_Q15; /* Q15 */
}
if ( CCmax_new_b > CCmax_b && /* Find maximum biased correlation */
CCmax_new > corr_thres_Q15 && /* Correlation needs to be high enough to be voiced */
silk_CB_lags_stage2[ 0 ][ CBimax_new ] <= min_lag_8kHz /* Lag must be in range */
) {
CCmax_b = CCmax_new_b;
CCmax = CCmax_new;
lag = d;
CBimax = CBimax_new;
}
}
if( lag == -1 ) {
/* No suitable candidate found */
SKP_memset( pitch_out, 0, nb_subfr * sizeof( opus_int ) );
*LTPCorr_Q15 = 0;
*lagIndex = 0;
*contourIndex = 0;
return 1;
}
if( Fs_kHz > 8 ) {
/******************************************************************************
** Scale input signal down to avoid correlations measures from overflowing
*******************************************************************************/
/* find scaling as max scaling for each subframe */
shift = silk_P_Ana_find_scaling( frame, frame_length, sf_length );
if( shift > 0 ) {
/* Move signal to scratch mem because the input signal should be unchanged */
/* Reuse the 32 bit scratch mem vector, use a 16 bit pointer from now */
input_frame_ptr = (opus_int16*)scratch_mem;
for( i = 0; i < frame_length; i++ ) {
input_frame_ptr[ i ] = SKP_RSHIFT( frame[ i ], shift );
}
} else {
input_frame_ptr = (opus_int16*)frame;
}
/*********************************************************************************/
/* Search in original signal */
CBimax_old = CBimax;
/* Compensate for decimation */
SKP_assert( lag == SKP_SAT16( lag ) );
if( Fs_kHz == 12 ) {
lag = SKP_RSHIFT( SKP_SMULBB( lag, 3 ), 1 );
} else if( Fs_kHz == 16 ) {
lag = SKP_LSHIFT( lag, 1 );
} else {
lag = SKP_SMULBB( lag, 3 );
}
lag = SKP_LIMIT_int( lag, min_lag, max_lag );
start_lag = SKP_max_int( lag - 2, min_lag );
end_lag = SKP_min_int( lag + 2, max_lag );
lag_new = lag; /* to avoid undefined lag */
CBimax = 0; /* to avoid undefined lag */
SKP_assert( SKP_LSHIFT( CCmax, 13 ) >= 0 );
*LTPCorr_Q15 = (opus_int)silk_SQRT_APPROX( SKP_LSHIFT( CCmax, 13 ) ); /* Output normalized correlation */
CCmax = SKP_int32_MIN;
/* pitch lags according to second stage */
for( k = 0; k < nb_subfr; k++ ) {
pitch_out[ k ] = lag + 2 * silk_CB_lags_stage2[ k ][ CBimax_old ];
}
/* Calculate the correlations and energies needed in stage 3 */
silk_P_Ana_calc_corr_st3( crosscorr_st3, input_frame_ptr, start_lag, sf_length, nb_subfr, complexity );
silk_P_Ana_calc_energy_st3( energies_st3, input_frame_ptr, start_lag, sf_length, nb_subfr, complexity );
lag_counter = 0;
SKP_assert( lag == SKP_SAT16( lag ) );
contour_bias_Q20 = SKP_DIV32_16( SILK_FIX_CONST( PE_FLATCONTOUR_BIAS, 20 ), lag );
/* Setup cbk parameters acording to complexity setting and frame length */
if( nb_subfr == PE_MAX_NB_SUBFR ) {
nb_cbk_search = (opus_int)silk_nb_cbk_searchs_stage3[ complexity ];
cbk_size = PE_NB_CBKS_STAGE3_MAX;
Lag_CB_ptr = &silk_CB_lags_stage3[ 0 ][ 0 ];
} else {
nb_cbk_search = PE_NB_CBKS_STAGE3_10MS;
cbk_size = PE_NB_CBKS_STAGE3_10MS;
Lag_CB_ptr = &silk_CB_lags_stage3_10_ms[ 0 ][ 0 ];
}
for( d = start_lag; d <= end_lag; d++ ) {
for( j = 0; j < nb_cbk_search; j++ ) {
cross_corr = 0;
energy = 0;
for( k = 0; k < nb_subfr; k++ ) {
SKP_assert( PE_MAX_NB_SUBFR == 4 );
energy += SKP_RSHIFT( energies_st3[ k ][ j ][ lag_counter ], 2 ); /* use mean, to avoid overflow */
SKP_assert( energy >= 0 );
cross_corr += SKP_RSHIFT( crosscorr_st3[ k ][ j ][ lag_counter ], 2 ); /* use mean, to avoid overflow */
}
if( cross_corr > 0 ) {
/* Divide cross_corr / energy and get result in Q15 */
lz = silk_CLZ32( cross_corr );
/* Divide with result in Q13, cross_corr could be larger than energy */
lshift = SKP_LIMIT_32( lz - 1, 0, 13 );
CCmax_new = SKP_DIV32( SKP_LSHIFT( cross_corr, lshift ), SKP_RSHIFT( energy, 13 - lshift ) + 1 );
CCmax_new = SKP_SAT16( CCmax_new );
CCmax_new = SKP_SMULWB( cross_corr, CCmax_new );
/* Saturate */
if( CCmax_new > SKP_RSHIFT( SKP_int32_MAX, 3 ) ) {
CCmax_new = SKP_int32_MAX;
} else {
CCmax_new = SKP_LSHIFT( CCmax_new, 3 );
}
/* Reduce depending on flatness of contour */
diff = SKP_int16_MAX - SKP_RSHIFT( SKP_MUL( contour_bias_Q20, j ), 5 ); /* Q20 -> Q15 */
SKP_assert( diff == SKP_SAT16( diff ) );
CCmax_new = SKP_LSHIFT( SKP_SMULWB( CCmax_new, diff ), 1 );
} else {
CCmax_new = 0;
}
if( CCmax_new > CCmax &&
( d + silk_CB_lags_stage3[ 0 ][ j ] ) <= max_lag
) {
CCmax = CCmax_new;
lag_new = d;
CBimax = j;
}
}
lag_counter++;
}
for( k = 0; k < nb_subfr; k++ ) {
pitch_out[ k ] = lag_new + matrix_ptr( Lag_CB_ptr, k, CBimax, cbk_size );
}
*lagIndex = (opus_int16)( lag_new - min_lag);
*contourIndex = (opus_int8)CBimax;
} else {
/* Save Lags and correlation */
CCmax = SKP_max( CCmax, 0 );
*LTPCorr_Q15 = (opus_int)silk_SQRT_APPROX( SKP_LSHIFT( CCmax, 13 ) ); /* Output normalized correlation */
for( k = 0; k < nb_subfr; k++ ) {
pitch_out[ k ] = lag + matrix_ptr( Lag_CB_ptr, k, CBimax, cbk_size );
}
*lagIndex = (opus_int16)( lag - min_lag_8kHz );
*contourIndex = (opus_int8)CBimax;
}
SKP_assert( *lagIndex >= 0 );
/* return as voiced */
return 0;
}
/*************************************************************************/
/* Calculates the correlations used in stage 3 search. In order to cover */
/* the whole lag codebook for all the searched offset lags (lag +- 2), */
/*************************************************************************/
void silk_P_Ana_calc_corr_st3(
opus_int32 cross_corr_st3[ PE_MAX_NB_SUBFR ][ PE_NB_CBKS_STAGE3_MAX ][ PE_NB_STAGE3_LAGS ],/* (O) 3 DIM correlation array */
const opus_int16 frame[], /* I vector to correlate */
opus_int start_lag, /* I lag offset to search around */
opus_int sf_length, /* I length of a 5 ms subframe */
opus_int nb_subfr, /* I number of subframes */
opus_int complexity /* I Complexity setting */
)
{
const opus_int16 *target_ptr, *basis_ptr;
opus_int32 cross_corr;
opus_int i, j, k, lag_counter, lag_low, lag_high;
opus_int nb_cbk_search, delta, idx, cbk_size;
opus_int32 scratch_mem[ SCRATCH_SIZE ];
const opus_int8 *Lag_range_ptr, *Lag_CB_ptr;
SKP_assert( complexity >= SILK_PE_MIN_COMPLEX );
SKP_assert( complexity <= SILK_PE_MAX_COMPLEX );
if( nb_subfr == PE_MAX_NB_SUBFR ){
Lag_range_ptr = &silk_Lag_range_stage3[ complexity ][ 0 ][ 0 ];
Lag_CB_ptr = &silk_CB_lags_stage3[ 0 ][ 0 ];
nb_cbk_search = silk_nb_cbk_searchs_stage3[ complexity ];
cbk_size = PE_NB_CBKS_STAGE3_MAX;
} else {
SKP_assert( nb_subfr == PE_MAX_NB_SUBFR >> 1);
Lag_range_ptr = &silk_Lag_range_stage3_10_ms[ 0 ][ 0 ];
Lag_CB_ptr = &silk_CB_lags_stage3_10_ms[ 0 ][ 0 ];
nb_cbk_search = PE_NB_CBKS_STAGE3_10MS;
cbk_size = PE_NB_CBKS_STAGE3_10MS;
}
target_ptr = &frame[ SKP_LSHIFT( sf_length, 2 ) ]; /* Pointer to middle of frame */
for( k = 0; k < nb_subfr; k++ ) {
lag_counter = 0;
/* Calculate the correlations for each subframe */
lag_low = matrix_ptr( Lag_range_ptr, k, 0, 2 );
lag_high = matrix_ptr( Lag_range_ptr, k, 1, 2 );
for( j = lag_low; j <= lag_high; j++ ) {
basis_ptr = target_ptr - ( start_lag + j );
cross_corr = silk_inner_prod_aligned( (opus_int16*)target_ptr, (opus_int16*)basis_ptr, sf_length );
SKP_assert( lag_counter < SCRATCH_SIZE );
scratch_mem[ lag_counter ] = cross_corr;
lag_counter++;
}
delta = matrix_ptr( Lag_range_ptr, k, 0, 2 );
for( i = 0; i < nb_cbk_search; i++ ) {
/* Fill out the 3 dim array that stores the correlations for */
/* each code_book vector for each start lag */
idx = matrix_ptr( Lag_CB_ptr, k, i, cbk_size ) - delta;
for( j = 0; j < PE_NB_STAGE3_LAGS; j++ ) {
SKP_assert( idx + j < SCRATCH_SIZE );
SKP_assert( idx + j < lag_counter );
cross_corr_st3[ k ][ i ][ j ] = scratch_mem[ idx + j ];
}
}
target_ptr += sf_length;
}
}
/********************************************************************/
/* Calculate the energies for first two subframes. The energies are */
/* calculated recursively. */
/********************************************************************/
void silk_P_Ana_calc_energy_st3(
opus_int32 energies_st3[ PE_MAX_NB_SUBFR ][ PE_NB_CBKS_STAGE3_MAX ][ PE_NB_STAGE3_LAGS ],/* (O) 3 DIM energy array */
const opus_int16 frame[], /* I vector to calc energy in */
opus_int start_lag, /* I lag offset to search around */
opus_int sf_length, /* I length of one 5 ms subframe */
opus_int nb_subfr, /* I number of subframes */
opus_int complexity /* I Complexity setting */
)
{
const opus_int16 *target_ptr, *basis_ptr;
opus_int32 energy;
opus_int k, i, j, lag_counter;
opus_int nb_cbk_search, delta, idx, cbk_size, lag_diff;
opus_int32 scratch_mem[ SCRATCH_SIZE ];
const opus_int8 *Lag_range_ptr, *Lag_CB_ptr;
SKP_assert( complexity >= SILK_PE_MIN_COMPLEX );
SKP_assert( complexity <= SILK_PE_MAX_COMPLEX );
if( nb_subfr == PE_MAX_NB_SUBFR ){
Lag_range_ptr = &silk_Lag_range_stage3[ complexity ][ 0 ][ 0 ];
Lag_CB_ptr = &silk_CB_lags_stage3[ 0 ][ 0 ];
nb_cbk_search = silk_nb_cbk_searchs_stage3[ complexity ];
cbk_size = PE_NB_CBKS_STAGE3_MAX;
} else {
SKP_assert( nb_subfr == PE_MAX_NB_SUBFR >> 1);
Lag_range_ptr = &silk_Lag_range_stage3_10_ms[ 0 ][ 0 ];
Lag_CB_ptr = &silk_CB_lags_stage3_10_ms[ 0 ][ 0 ];
nb_cbk_search = PE_NB_CBKS_STAGE3_10MS;
cbk_size = PE_NB_CBKS_STAGE3_10MS;
}
target_ptr = &frame[ SKP_LSHIFT( sf_length, 2 ) ];
for( k = 0; k < nb_subfr; k++ ) {
lag_counter = 0;
/* Calculate the energy for first lag */
basis_ptr = target_ptr - ( start_lag + matrix_ptr( Lag_range_ptr, k, 0, 2 ) );
energy = silk_inner_prod_aligned( basis_ptr, basis_ptr, sf_length );
SKP_assert( energy >= 0 );
scratch_mem[ lag_counter ] = energy;
lag_counter++;
lag_diff = ( matrix_ptr( Lag_range_ptr, k, 1, 2 ) - matrix_ptr( Lag_range_ptr, k, 0, 2 ) + 1 );
for( i = 1; i < lag_diff; i++ ) {
/* remove part outside new window */
energy -= SKP_SMULBB( basis_ptr[ sf_length - i ], basis_ptr[ sf_length - i ] );
SKP_assert( energy >= 0 );
/* add part that comes into window */
energy = SKP_ADD_SAT32( energy, SKP_SMULBB( basis_ptr[ -i ], basis_ptr[ -i ] ) );
SKP_assert( energy >= 0 );
SKP_assert( lag_counter < SCRATCH_SIZE );
scratch_mem[ lag_counter ] = energy;
lag_counter++;
}
delta = matrix_ptr( Lag_range_ptr, k, 0, 2 );
for( i = 0; i < nb_cbk_search; i++ ) {
/* Fill out the 3 dim array that stores the correlations for */
/* each code_book vector for each start lag */
idx = matrix_ptr( Lag_CB_ptr, k, i, cbk_size ) - delta;
for( j = 0; j < PE_NB_STAGE3_LAGS; j++ ) {
SKP_assert( idx + j < SCRATCH_SIZE );
SKP_assert( idx + j < lag_counter );
energies_st3[ k ][ i ][ j ] = scratch_mem[ idx + j ];
SKP_assert( energies_st3[ k ][ i ][ j ] >= 0 );
}
}
target_ptr += sf_length;
}
}
opus_int32 silk_P_Ana_find_scaling(
const opus_int16 *frame,
const opus_int frame_length,
const opus_int sum_sqr_len
)
{
opus_int32 nbits, x_max;
x_max = silk_int16_array_maxabs( frame, frame_length );
if( x_max < SKP_int16_MAX ) {
/* Number of bits needed for the sum of the squares */
nbits = 32 - silk_CLZ32( SKP_SMULBB( x_max, x_max ) );
} else {
/* Here we don't know if x_max should have been SKP_int16_MAX + 1, so we expect the worst case */
nbits = 30;
}
nbits += 17 - silk_CLZ16( sum_sqr_len );
/* Without a guarantee of saturation, we need to keep the 31st bit free */
if( nbits < 31 ) {
return 0;
} else {
return( nbits - 30 );
}
}
|