-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathdtx.c
More file actions
441 lines (370 loc) · 11.8 KB
/
Copy pathdtx.c
File metadata and controls
441 lines (370 loc) · 11.8 KB
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
/*
ITU-T G.729A Annex B ANSI-C Source Code
Version 1.3 Last modified: August 1997
Copyright (c) 1996, France Telecom, Rockwell International,
Universite de Sherbrooke.
All rights reserved.
*/
/* DTX and Comfort Noise Generator - Encoder part */
#include <stdio.h>
#include <stdlib.h>
#include "typedef.h"
#include "basic_op.h"
#include "ld8a.h"
#include "oper_32b.h"
#include "tab_ld8a.h"
#include "vad.h"
#include "dtx.h"
#include "tab_dtx.h"
#include "sid.h"
/* Static Variables */
static Word16 lspSid_q[M] ;
static Word16 pastCoeff[MP1];
static Word16 RCoeff[MP1];
static Word16 sh_RCoeff;
static Word16 Acf[SIZ_ACF];
static Word16 sh_Acf[NB_CURACF];
static Word16 sumAcf[SIZ_SUMACF];
static Word16 sh_sumAcf[NB_SUMACF];
static Word16 ener[NB_GAIN];
static Word16 sh_ener[NB_GAIN];
static Word16 fr_cur;
static Word16 cur_gain;
static Word16 nb_ener;
static Word16 sid_gain;
static Word16 flag_chang;
static Word16 prev_energy;
static Word16 count_fr0;
/* Local functions */
static void Calc_pastfilt(Word16 *Coeff);
static void Calc_RCoeff(Word16 *Coeff, Word16 *RCoeff, Word16 *sh_RCoeff);
static Word16 Cmp_filt(Word16 *RCoeff, Word16 sh_RCoeff, Word16 *acf,
Word16 alpha, Word16 Fracthresh);
static void Calc_sum_acf(Word16 *acf, Word16 *sh_acf,
Word16 *sum, Word16 *sh_sum, Word16 nb);
static void Update_sumAcf(void);
/*-----------------------------------------------------------*
* procedure Init_Cod_cng: *
* ~~~~~~~~~~~~ *
* Initialize variables used for dtx at the encoder *
*-----------------------------------------------------------*/
void Init_Cod_cng(void)
{
Word16 i;
for(i=0; i<SIZ_SUMACF; i++) sumAcf[i] = 0;
for(i=0; i<NB_SUMACF; i++) sh_sumAcf[i] = 40;
for(i=0; i<SIZ_ACF; i++) Acf[i] = 0;
for(i=0; i<NB_CURACF; i++) sh_Acf[i] = 40;
for(i=0; i<NB_GAIN; i++) sh_ener[i] = 40;
for(i=0; i<NB_GAIN; i++) ener[i] = 0;
cur_gain = 0;
fr_cur = 0;
flag_chang = 0;
return;
}
/*-----------------------------------------------------------*
* procedure Cod_cng: *
* ~~~~~~~~ *
* computes DTX decision *
* encodes SID frames *
* computes CNG excitation for encoder update *
*-----------------------------------------------------------*/
void Cod_cng(
Word16 *exc, /* (i/o) : excitation array */
Word16 pastVad, /* (i) : previous VAD decision */
Word16 *lsp_old_q, /* (i/o) : previous quantized lsp */
Word16 *Aq, /* (o) : set of interpolated LPC coefficients */
Word16 *ana, /* (o) : coded SID parameters */
Word16 freq_prev[MA_NP][M],
/* (i/o) : previous LPS for quantization */
Word16 *seed /* (i/o) : random generator seed */
)
{
Word16 i;
Word16 curAcf[MP1];
Word16 bid[M], zero[MP1];
Word16 curCoeff[MP1];
Word16 lsp_new[M];
Word16 *lpcCoeff;
Word16 cur_igain;
Word16 energyq, temp;
/* Update Ener and sh_ener */
for(i = NB_GAIN-1; i>=1; i--) {
ener[i] = ener[i-1];
sh_ener[i] = sh_ener[i-1];
}
/* Compute current Acfs */
Calc_sum_acf(Acf, sh_Acf, curAcf, &sh_ener[0], NB_CURACF);
/* Compute LPC coefficients and residual energy */
if(curAcf[0] == 0) {
ener[0] = 0; /* should not happen */
}
else {
Set_zero(zero, MP1);
Levinson(curAcf, zero, curCoeff, bid, &ener[0]);
}
/* if first frame of silence => SID frame */
if(pastVad != 0) {
ana[0] = 2;
count_fr0 = 0;
nb_ener = 1;
Qua_Sidgain(ener, sh_ener, nb_ener, &energyq, &cur_igain);
}
else {
nb_ener = add(nb_ener, 1);
if(sub(nb_ener, NB_GAIN) > 0) nb_ener = NB_GAIN;
Qua_Sidgain(ener, sh_ener, nb_ener, &energyq, &cur_igain);
/* Compute stationarity of current filter */
/* versus reference filter */
if(Cmp_filt(RCoeff, sh_RCoeff, curAcf, ener[0], FRAC_THRESH1) != 0) {
flag_chang = 1;
}
/* compare energy difference between current frame and last frame */
temp = abs_s(sub(prev_energy, energyq));
temp = sub(temp, 2);
if (temp > 0) flag_chang = 1;
count_fr0 = add(count_fr0, 1);
if(sub(count_fr0, FR_SID_MIN) < 0) {
ana[0] = 0; /* no transmission */
}
else {
if(flag_chang != 0) {
ana[0] = 2; /* transmit SID frame */
}
else{
ana[0] = 0;
}
count_fr0 = FR_SID_MIN; /* to avoid overflow */
}
}
if(sub(ana[0], 2) == 0) {
/* Reset frame count and change flag */
count_fr0 = 0;
flag_chang = 0;
/* Compute past average filter */
Calc_pastfilt(pastCoeff);
Calc_RCoeff(pastCoeff, RCoeff, &sh_RCoeff);
/* Compute stationarity of current filter */
/* versus past average filter */
/* if stationary */
/* transmit average filter => new ref. filter */
if(Cmp_filt(RCoeff, sh_RCoeff, curAcf, ener[0], FRAC_THRESH2) == 0) {
lpcCoeff = pastCoeff;
}
/* else */
/* transmit current filter => new ref. filter */
else {
lpcCoeff = curCoeff;
Calc_RCoeff(curCoeff, RCoeff, &sh_RCoeff);
}
/* Compute SID frame codes */
Az_lsp(lpcCoeff, lsp_new, lsp_old_q); /* From A(z) to lsp */
/* LSP quantization */
lsfq_noise(lsp_new, lspSid_q, freq_prev, &ana[1]);
prev_energy = energyq;
ana[4] = cur_igain;
sid_gain = tab_Sidgain[cur_igain];
} /* end of SID frame case */
/* Compute new excitation */
if(pastVad != 0) {
cur_gain = sid_gain;
}
else {
cur_gain = mult_r(cur_gain, A_GAIN0);
cur_gain = add(cur_gain, mult_r(sid_gain, A_GAIN1));
}
Calc_exc_rand(cur_gain, exc, seed, FLAG_COD);
Int_qlpc(lsp_old_q, lspSid_q, Aq);
for(i=0; i<M; i++) {
lsp_old_q[i] = lspSid_q[i];
}
/* Update sumAcf if fr_cur = 0 */
if(fr_cur == 0) {
Update_sumAcf();
}
return;
}
/*-----------------------------------------------------------*
* procedure Update_cng: *
* ~~~~~~~~~~ *
* Updates autocorrelation arrays *
* used for DTX/CNG *
* If Vad=1 : updating of array sumAcf *
*-----------------------------------------------------------*/
void Update_cng(
Word16 *r_h, /* (i) : MSB of frame autocorrelation */
Word16 exp_r, /* (i) : scaling factor associated */
Word16 Vad /* (i) : current Vad decision */
)
{
Word16 i;
Word16 *ptr1, *ptr2;
/* Update Acf and shAcf */
ptr1 = Acf + SIZ_ACF - 1;
ptr2 = ptr1 - MP1;
for(i=0; i<(SIZ_ACF-MP1); i++) {
*ptr1-- = *ptr2--;
}
for(i=NB_CURACF-1; i>=1; i--) {
sh_Acf[i] = sh_Acf[i-1];
}
/* Save current Acf */
sh_Acf[0] = negate(add(16, exp_r));
for(i=0; i<MP1; i++) {
Acf[i] = r_h[i];
}
fr_cur = add(fr_cur, 1);
if(sub(fr_cur, NB_CURACF) == 0) {
fr_cur = 0;
if(Vad != 0) {
Update_sumAcf();
}
}
return;
}
/*-----------------------------------------------------------*
* Local procedures *
* ~~~~~~~~~~~~~~~~ *
*-----------------------------------------------------------*/
/* Compute scaled autocorr of LPC coefficients used for Itakura distance */
/*************************************************************************/
static void Calc_RCoeff(Word16 *Coeff, Word16 *RCoeff, Word16 *sh_RCoeff)
{
Word16 i, j;
Word16 sh1;
Word32 L_acc;
/* RCoeff[0] = SUM(j=0->M) Coeff[j] ** 2 */
L_acc = 0L;
for(j=0; j <= M; j++) {
L_acc = L_mac(L_acc, Coeff[j], Coeff[j]);
}
/* Compute exponent RCoeff */
sh1 = norm_l(L_acc);
L_acc = L_shl(L_acc, sh1);
RCoeff[0] = round(L_acc);
/* RCoeff[i] = SUM(j=0->M-i) Coeff[j] * Coeff[j+i] */
for(i=1; i<=M; i++) {
L_acc = 0L;
for(j=0; j<=M-i; j++) {
L_acc = L_mac(L_acc, Coeff[j], Coeff[j+i]);
}
L_acc = L_shl(L_acc, sh1);
RCoeff[i] = round(L_acc);
}
*sh_RCoeff = sh1;
return;
}
/* Compute Itakura distance and compare to threshold */
/*****************************************************/
static Word16 Cmp_filt(Word16 *RCoeff, Word16 sh_RCoeff, Word16 *acf,
Word16 alpha, Word16 FracThresh)
{
Word32 L_temp0, L_temp1;
Word16 temp1, temp2, sh[2], ind;
Word16 i;
Word16 diff, flag;
extern Flag Overflow;
sh[0] = 0;
sh[1] = 0;
ind = 1;
flag = 0;
do {
Overflow = 0;
temp1 = shr(RCoeff[0], sh[0]);
temp2 = shr(acf[0], sh[1]);
L_temp0 = L_shr(L_mult(temp1, temp2),1);
for(i=1; i <= M; i++) {
temp1 = shr(RCoeff[i], sh[0]);
temp2 = shr(acf[i], sh[1]);
L_temp0 = L_mac(L_temp0, temp1, temp2);
}
if(Overflow != 0) {
sh[(int)ind] = add(sh[(int)ind], 1);
ind = sub(1, ind);
}
else flag = 1;
} while (flag == 0);
temp1 = mult_r(alpha, FracThresh);
L_temp1 = L_add(L_deposit_l(temp1), L_deposit_l(alpha));
temp1 = add(sh_RCoeff, 9); /* 9 = Lpc_justif. * 2 - 16 + 1 */
temp2 = add(sh[0], sh[1]);
temp1 = sub(temp1, temp2);
L_temp1 = L_shl(L_temp1, temp1);
L_temp0 = L_sub(L_temp0, L_temp1);
if(L_temp0 > 0L) diff = 1;
else diff = 0;
return(diff);
}
/* Compute past average filter */
/*******************************/
static void Calc_pastfilt(Word16 *Coeff)
{
Word16 i;
Word16 s_sumAcf[MP1];
Word16 bid[M], zero[MP1];
Word16 temp;
Calc_sum_acf(sumAcf, sh_sumAcf, s_sumAcf, &temp, NB_SUMACF);
if(s_sumAcf[0] == 0L) {
Coeff[0] = 4096;
for(i=1; i<=M; i++) Coeff[i] = 0;
return;
}
Set_zero(zero, MP1);
Levinson(s_sumAcf, zero, Coeff, bid, &temp);
return;
}
/* Update sumAcf */
/*****************/
static void Update_sumAcf(void)
{
Word16 *ptr1, *ptr2;
Word16 i;
/*** Move sumAcf ***/
ptr1 = sumAcf + SIZ_SUMACF - 1;
ptr2 = ptr1 - MP1;
for(i=0; i<(SIZ_SUMACF-MP1); i++) {
*ptr1-- = *ptr2--;
}
for(i=NB_SUMACF-1; i>=1; i--) {
sh_sumAcf[i] = sh_sumAcf[i-1];
}
/* Compute new sumAcf */
Calc_sum_acf(Acf, sh_Acf, sumAcf, sh_sumAcf, NB_CURACF);
return;
}
/* Compute sum of acfs (curAcf, sumAcf or s_sumAcf) */
/****************************************************/
static void Calc_sum_acf(Word16 *acf, Word16 *sh_acf,
Word16 *sum, Word16 *sh_sum, Word16 nb)
{
Word16 *ptr1;
Word32 L_temp, L_tab[MP1];
Word16 sh0, temp;
Word16 i, j;
/* Compute sum = sum of nb acfs */
/* Find sh_acf minimum */
sh0 = sh_acf[0];
for(i=1; i<nb; i++) {
if(sub(sh_acf[i], sh0) < 0) sh0 = sh_acf[i];
}
sh0 = add(sh0, 14); /* 2 bits of margin */
for(j=0; j<MP1; j++) {
L_tab[j] = 0L;
}
ptr1 = acf;
for(i=0; i<nb; i++) {
temp = sub(sh0, sh_acf[i]);
for(j=0; j<MP1; j++) {
L_temp = L_deposit_l(*ptr1++);
L_temp = L_shl(L_temp, temp); /* shift right if temp<0 */
L_tab[j] = L_add(L_tab[j], L_temp);
}
}
temp = norm_l(L_tab[0]);
for(i=0; i<=M; i++) {
sum[i] = extract_h(L_shl(L_tab[i], temp));
}
temp = sub(temp, 16);
*sh_sum = add(sh0, temp);
return;
}