-
Notifications
You must be signed in to change notification settings - Fork 3
/
dec_sid.c
196 lines (156 loc) · 5.29 KB
/
dec_sid.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
/*
ITU-T G.729A Annex B ANSI-C Source Code
Version 1.4 Last modified: November 2000
Copyright (c) 1996, France Telecom, Rockwell International,
Universite de Sherbrooke.
All rights reserved.
*/
/*
**
** File: "dec_cng.c"
**
** Description: Comfort noise generation
** performed at the decoder part
**
*/
/**** Fixed point version ***/
#include <stdio.h>
#include <stdlib.h>
#include "typedef.h"
#include "ld8a.h"
#include "tab_ld8a.h"
#include "basic_op.h"
#include "vad.h"
#include "dtx.h"
#include "sid.h"
#include "tab_dtx.h"
static int16_t cur_gain;
static int16_t lspSid[M]={
31441, 27566, 21458, 13612, 4663,
-4663, -13612, -21458, -27566, -31441};
static int16_t sid_gain;
/*
**
** Function: Init_Dec_cng()
**
** Description: Initialize dec_cng static variables
**
**
*/
void Init_Dec_cng(void)
{
sid_gain = tab_Sidgain[0];
return;
}
/*-----------------------------------------------------------*
* procedure Dec_cng: *
* ~~~~~~~~ *
* Receives frame type *
* 0 : for untransmitted frames *
* 2 : for SID frames *
* Decodes SID frames *
* Computes current frame excitation *
* Computes current frame LSPs
*-----------------------------------------------------------*/
void Dec_cng(
int16_t past_ftyp, /* (i) : past frame type */
int16_t sid_sav, /* (i) : energy to recover SID gain */
int16_t sh_sid_sav, /* (i) : corresponding scaling factor */
int16_t *parm, /* (i) : coded SID parameters */
int16_t *exc, /* (i/o) : excitation array */
int16_t *lsp_old, /* (i/o) : previous lsp */
int16_t *A_t, /* (o) : set of interpolated LPC coefficients */
int16_t *seed, /* (i/o) : random generator seed */
int16_t freq_prev[MA_NP][M]
/* (i/o) : previous LPS for quantization */
)
{
int16_t temp, ind;
int16_t dif;
dif = sub(past_ftyp, 1);
/* SID Frame */
/*************/
if(parm[0] != 0) {
sid_gain = tab_Sidgain[(int)parm[4]];
/* Inverse quantization of the LSP */
sid_lsfq_decode(&parm[1], lspSid, freq_prev);
}
/* non SID Frame */
/*****************/
else {
/* Case of 1st SID frame erased : quantize-decode */
/* energy estimate stored in sid_gain */
if(dif == 0) {
Qua_Sidgain(&sid_sav, &sh_sid_sav, 0, &temp, &ind);
sid_gain = tab_Sidgain[(int)ind];
}
}
if(dif == 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_DEC);
/* Interpolate the Lsp vectors */
Int_qlpc(lsp_old, lspSid, A_t);
Copy(lspSid, lsp_old, M);
return;
}
/*---------------------------------------------------------------------------*
* Function Init_lsfq_noise *
* ~~~~~~~~~~~~~~~~~~~~~~~~~ *
* *
* -> Initialization of variables for the lsf quantization in the SID *
* *
*---------------------------------------------------------------------------*/
void Init_lsfq_noise(void)
{
int16_t i, j;
int32_t acc0;
/* initialize the noise_fg */
for (i=0; i<4; i++)
Copy(fg[0][i], noise_fg[0][i], M);
for (i=0; i<4; i++)
for (j=0; j<M; j++){
acc0 = L_mult(fg[0][i][j], 19660);
acc0 = L_mac(acc0, fg[1][i][j], 13107);
noise_fg[1][i][j] = extract_h(acc0);
}
}
void sid_lsfq_decode(int16_t *index, /* (i) : quantized indices */
int16_t *lspq, /* (o) : quantized lsp vector */
int16_t freq_prev[MA_NP][M] /* (i) : memory of predictor */
)
{
int32_t acc0;
int16_t i, j, k, lsfq[M], tmpbuf[M];
/* get the lsf error vector */
Copy(lspcb1[PtrTab_1[index[1]]], tmpbuf, M);
for (i=0; i<M/2; i++)
tmpbuf[i] = add(tmpbuf[i], lspcb2[PtrTab_2[0][index[2]]][i]);
for (i=M/2; i<M; i++)
tmpbuf[i] = add(tmpbuf[i], lspcb2[PtrTab_2[1][index[2]]][i]);
/* guarantee minimum distance of 0.0012 (~10 in Q13) between tmpbuf[j]
and tmpbuf[j+1] */
for (j=1; j<M; j++){
acc0 = L_mult(tmpbuf[j-1], 16384);
acc0 = L_mac(acc0, tmpbuf[j], -16384);
acc0 = L_mac(acc0, 10, 16384);
k = extract_h(acc0);
if (k > 0){
tmpbuf[j-1] = sub(tmpbuf[j-1], k);
tmpbuf[j] = add(tmpbuf[j], k);
}
}
/* compute the quantized lsf vector */
Lsp_prev_compose(tmpbuf, lsfq, noise_fg[index[0]], freq_prev,
noise_fg_sum[index[0]]);
/* update the prediction memory */
Lsp_prev_update(tmpbuf, freq_prev);
/* lsf stability check */
Lsp_stability(lsfq);
/* convert lsf to lsp */
Lsf_lsp2(lsfq, lspq, M);
}