-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathdec_ld8a.c
More file actions
380 lines (289 loc) · 12.2 KB
/
Copy pathdec_ld8a.c
File metadata and controls
380 lines (289 loc) · 12.2 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
/*
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 Init_Decod_ld8a and Decod_ld8a *
*-----------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include "typedef.h"
#include "basic_op.h"
#include "ld8a.h"
#include "dtx.h"
#include "sid.h"
/*---------------------------------------------------------------*
* Decoder constant parameters (defined in "ld8a.h") *
*---------------------------------------------------------------*
* L_FRAME : Frame size. *
* L_SUBFR : Sub-frame size. *
* M : LPC order. *
* MP1 : LPC order+1 *
* PIT_MIN : Minimum pitch lag. *
* PIT_MAX : Maximum pitch lag. *
* L_INTERPOL : Length of filter for interpolation *
* PRM_SIZE : Size of vector containing analysis parameters *
*---------------------------------------------------------------*/
/*--------------------------------------------------------*
* Static memory allocation. *
*--------------------------------------------------------*/
/* Excitation vector */
static Word16 old_exc[L_FRAME+PIT_MAX+L_INTERPOL];
static Word16 *exc;
/* Lsp (Line spectral pairs) */
static Word16 lsp_old[M]={
30000, 26000, 21000, 15000, 8000, 0, -8000,-15000,-21000,-26000};
/* Filter's memory */
static Word16 mem_syn[M];
static Word16 sharp; /* pitch sharpening of previous frame */
static Word16 old_T0; /* integer delay of previous frame */
static Word16 gain_code; /* Code gain */
static Word16 gain_pitch; /* Pitch gain */
/* for G.729B */
static Word16 seed_fer;
/* CNG variables */
static Word16 past_ftyp;
static Word16 seed;
static Word16 sid_sav, sh_sid_sav;
/*-----------------------------------------------------------------*
* Function Init_Decod_ld8a *
* ~~~~~~~~~~~~~~~ *
* *
* ->Initialization of variables for the decoder section. *
* *
*-----------------------------------------------------------------*/
void Init_Decod_ld8a(void)
{
/* Initialize static pointer */
exc = old_exc + PIT_MAX + L_INTERPOL;
/* Static vectors to zero */
Set_zero(old_exc, PIT_MAX+L_INTERPOL);
Set_zero(mem_syn, M);
sharp = SHARPMIN;
old_T0 = 60;
gain_code = 0;
gain_pitch = 0;
Lsp_decw_reset();
/* for G.729B */
seed_fer = 21845;
past_ftyp = 1;
seed = INIT_SEED;
sid_sav = 0;
sh_sid_sav = 1;
Init_lsfq_noise();
return;
}
/*-----------------------------------------------------------------*
* Function Decod_ld8a *
* ~~~~~~~~~~ *
* ->Main decoder routine. *
* *
*-----------------------------------------------------------------*/
void Decod_ld8a(
Word16 parm[], /* (i) : vector of synthesis parameters
parm[0] = bad frame indicator (bfi) */
Word16 synth[], /* (o) : synthesis speech */
Word16 A_t[], /* (o) : decoded LP filter in 2 subframes */
Word16 *T2, /* (o) : decoded pitch lag in 2 subframes */
Word16 *Vad /* (o) : frame type */
)
{
Word16 *Az; /* Pointer on A_t */
Word16 lsp_new[M]; /* LSPs */
Word16 code[L_SUBFR]; /* ACELP codevector */
/* Scalars */
Word16 i, j, i_subfr;
Word16 T0, T0_frac, index;
Word16 bfi;
Word32 L_temp;
Word16 bad_pitch; /* bad pitch indicator */
extern Word16 bad_lsf; /* bad LSF indicator */
/* for G.729B */
Word16 ftyp;
Word16 lsfq_mem[MA_NP][M];
/* Test bad frame indicator (bfi) */
bfi = *parm++;
/* for G.729B */
ftyp = *parm;
if(bfi == 1) {
if(past_ftyp == 1) ftyp = 1;
else ftyp = 0;
*parm = ftyp; /* modification introduced in version V1.3 */
}
*Vad = ftyp;
/* Processing non active frames (SID & not transmitted) */
if(ftyp != 1) {
Get_decfreq_prev(lsfq_mem);
Dec_cng(past_ftyp, sid_sav, sh_sid_sav, parm, exc, lsp_old,
A_t, &seed, lsfq_mem);
Update_decfreq_prev(lsfq_mem);
Az = A_t;
for (i_subfr = 0; i_subfr < L_FRAME; i_subfr += L_SUBFR) {
Overflow = 0;
Syn_filt(Az, &exc[i_subfr], &synth[i_subfr], L_SUBFR, mem_syn, 0);
if(Overflow != 0) {
/* In case of overflow in the synthesis */
/* -> Scale down vector exc[] and redo synthesis */
for(i=0; i<PIT_MAX+L_INTERPOL+L_FRAME; i++)
old_exc[i] = shr(old_exc[i], 2);
Syn_filt(Az, &exc[i_subfr], &synth[i_subfr], L_SUBFR, mem_syn, 1);
}
else
Copy(&synth[i_subfr+L_SUBFR-M], mem_syn, M);
Az += MP1;
*T2++ = old_T0;
}
sharp = SHARPMIN;
}
/* Processing active frame */
else {
seed = INIT_SEED;
parm++;
/* Decode the LSPs */
D_lsp(parm, lsp_new, add(bfi, bad_lsf));
parm += 2;
/*
Note: "bad_lsf" is introduce in case the standard is used with
channel protection.
*/
/* Interpolation of LPC for the 2 subframes */
Int_qlpc(lsp_old, lsp_new, A_t);
/* update the LSFs for the next frame */
Copy(lsp_new, lsp_old, M);
/*------------------------------------------------------------------------*
* Loop for every subframe in the analysis frame *
*------------------------------------------------------------------------*
* The subframe size is L_SUBFR and the loop is repeated L_FRAME/L_SUBFR *
* times *
* - decode the pitch delay *
* - decode algebraic code *
* - decode pitch and codebook gains *
* - find the excitation and compute synthesis speech *
*------------------------------------------------------------------------*/
Az = A_t; /* pointer to interpolated LPC parameters */
for (i_subfr = 0; i_subfr < L_FRAME; i_subfr += L_SUBFR)
{
index = *parm++; /* pitch index */
if(i_subfr == 0)
{
i = *parm++; /* get parity check result */
bad_pitch = add(bfi, i);
if( bad_pitch == 0)
{
Dec_lag3(index, PIT_MIN, PIT_MAX, i_subfr, &T0, &T0_frac);
old_T0 = T0;
}
else /* Bad frame, or parity error */
{
T0 = old_T0;
T0_frac = 0;
old_T0 = add( old_T0, 1);
if( sub(old_T0, PIT_MAX) > 0) {
old_T0 = PIT_MAX;
}
}
}
else /* second subframe */
{
if( bfi == 0)
{
Dec_lag3(index, PIT_MIN, PIT_MAX, i_subfr, &T0, &T0_frac);
old_T0 = T0;
}
else
{
T0 = old_T0;
T0_frac = 0;
old_T0 = add( old_T0, 1);
if( sub(old_T0, PIT_MAX) > 0) {
old_T0 = PIT_MAX;
}
}
}
*T2++ = T0;
/*-------------------------------------------------*
* - Find the adaptive codebook vector. *
*-------------------------------------------------*/
Pred_lt_3(&exc[i_subfr], T0, T0_frac, L_SUBFR);
/*-------------------------------------------------------*
* - Decode innovative codebook. *
* - Add the fixed-gain pitch contribution to code[]. *
*-------------------------------------------------------*/
if(bfi != 0) /* Bad frame */
{
parm[0] = Random(&seed_fer) & (Word16)0x1fff; /* 13 bits random */
parm[1] = Random(&seed_fer) & (Word16)0x000f; /* 4 bits random */
}
Decod_ACELP(parm[1], parm[0], code);
parm +=2;
j = shl(sharp, 1); /* From Q14 to Q15 */
if(sub(T0, L_SUBFR) <0 ) {
for (i = T0; i < L_SUBFR; i++) {
code[i] = add(code[i], mult(code[i-T0], j));
}
}
/*-------------------------------------------------*
* - Decode pitch and codebook gains. *
*-------------------------------------------------*/
index = *parm++; /* index of energy VQ */
Dec_gain(index, code, L_SUBFR, bfi, &gain_pitch, &gain_code);
/*-------------------------------------------------------------*
* - Update pitch sharpening "sharp" with quantized gain_pitch *
*-------------------------------------------------------------*/
sharp = gain_pitch;
if (sub(sharp, SHARPMAX) > 0) { sharp = SHARPMAX; }
if (sub(sharp, SHARPMIN) < 0) { sharp = SHARPMIN; }
/*-------------------------------------------------------*
* - Find the total excitation. *
* - Find synthesis speech corresponding to exc[]. *
*-------------------------------------------------------*/
for (i = 0; i < L_SUBFR; i++)
{
/* exc[i] = gain_pitch*exc[i] + gain_code*code[i]; */
/* exc[i] in Q0 gain_pitch in Q14 */
/* code[i] in Q13 gain_codeode in Q1 */
L_temp = L_mult(exc[i+i_subfr], gain_pitch);
L_temp = L_mac(L_temp, code[i], gain_code);
L_temp = L_shl(L_temp, 1);
exc[i+i_subfr] = round(L_temp);
}
Overflow = 0;
Syn_filt(Az, &exc[i_subfr], &synth[i_subfr], L_SUBFR, mem_syn, 0);
if(Overflow != 0)
{
/* In case of overflow in the synthesis */
/* -> Scale down vector exc[] and redo synthesis */
for(i=0; i<PIT_MAX+L_INTERPOL+L_FRAME; i++)
old_exc[i] = shr(old_exc[i], 2);
Syn_filt(Az, &exc[i_subfr], &synth[i_subfr], L_SUBFR, mem_syn, 1);
}
else
Copy(&synth[i_subfr+L_SUBFR-M], mem_syn, M);
Az += MP1; /* interpolated LPC parameters for next subframe */
}
}
/*------------*
* For G729b
*-----------*/
if(bfi == 0) {
L_temp = 0L;
for(i=0; i<L_FRAME; i++) {
L_temp = L_mac(L_temp, exc[i], exc[i]);
} /* may overflow => last level of SID quantizer */
sh_sid_sav = norm_l(L_temp);
sid_sav = round(L_shl(L_temp, sh_sid_sav));
sh_sid_sav = sub(16, sh_sid_sav);
}
/*--------------------------------------------------*
* Update signal for next frame. *
* -> shift to the left by L_FRAME exc[] *
*--------------------------------------------------*/
Copy(&old_exc[L_FRAME], &old_exc[0], PIT_MAX+L_INTERPOL);
/* for G729b */
past_ftyp = ftyp;
return;
}