-
Notifications
You must be signed in to change notification settings - Fork 3
/
pitch_a.c
549 lines (442 loc) · 17.2 KB
/
pitch_a.c
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
/* G.729A Version 1.1 Last modified: September 1996 */
/*
ITU-T G.729A Speech Coder ANSI-C Source Code
Copyright (c) 1996,
AT&T, France Telecom, NTT, Universite de Sherbrooke, Lucent Technologies
All rights reserved.
*/
/*---------------------------------------------------------------------------*
* Pitch related functions *
* ~~~~~~~~~~~~~~~~~~~~~~~ *
*---------------------------------------------------------------------------*/
#include "typedef.h"
#include "basic_op.h"
#include "oper_32b.h"
#include "ld8a.h"
#include "tab_ld8a.h"
/*---------------------------------------------------------------------------*
* Function Pitch_ol_fast *
* ~~~~~~~~~~~~~~~~~~~~~~~ *
* Compute the open loop pitch lag. (fast version) *
* *
*---------------------------------------------------------------------------*/
int16_t Pitch_ol_fast( /* output: open loop pitch lag */
int16_t signal[], /* input : signal used to compute the open loop pitch */
/* signal[-pit_max] to signal[-1] should be known */
int16_t pit_max, /* input : maximum pitch lag */
int16_t L_frame /* input : length of frame to compute pitch */
)
{
int16_t i, j;
int16_t max1, max2, max3;
int16_t max_h, max_l, ener_h, ener_l;
int16_t T1, T2, T3;
int16_t *p, *p1;
int32_t max, sum, L_temp;
/* Scaled signal */
int16_t scaled_signal[L_FRAME+PIT_MAX];
int16_t *scal_sig;
scal_sig = &scaled_signal[pit_max];
/*--------------------------------------------------------*
* Verification for risk of overflow. *
*--------------------------------------------------------*/
Overflow = 0;
sum = 0;
for(i= -pit_max; i< L_frame; i+=2)
sum = L_mac(sum, signal[i], signal[i]);
/*--------------------------------------------------------*
* Scaling of input signal. *
* *
* if Overflow -> scal_sig[i] = signal[i]>>3 *
* else if sum < 1^20 -> scal_sig[i] = signal[i]<<3 *
* else -> scal_sig[i] = signal[i] *
*--------------------------------------------------------*/
if(Overflow == 1)
{
for(i=-pit_max; i<L_frame; i++)
{
scal_sig[i] = shr(signal[i], 3);
}
}
else {
L_temp = L_sub(sum, (int32_t)1048576L);
if ( L_temp < (int32_t)0 ) /* if (sum < 2^20) */
{
for(i=-pit_max; i<L_frame; i++)
{
scal_sig[i] = shl(signal[i], 3);
}
}
else
{
for(i=-pit_max; i<L_frame; i++)
{
scal_sig[i] = signal[i];
}
}
}
/*--------------------------------------------------------------------*
* The pitch lag search is divided in three sections. *
* Each section cannot have a pitch multiple. *
* We find a maximum for each section. *
* We compare the maxima of each section by favoring small lag. *
* *
* First section: lag delay = 20 to 39 *
* Second section: lag delay = 40 to 79 *
* Third section: lag delay = 80 to 143 *
*--------------------------------------------------------------------*/
/* First section */
max = MIN_32;
T1 = 20; /* Only to remove warning from some compilers */
for (i = 20; i < 40; i++) {
p = scal_sig;
p1 = &scal_sig[-i];
sum = 0;
for (j=0; j<L_frame; j+=2, p+=2, p1+=2)
sum = L_mac(sum, *p, *p1);
L_temp = L_sub(sum, max);
if (L_temp > 0) { max = sum; T1 = i; }
}
/* compute energy of maximum */
sum = 1; /* to avoid division by zero */
p = &scal_sig[-T1];
for(i=0; i<L_frame; i+=2, p+=2)
sum = L_mac(sum, *p, *p);
/* max1 = max/sqrt(energy) */
/* This result will always be on 16 bits !! */
sum = Inv_sqrt(sum); /* 1/sqrt(energy), result in Q30 */
L_Extract(max, &max_h, &max_l);
L_Extract(sum, &ener_h, &ener_l);
sum = Mpy_32(max_h, max_l, ener_h, ener_l);
max1 = extract_l(sum);
/* Second section */
max = MIN_32;
T2 = 40; /* Only to remove warning from some compilers */
for (i = 40; i < 80; i++) {
p = scal_sig;
p1 = &scal_sig[-i];
sum = 0;
for (j=0; j<L_frame; j+=2, p+=2, p1+=2)
sum = L_mac(sum, *p, *p1);
L_temp = L_sub(sum, max);
if (L_temp > 0) { max = sum; T2 = i; }
}
/* compute energy of maximum */
sum = 1; /* to avoid division by zero */
p = &scal_sig[-T2];
for(i=0; i<L_frame; i+=2, p+=2)
sum = L_mac(sum, *p, *p);
/* max2 = max/sqrt(energy) */
/* This result will always be on 16 bits !! */
sum = Inv_sqrt(sum); /* 1/sqrt(energy), result in Q30 */
L_Extract(max, &max_h, &max_l);
L_Extract(sum, &ener_h, &ener_l);
sum = Mpy_32(max_h, max_l, ener_h, ener_l);
max2 = extract_l(sum);
/* Third section */
max = MIN_32;
T3 = 80; /* Only to remove warning from some compilers */
for (i = 80; i < 143; i+=2) {
p = scal_sig;
p1 = &scal_sig[-i];
sum = 0;
for (j=0; j<L_frame; j+=2, p+=2, p1+=2)
sum = L_mac(sum, *p, *p1);
L_temp = L_sub(sum, max);
if (L_temp > 0) { max = sum; T3 = i; }
}
/* Test around max3 */
i = T3;
p = scal_sig;
p1 = &scal_sig[-(i+1)];
sum = 0;
for (j=0; j<L_frame; j+=2, p+=2, p1+=2)
sum = L_mac(sum, *p, *p1);
L_temp = L_sub(sum, max);
if (L_temp > 0) { max = sum; T3 = i+(int16_t)1; }
p = scal_sig;
p1 = &scal_sig[-(i-1)];
sum = 0;
for (j=0; j<L_frame; j+=2, p+=2, p1+=2)
sum = L_mac(sum, *p, *p1);
L_temp = L_sub(sum, max);
if (L_temp > 0) { max = sum; T3 = i-(int16_t)1; }
/* compute energy of maximum */
sum = 1; /* to avoid division by zero */
p = &scal_sig[-T3];
for(i=0; i<L_frame; i+=2, p+=2)
sum = L_mac(sum, *p, *p);
/* max1 = max/sqrt(energy) */
/* This result will always be on 16 bits !! */
sum = Inv_sqrt(sum); /* 1/sqrt(energy), result in Q30 */
L_Extract(max, &max_h, &max_l);
L_Extract(sum, &ener_h, &ener_l);
sum = Mpy_32(max_h, max_l, ener_h, ener_l);
max3 = extract_l(sum);
/*-----------------------*
* Test for multiple. *
*-----------------------*/
/* if( abs(T2*2 - T3) < 5) */
/* max2 += max3 * 0.25; */
i = sub(shl(T2,1), T3);
j = sub(abs_s(i), 5);
if(j < 0)
max2 = add(max2, shr(max3, 2));
/* if( abs(T2*3 - T3) < 7) */
/* max2 += max3 * 0.25; */
i = add(i, T2);
j = sub(abs_s(i), 7);
if(j < 0)
max2 = add(max2, shr(max3, 2));
/* if( abs(T1*2 - T2) < 5) */
/* max1 += max2 * 0.20; */
i = sub(shl(T1,1), T2);
j = sub(abs_s(i), 5);
if(j < 0)
max1 = add(max1, mult(max2, 6554));
/* if( abs(T1*3 - T2) < 7) */
/* max1 += max2 * 0.20; */
i = add(i, T1);
j = sub(abs_s(i), 7);
if(j < 0)
max1 = add(max1, mult(max2, 6554));
/*--------------------------------------------------------------------*
* Compare the 3 sections maxima. *
*--------------------------------------------------------------------*/
if( sub(max1, max2) < 0 ) {max1 = max2; T1 = T2; }
if( sub(max1, max3) <0 ) {T1 = T3; }
return T1;
}
/*--------------------------------------------------------------------------*
* Function Dot_Product() *
* ~~~~~~~~~~~~~~~~~~~~~~ *
*--------------------------------------------------------------------------*/
int32_t Dot_Product( /* (o) :Result of scalar product. */
int16_t x[], /* (i) :First vector. */
int16_t y[], /* (i) :Second vector. */
int16_t lg /* (i) :Number of point. */
)
{
int16_t i;
int32_t sum;
sum = 0;
for(i=0; i<lg; i++)
sum = L_mac(sum, x[i], y[i]);
return sum;
}
/*--------------------------------------------------------------------------*
* Function Pitch_fr3_fast() *
* ~~~~~~~~~~~~~~~~~~~~~~~~~~ *
* Fast version of the pitch close loop. *
*--------------------------------------------------------------------------*/
int16_t Pitch_fr3_fast(/* (o) : pitch period. */
int16_t exc[], /* (i) : excitation buffer */
int16_t xn[], /* (i) : target vector */
int16_t h[], /* (i) Q12 : impulse response of filters. */
int16_t L_subfr, /* (i) : Length of subframe */
int16_t t0_min, /* (i) : minimum value in the searched range. */
int16_t t0_max, /* (i) : maximum value in the searched range. */
int16_t i_subfr, /* (i) : indicator for first subframe. */
int16_t *pit_frac /* (o) : chosen fraction. */
)
{
int16_t t, t0;
int16_t Dn[L_SUBFR];
int16_t exc_tmp[L_SUBFR];
int32_t max, corr, L_temp;
/*-----------------------------------------------------------------*
* Compute correlation of target vector with impulse response. *
*-----------------------------------------------------------------*/
Cor_h_X(h, xn, Dn);
/*-----------------------------------------------------------------*
* Find maximum integer delay. *
*-----------------------------------------------------------------*/
max = MIN_32;
t0 = t0_min; /* Only to remove warning from some compilers */
for(t=t0_min; t<=t0_max; t++)
{
corr = Dot_Product(Dn, &exc[-t], L_subfr);
L_temp = L_sub(corr, max);
if(L_temp > 0) {max = corr; t0 = t; }
}
/*-----------------------------------------------------------------*
* Test fractions. *
*-----------------------------------------------------------------*/
/* Fraction 0 */
Pred_lt_3(exc, t0, 0, L_subfr);
max = Dot_Product(Dn, exc, L_subfr);
*pit_frac = 0;
/* If first subframe and lag > 84 do not search fractional pitch */
if( (i_subfr == 0) && (sub(t0, 84) > 0) )
return t0;
Copy(exc, exc_tmp, L_subfr);
/* Fraction -1/3 */
Pred_lt_3(exc, t0, -1, L_subfr);
corr = Dot_Product(Dn, exc, L_subfr);
L_temp = L_sub(corr, max);
if(L_temp > 0) {
max = corr;
*pit_frac = -1;
Copy(exc, exc_tmp, L_subfr);
}
/* Fraction +1/3 */
Pred_lt_3(exc, t0, 1, L_subfr);
corr = Dot_Product(Dn, exc, L_subfr);
L_temp = L_sub(corr, max);
if(L_temp > 0) {
max = corr;
*pit_frac = 1;
}
else
Copy(exc_tmp, exc, L_subfr);
return t0;
}
/*---------------------------------------------------------------------*
* Function G_pitch: *
* ~~~~~~~~ *
*---------------------------------------------------------------------*
* Compute correlations <xn,y1> and <y1,y1> to use in gains quantizer. *
* Also compute the gain of pitch. Result in Q14 *
* if (gain < 0) gain =0 *
* if (gain >1.2) gain =1.2 *
*---------------------------------------------------------------------*/
int16_t G_pitch( /* (o) Q14 : Gain of pitch lag saturated to 1.2 */
int16_t xn[], /* (i) : Pitch target. */
int16_t y1[], /* (i) : Filtered adaptive codebook. */
int16_t g_coeff[], /* (i) : Correlations need for gain quantization. */
int16_t L_subfr /* (i) : Length of subframe. */
)
{
int16_t i;
int16_t xy, yy, exp_xy, exp_yy, gain;
int32_t s;
int16_t scaled_y1[L_SUBFR];
/* divide "y1[]" by 4 to avoid overflow */
for(i=0; i<L_subfr; i++)
scaled_y1[i] = shr(y1[i], 2);
/* Compute scalar product <y1[],y1[]> */
Overflow = 0;
s = 1; /* Avoid case of all zeros */
for(i=0; i<L_subfr; i++)
s = L_mac(s, y1[i], y1[i]);
if (Overflow == 0) {
exp_yy = norm_l(s);
yy = _round( L_shl(s, exp_yy) );
}
else {
s = 1; /* Avoid case of all zeros */
for(i=0; i<L_subfr; i++)
s = L_mac(s, scaled_y1[i], scaled_y1[i]);
exp_yy = norm_l(s);
yy = _round( L_shl(s, exp_yy) );
exp_yy = sub(exp_yy, 4);
}
/* Compute scalar product <xn[],y1[]> */
Overflow = 0;
s = 0;
for(i=0; i<L_subfr; i++)
s = L_mac(s, xn[i], y1[i]);
if (Overflow == 0) {
exp_xy = norm_l(s);
xy = _round( L_shl(s, exp_xy) );
}
else {
s = 0;
for(i=0; i<L_subfr; i++)
s = L_mac(s, xn[i], scaled_y1[i]);
exp_xy = norm_l(s);
xy = _round( L_shl(s, exp_xy) );
exp_xy = sub(exp_xy, 2);
}
g_coeff[0] = yy;
g_coeff[1] = sub(15, exp_yy);
g_coeff[2] = xy;
g_coeff[3] = sub(15, exp_xy);
/* If (xy <= 0) gain = 0 */
if (xy <= 0)
{
g_coeff[3] = -15; /* Force exp_xy to -15 = (15-30) */
return( (int16_t) 0);
}
/* compute gain = xy/yy */
xy = shr(xy, 1); /* Be sure xy < yy */
gain = div_s( xy, yy);
i = sub(exp_xy, exp_yy);
gain = shr(gain, i); /* saturation if > 1.99 in Q14 */
/* if(gain >1.2) gain = 1.2 in Q14 */
if( sub(gain, 19661) > 0)
{
gain = 19661;
}
return(gain);
}
/*----------------------------------------------------------------------*
* Function Enc_lag3 *
* ~~~~~~~~ *
* Encoding of fractional pitch lag with 1/3 resolution. *
*----------------------------------------------------------------------*
* The pitch range for the first subframe is divided as follows: *
* 19 1/3 to 84 2/3 resolution 1/3 *
* 85 to 143 resolution 1 *
* *
* The period in the first subframe is encoded with 8 bits. *
* For the range with fractions: *
* index = (T-19)*3 + frac - 1; where T=[19..85] and frac=[-1,0,1] *
* and for the integer only range *
* index = (T - 85) + 197; where T=[86..143] *
*----------------------------------------------------------------------*
* For the second subframe a resolution of 1/3 is always used, and the *
* search range is relative to the lag in the first subframe. *
* If t0 is the lag in the first subframe then *
* t_min=t0-5 and t_max=t0+4 and the range is given by *
* t_min - 2/3 to t_max + 2/3 *
* *
* The period in the 2nd subframe is encoded with 5 bits: *
* index = (T-(t_min-1))*3 + frac - 1; where T[t_min-1 .. t_max+1] *
*----------------------------------------------------------------------*/
int16_t Enc_lag3( /* output: Return index of encoding */
int16_t T0, /* input : Pitch delay */
int16_t T0_frac, /* input : Fractional pitch delay */
int16_t *T0_min, /* in/out: Minimum search delay */
int16_t *T0_max, /* in/out: Maximum search delay */
int16_t pit_min, /* input : Minimum pitch delay */
int16_t pit_max, /* input : Maximum pitch delay */
int16_t pit_flag /* input : int32_t for 1st subframe */
)
{
int16_t index, i;
if (pit_flag == 0) /* if 1st subframe */
{
/* encode pitch delay (with fraction) */
if (sub(T0, 85) <= 0)
{
/* index = t0*3 - 58 + t0_frac */
i = add(add(T0, T0), T0);
index = add(sub(i, 58), T0_frac);
}
else {
index = add(T0, 112);
}
/* find T0_min and T0_max for second subframe */
*T0_min = sub(T0, 5);
if (sub(*T0_min, pit_min) < 0)
{
*T0_min = pit_min;
}
*T0_max = add(*T0_min, 9);
if (sub(*T0_max, pit_max) > 0)
{
*T0_max = pit_max;
*T0_min = sub(*T0_max, 9);
}
}
else /* if second subframe */
{
/* i = t0 - t0_min; */
/* index = i*3 + 2 + t0_frac; */
i = sub(T0, *T0_min);
i = add(add(i, i), i);
index = add(add(i, 2), T0_frac);
}
return index;
}