-
Notifications
You must be signed in to change notification settings - Fork 3
/
lspgetq.c
233 lines (186 loc) · 5.48 KB
/
lspgetq.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
/*
ITU-T G.729A Speech Coder ANSI-C Source Code
Version 1.1 Last modified: September 1996
Copyright (c) 1996,
AT&T, France Telecom, NTT, Universite de Sherbrooke, Lucent Technologies
All rights reserved.
*/
#include <stdio.h>
#include "typedef.h"
#include "basic_op.h"
#include "ld8a.h"
void Lsp_get_quant(
int16_t lspcb1[][M], /* (i) Q13 : first stage LSP codebook */
int16_t lspcb2[][M], /* (i) Q13 : Second stage LSP codebook */
int16_t code0, /* (i) : selected code of first stage */
int16_t code1, /* (i) : selected code of second stage */
int16_t code2, /* (i) : selected code of second stage */
int16_t fg[][M], /* (i) Q15 : MA prediction coef. */
int16_t freq_prev[][M], /* (i) Q13 : previous LSP vector */
int16_t lspq[], /* (o) Q13 : quantized LSP parameters */
int16_t fg_sum[] /* (i) Q15 : present MA prediction coef. */
)
{
int16_t j;
int16_t buf[M]; /* Q13 */
for ( j = 0 ; j < NC ; j++ )
buf[j] = add( lspcb1[code0][j], lspcb2[code1][j] );
for ( j = NC ; j < M ; j++ )
buf[j] = add( lspcb1[code0][j], lspcb2[code2][j] );
Lsp_expand_1_2(buf, GAP1);
Lsp_expand_1_2(buf, GAP2);
Lsp_prev_compose(buf, lspq, fg, freq_prev, fg_sum);
Lsp_prev_update(buf, freq_prev);
Lsp_stability( lspq );
return;
}
void Lsp_expand_1(
int16_t buf[], /* (i/o) Q13 : LSP vectors */
int16_t gap /* (i) Q13 : gap */
)
{
int16_t j, tmp;
int16_t diff; /* Q13 */
for ( j = 1 ; j < NC ; j++ ) {
diff = sub( buf[j-1], buf[j] );
tmp = shr( add( diff, gap), 1 );
if ( tmp > 0 ) {
buf[j-1] = sub( buf[j-1], tmp );
buf[j] = add( buf[j], tmp );
}
}
return;
}
void Lsp_expand_2(
int16_t buf[], /* (i/o) Q13 : LSP vectors */
int16_t gap /* (i) Q13 : gap */
)
{
int16_t j, tmp;
int16_t diff; /* Q13 */
for ( j = NC ; j < M ; j++ ) {
diff = sub( buf[j-1], buf[j] );
tmp = shr( add( diff, gap), 1 );
if ( tmp > 0 ) {
buf[j-1] = sub( buf[j-1], tmp );
buf[j] = add( buf[j], tmp );
}
}
return;
}
void Lsp_expand_1_2(
int16_t buf[], /* (i/o) Q13 : LSP vectors */
int16_t gap /* (i) Q13 : gap */
)
{
int16_t j, tmp;
int16_t diff; /* Q13 */
for ( j = 1 ; j < M ; j++ ) {
diff = sub( buf[j-1], buf[j] );
tmp = shr( add( diff, gap), 1 );
if ( tmp > 0 ) {
buf[j-1] = sub( buf[j-1], tmp );
buf[j] = add( buf[j], tmp );
}
}
return;
}
/*
Functions which use previous LSP parameter (freq_prev).
*/
/*
compose LSP parameter from elementary LSP with previous LSP.
*/
void Lsp_prev_compose(
int16_t lsp_ele[], /* (i) Q13 : LSP vectors */
int16_t lsp[], /* (o) Q13 : quantized LSP parameters */
int16_t fg[][M], /* (i) Q15 : MA prediction coef. */
int16_t freq_prev[][M], /* (i) Q13 : previous LSP vector */
int16_t fg_sum[] /* (i) Q15 : present MA prediction coef. */
)
{
int16_t j, k;
int32_t L_acc; /* Q29 */
for ( j = 0 ; j < M ; j++ ) {
L_acc = L_mult( lsp_ele[j], fg_sum[j] );
for ( k = 0 ; k < MA_NP ; k++ )
L_acc = L_mac( L_acc, freq_prev[k][j], fg[k][j] );
lsp[j] = extract_h(L_acc);
}
return;
}
/*
extract elementary LSP from composed LSP with previous LSP
*/
void Lsp_prev_extract(
int16_t lsp[M], /* (i) Q13 : unquantized LSP parameters */
int16_t lsp_ele[M], /* (o) Q13 : target vector */
int16_t fg[MA_NP][M], /* (i) Q15 : MA prediction coef. */
int16_t freq_prev[MA_NP][M], /* (i) Q13 : previous LSP vector */
int16_t fg_sum_inv[M] /* (i) Q12 : inverse previous LSP vector */
)
{
int16_t j, k;
int32_t L_temp; /* Q19 */
int16_t temp; /* Q13 */
for ( j = 0 ; j < M ; j++ ) {
L_temp = L_deposit_h(lsp[j]);
for ( k = 0 ; k < MA_NP ; k++ )
L_temp = L_msu( L_temp, freq_prev[k][j], fg[k][j] );
temp = extract_h(L_temp);
L_temp = L_mult( temp, fg_sum_inv[j] );
lsp_ele[j] = extract_h( L_shl( L_temp, 3 ) );
}
return;
}
/*
update previous LSP parameter
*/
void Lsp_prev_update(
int16_t lsp_ele[M], /* (i) Q13 : LSP vectors */
int16_t freq_prev[MA_NP][M] /* (i/o) Q13 : previous LSP vectors */
)
{
int16_t k;
for ( k = MA_NP-1 ; k > 0 ; k-- )
Copy(freq_prev[k-1], freq_prev[k], M);
Copy(lsp_ele, freq_prev[0], M);
return;
}
void Lsp_stability(
int16_t buf[] /* (i/o) Q13 : quantized LSP parameters */
)
{
int16_t j;
int16_t tmp;
int32_t L_diff;
int32_t L_acc, L_accb;
for(j=0; j<M-1; j++) {
L_acc = L_deposit_l( buf[j+1] );
L_accb = L_deposit_l( buf[j] );
L_diff = L_sub( L_acc, L_accb );
if( L_diff < 0L ) {
/* exchange buf[j]<->buf[j+1] */
tmp = buf[j+1];
buf[j+1] = buf[j];
buf[j] = tmp;
}
}
if( sub(buf[0], L_LIMIT) <0 ) {
buf[0] = L_LIMIT;
printf("lsp_stability warning Low \n");
}
for(j=0; j<M-1; j++) {
L_acc = L_deposit_l( buf[j+1] );
L_accb = L_deposit_l( buf[j] );
L_diff = L_sub( L_acc, L_accb );
if( L_sub(L_diff, GAP3)<0L ) {
buf[j+1] = add( buf[j], GAP3 );
}
}
if( sub(buf[M-1],M_LIMIT)>0 ) {
buf[M-1] = M_LIMIT;
printf("lsp_stability warning High \n");
}
return;
}