-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcod_ld8a.c
541 lines (422 loc) · 20.8 KB
/
cod_ld8a.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
/*
ITU-T G.729A Speech Coder with Annex B ANSI-C Source Code
Version 1.3 Last modified: August 1997
Copyright (c) 1996,
AT&T, France Telecom, NTT, Universite de Sherbrooke, Lucent Technologies,
Rockwell International
All rights reserved.
*/
/*-----------------------------------------------------------------*
* Functions Coder_ld8a and Init_Coder_ld8a *
* ~~~~~~~~~~ ~~~~~~~~~~~~~~~ *
* *
* Init_Coder_ld8a(void); *
* *
* ->Initialization of variables for the coder section. *
* *
* *
* Coder_ld8a(int16_t ana[]); *
* *
* ->Main coder function. *
* *
* *
* Input: *
* *
* 80 speech data should have beee copy to vector new_speech[]. *
* This vector is global and is declared in this function. *
* *
* Ouputs: *
* *
* ana[] ->analysis parameters. *
* *
*-----------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include "typedef.h"
#include "basic_op.h"
#include "ld8a.h"
#include "vad.h"
#include "dtx.h"
#include "sid.h"
/*-----------------------------------------------------------*
* Coder constant parameters (defined in "ld8a.h") *
*-----------------------------------------------------------*
* L_WINDOW : LPC analysis window size. *
* L_NEXT : Samples of next frame needed for autocor. *
* L_FRAME : Frame size. *
* L_SUBFR : Sub-frame size. *
* M : LPC order. *
* MP1 : LPC order+1 *
* L_TOTAL : Total size of speech buffer. *
* PIT_MIN : Minimum pitch lag. *
* PIT_MAX : Maximum pitch lag. *
* L_INTERPOL : Length of filter for interpolation *
*-----------------------------------------------------------*/
/*--------------------------------------------------------*
* Static memory allocation. *
*--------------------------------------------------------*/
/* Speech vector */
static int16_t old_speech[L_TOTAL];
static int16_t *speech, *p_window;
int16_t *new_speech; /* Global variable */
/* Weighted speech vector */
static int16_t old_wsp[L_FRAME+PIT_MAX];
static int16_t *wsp;
/* Excitation vector */
static int16_t old_exc[L_FRAME+PIT_MAX+L_INTERPOL];
static int16_t *exc;
/* Lsp (Line spectral pairs) */
static int16_t lsp_old[M]={
30000, 26000, 21000, 15000, 8000, 0, -8000,-15000,-21000,-26000};
static int16_t lsp_old_q[M];
/* Filter's memory */
static int16_t mem_w0[M], mem_w[M], mem_zero[M];
static int16_t sharp;
/* For G.729B */
/* DTX variables */
static int16_t pastVad;
static int16_t ppastVad;
static int16_t seed;
/*-----------------------------------------------------------------*
* Function Init_Coder_ld8a *
* ~~~~~~~~~~~~~~~ *
* *
* Init_Coder_ld8a(void); *
* *
* ->Initialization of variables for the coder section. *
* - initialize pointers to speech buffer *
* - initialize static pointers *
* - set static vectors to zero *
* *
*-----------------------------------------------------------------*/
void Init_Coder_ld8a(void)
{
/*----------------------------------------------------------------------*
* Initialize pointers to speech vector. *
* *
* *
* |--------------------|-------------|-------------|------------| *
* previous speech sf1 sf2 L_NEXT *
* *
* <---------------- Total speech vector (L_TOTAL) -----------> *
* <---------------- LPC analysis window (L_WINDOW) -----------> *
* | <-- present frame (L_FRAME) --> *
* old_speech | <-- new speech (L_FRAME) --> *
* p_window | | *
* speech | *
* new_speech *
*-----------------------------------------------------------------------*/
new_speech = old_speech + L_TOTAL - L_FRAME; /* New speech */
speech = new_speech - L_NEXT; /* Present frame */
p_window = old_speech + L_TOTAL - L_WINDOW; /* For LPC window */
/* Initialize static pointers */
wsp = old_wsp + PIT_MAX;
exc = old_exc + PIT_MAX + L_INTERPOL;
/* Static vectors to zero */
Set_zero(old_speech, L_TOTAL);
Set_zero(old_exc, PIT_MAX+L_INTERPOL);
Set_zero(old_wsp, PIT_MAX);
Set_zero(mem_w, M);
Set_zero(mem_w0, M);
Set_zero(mem_zero, M);
sharp = SHARPMIN;
/* Initialize lsp_old_q[] */
Copy(lsp_old, lsp_old_q, M);
Lsp_encw_reset();
Init_exc_err();
/* For G.729B */
/* Initialize VAD/DTX parameters */
pastVad = 1;
ppastVad = 1;
seed = INIT_SEED;
vad_init();
Init_lsfq_noise();
return;
}
/*-----------------------------------------------------------------*
* Functions Coder_ld8a *
* ~~~~~~~~~~ *
* Coder_ld8a(int16_t ana[]); *
* *
* ->Main coder function. *
* *
* *
* Input: *
* *
* 80 speech data should have beee copy to vector new_speech[]. *
* This vector is global and is declared in this function. *
* *
* Ouputs: *
* *
* ana[] ->analysis parameters. *
* *
*-----------------------------------------------------------------*/
void Coder_ld8a(
int16_t ana[], /* output : Analysis parameters */
int16_t frame, /* input : frame counter */
int16_t vad_enable /* input : VAD enable flag */
)
{
/* LPC analysis */
int16_t Aq_t[(MP1)*2]; /* A(z) quantized for the 2 subframes */
int16_t Ap_t[(MP1)*2]; /* A(z/gamma) for the 2 subframes */
int16_t *Aq, *Ap; /* Pointer on Aq_t and Ap_t */
/* Other vectors */
int16_t h1[L_SUBFR]; /* Impulse response h1[] */
int16_t xn[L_SUBFR]; /* Target vector for pitch search */
int16_t xn2[L_SUBFR]; /* Target vector for codebook search */
int16_t code[L_SUBFR]; /* Fixed codebook excitation */
int16_t y1[L_SUBFR]; /* Filtered adaptive excitation */
int16_t y2[L_SUBFR]; /* Filtered fixed codebook excitation */
int16_t g_coeff[4]; /* Correlations between xn & y1 */
int16_t g_coeff_cs[5];
int16_t exp_g_coeff_cs[5]; /* Correlations between xn, y1, & y2
<y1,y1>, -2<xn,y1>,
<y2,y2>, -2<xn,y2>, 2<y1,y2> */
/* Scalars */
int16_t i, j, k, i_subfr;
int16_t T_op, T0, T0_min, T0_max, T0_frac;
int16_t gain_pit, gain_code, index;
int16_t temp, taming;
int32_t L_temp;
/*------------------------------------------------------------------------*
* - Perform LPC analysis: *
* * autocorrelation + lag windowing *
* * Levinson-durbin algorithm to find a[] *
* * convert a[] to lsp[] *
* * quantize and code the LSPs *
* * find the interpolated LSPs and convert to a[] for the 2 *
* subframes (both quantized and unquantized) *
*------------------------------------------------------------------------*/
{
/* Temporary vectors */
int16_t r_l[NP+1], r_h[NP+1]; /* Autocorrelations low and hi */
int16_t rc[M]; /* Reflection coefficients. */
int16_t lsp_new[M], lsp_new_q[M]; /* LSPs at 2th subframe */
/* For G.729B */
int16_t rh_nbe[MP1];
int16_t lsf_new[M];
int16_t lsfq_mem[MA_NP][M];
int16_t exp_R0, Vad;
/* LP analysis */
Autocorr(p_window, NP, r_h, r_l, &exp_R0); /* Autocorrelations */
Copy(r_h, rh_nbe, MP1);
Lag_window(NP, r_h, r_l); /* Lag windowing */
Levinson(r_h, r_l, Ap_t, rc, &temp); /* Levinson Durbin */
Az_lsp(Ap_t, lsp_new, lsp_old); /* From A(z) to lsp */
/* For G.729B */
/* ------ VAD ------- */
Lsp_lsf(lsp_new, lsf_new, M);
vad(rc[1], lsf_new, r_h, r_l, exp_R0, p_window, frame,
pastVad, ppastVad, &Vad);
Update_cng(rh_nbe, exp_R0, Vad);
/* ---------------------- */
/* Case of Inactive frame */
/* ---------------------- */
if ((Vad == 0) && (vad_enable == 1)){
Get_freq_prev(lsfq_mem);
Cod_cng(exc, pastVad, lsp_old_q, Aq_t, ana, lsfq_mem, &seed);
Update_freq_prev(lsfq_mem);
ppastVad = pastVad;
pastVad = Vad;
/* Update wsp, mem_w and mem_w0 */
Aq = Aq_t;
for(i_subfr=0; i_subfr < L_FRAME; i_subfr += L_SUBFR) {
/* Residual signal in xn */
Residu(Aq, &speech[i_subfr], xn, L_SUBFR);
Weight_Az(Aq, GAMMA1, M, Ap_t);
/* Compute wsp and mem_w */
Ap = Ap_t + MP1;
Ap[0] = 4096;
for(i=1; i<=M; i++) /* Ap[i] = Ap_t[i] - 0.7 * Ap_t[i-1]; */
Ap[i] = sub(Ap_t[i], mult(Ap_t[i-1], 22938));
Syn_filt(Ap, xn, &wsp[i_subfr], L_SUBFR, mem_w, 1);
/* Compute mem_w0 */
for(i=0; i<L_SUBFR; i++) {
xn[i] = sub(xn[i], exc[i_subfr+i]); /* residu[] - exc[] */
}
Syn_filt(Ap_t, xn, xn, L_SUBFR, mem_w0, 1);
Aq += MP1;
}
sharp = SHARPMIN;
/* Update memories for next frames */
Copy(&old_speech[L_FRAME], &old_speech[0], L_TOTAL-L_FRAME);
Copy(&old_wsp[L_FRAME], &old_wsp[0], PIT_MAX);
Copy(&old_exc[L_FRAME], &old_exc[0], PIT_MAX+L_INTERPOL);
return;
} /* End of inactive frame case */
/* -------------------- */
/* Case of Active frame */
/* -------------------- */
/* Case of active frame */
*ana++ = 1;
seed = INIT_SEED;
ppastVad = pastVad;
pastVad = Vad;
/* LSP quantization */
Qua_lsp(lsp_new, lsp_new_q, ana);
ana += 2; /* Advance analysis parameters pointer */
/*--------------------------------------------------------------------*
* Find interpolated LPC parameters in all subframes *
* The interpolated parameters are in array Aq_t[]. *
*--------------------------------------------------------------------*/
Int_qlpc(lsp_old_q, lsp_new_q, Aq_t);
/* Compute A(z/gamma) */
Weight_Az(&Aq_t[0], GAMMA1, M, &Ap_t[0]);
Weight_Az(&Aq_t[MP1], GAMMA1, M, &Ap_t[MP1]);
/* update the LSPs for the next frame */
Copy(lsp_new, lsp_old, M);
Copy(lsp_new_q, lsp_old_q, M);
}
/*----------------------------------------------------------------------*
* - Find the weighted input speech w_sp[] for the whole speech frame *
* - Find the open-loop pitch delay *
*----------------------------------------------------------------------*/
Residu(&Aq_t[0], &speech[0], &exc[0], L_SUBFR);
Residu(&Aq_t[MP1], &speech[L_SUBFR], &exc[L_SUBFR], L_SUBFR);
{
int16_t Ap1[MP1];
Ap = Ap_t;
Ap1[0] = 4096;
for(i=1; i<=M; i++) /* Ap1[i] = Ap[i] - 0.7 * Ap[i-1]; */
Ap1[i] = sub(Ap[i], mult(Ap[i-1], 22938));
Syn_filt(Ap1, &exc[0], &wsp[0], L_SUBFR, mem_w, 1);
Ap += MP1;
for(i=1; i<=M; i++) /* Ap1[i] = Ap[i] - 0.7 * Ap[i-1]; */
Ap1[i] = sub(Ap[i], mult(Ap[i-1], 22938));
Syn_filt(Ap1, &exc[L_SUBFR], &wsp[L_SUBFR], L_SUBFR, mem_w, 1);
}
/* Find open loop pitch lag */
T_op = Pitch_ol_fast(wsp, PIT_MAX, L_FRAME);
/* Range for closed loop pitch search in 1st subframe */
T0_min = sub(T_op, 3);
if (sub(T0_min,PIT_MIN)<0) {
T0_min = PIT_MIN;
}
T0_max = add(T0_min, 6);
if (sub(T0_max ,PIT_MAX)>0)
{
T0_max = PIT_MAX;
T0_min = sub(T0_max, 6);
}
/*------------------------------------------------------------------------*
* Loop for every subframe in the analysis frame *
*------------------------------------------------------------------------*
* To find the pitch and innovation parameters. The subframe size is *
* L_SUBFR and the loop is repeated 2 times. *
* - find the weighted LPC coefficients *
* - find the LPC residual signal res[] *
* - compute the target signal for pitch search *
* - compute impulse response of weighted synthesis filter (h1[]) *
* - find the closed-loop pitch parameters *
* - encode the pitch delay *
* - find target vector for codebook search *
* - codebook search *
* - VQ of pitch and codebook gains *
* - update states of weighting filter *
*------------------------------------------------------------------------*/
Aq = Aq_t; /* pointer to interpolated quantized LPC parameters */
Ap = Ap_t; /* pointer to weighted LPC coefficients */
for (i_subfr = 0; i_subfr < L_FRAME; i_subfr += L_SUBFR)
{
/*---------------------------------------------------------------*
* Compute impulse response, h1[], of weighted synthesis filter *
*---------------------------------------------------------------*/
h1[0] = 4096;
Set_zero(&h1[1], L_SUBFR-1);
Syn_filt(Ap, h1, h1, L_SUBFR, &h1[1], 0);
/*----------------------------------------------------------------------*
* Find the target vector for pitch search: *
*----------------------------------------------------------------------*/
Syn_filt(Ap, &exc[i_subfr], xn, L_SUBFR, mem_w0, 0);
/*---------------------------------------------------------------------*
* Closed-loop fractional pitch search *
*---------------------------------------------------------------------*/
T0 = Pitch_fr3_fast(&exc[i_subfr], xn, h1, L_SUBFR, T0_min, T0_max,
i_subfr, &T0_frac);
index = Enc_lag3(T0, T0_frac, &T0_min, &T0_max,PIT_MIN,PIT_MAX,i_subfr);
*ana++ = index;
if (i_subfr == 0) {
*ana++ = Parity_Pitch(index);
}
/*-----------------------------------------------------------------*
* - find filtered pitch exc *
* - compute pitch gain and limit between 0 and 1.2 *
* - update target vector for codebook search *
*-----------------------------------------------------------------*/
Syn_filt(Ap, &exc[i_subfr], y1, L_SUBFR, mem_zero, 0);
gain_pit = G_pitch(xn, y1, g_coeff, L_SUBFR);
/* clip pitch gain if taming is necessary */
taming = test_err(T0, T0_frac);
if( taming == 1){
if (sub(gain_pit, GPCLIP) > 0) {
gain_pit = GPCLIP;
}
}
/* xn2[i] = xn[i] - y1[i] * gain_pit */
for (i = 0; i < L_SUBFR; i++)
{
L_temp = L_mult(y1[i], gain_pit);
L_temp = L_shl(L_temp, 1); /* gain_pit in Q14 */
xn2[i] = sub(xn[i], extract_h(L_temp));
}
/*-----------------------------------------------------*
* - Innovative codebook search. *
*-----------------------------------------------------*/
index = ACELP_Code_A(xn2, h1, T0, sharp, code, y2, &i);
*ana++ = index; /* Positions index */
*ana++ = i; /* Signs index */
/*-----------------------------------------------------*
* - Quantization of gains. *
*-----------------------------------------------------*/
g_coeff_cs[0] = g_coeff[0]; /* <y1,y1> */
exp_g_coeff_cs[0] = negate(g_coeff[1]); /* Q-Format:XXX -> JPN */
g_coeff_cs[1] = negate(g_coeff[2]); /* (xn,y1) -> -2<xn,y1> */
exp_g_coeff_cs[1] = negate(add(g_coeff[3], 1)); /* Q-Format:XXX -> JPN */
Corr_xy2( xn, y1, y2, g_coeff_cs, exp_g_coeff_cs ); /* Q0 Q0 Q12 ^Qx ^Q0 */
/* g_coeff_cs[3]:exp_g_coeff_cs[3] = <y2,y2> */
/* g_coeff_cs[4]:exp_g_coeff_cs[4] = -2<xn,y2> */
/* g_coeff_cs[5]:exp_g_coeff_cs[5] = 2<y1,y2> */
*ana++ = Qua_gain(code, g_coeff_cs, exp_g_coeff_cs,
L_SUBFR, &gain_pit, &gain_code, taming);
/*------------------------------------------------------------*
* - Update pitch sharpening "sharp" with quantized gain_pit *
*------------------------------------------------------------*/
sharp = gain_pit;
if (sub(sharp, SHARPMAX) > 0) { sharp = SHARPMAX; }
if (sub(sharp, SHARPMIN) < 0) { sharp = SHARPMIN; }
/*------------------------------------------------------*
* - Find the total excitation *
* - update filters memories for finding the target *
* vector in the next subframe *
*------------------------------------------------------*/
for (i = 0; i < L_SUBFR; i++)
{
/* exc[i] = gain_pit*exc[i] + gain_code*code[i]; */
/* exc[i] in Q0 gain_pit in Q14 */
/* code[i] in Q13 gain_cod in Q1 */
L_temp = L_mult(exc[i+i_subfr], gain_pit);
L_temp = L_mac(L_temp, code[i], gain_code);
L_temp = L_shl(L_temp, 1);
exc[i+i_subfr] = _round(L_temp);
}
update_exc_err(gain_pit, T0);
for (i = L_SUBFR-M, j = 0; i < L_SUBFR; i++, j++)
{
temp = extract_h(L_shl( L_mult(y1[i], gain_pit), 1) );
k = extract_h(L_shl( L_mult(y2[i], gain_code), 2) );
mem_w0[j] = sub(xn[i], add(temp, k));
}
Aq += MP1; /* interpolated LPC parameters for next subframe */
Ap += MP1;
}
/*--------------------------------------------------*
* Update signal for next frame. *
* -> shift to the left by L_FRAME: *
* speech[], wsp[] and exc[] *
*--------------------------------------------------*/
Copy(&old_speech[L_FRAME], &old_speech[0], L_TOTAL-L_FRAME);
Copy(&old_wsp[L_FRAME], &old_wsp[0], PIT_MAX);
Copy(&old_exc[L_FRAME], &old_exc[0], PIT_MAX+L_INTERPOL);
return;
}