vencode.c
5.12 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
#include "vadpcm.h"
int
vencodeframe(FILE *ofile, short *inBuffer, int *state, int ***coefTable, int order, int npredictors, int nsam)
{
short
ix[FRAMESIZE];
int
prediction[FRAMESIZE],
inVector[FRAMESIZE],
saveState[FRAMESIZE],
optimalp,
scale,
llevel,
ulevel,
i,
j,
k,
ie[FRAMESIZE],
nIter,
max,
cV,
maxClip;
unsigned char
header,
c;
float
e[FRAMESIZE],
se,
min;
/*
* Clear input for nsam less than FRAMESIZE
*/
for (i=nsam; i<FRAMESIZE; i++)
inBuffer[i] = 0;
/*
* Maximum and minimum allowable levels
*/
llevel = - (short) (1L<<(SAMPLEBITS-1));
ulevel = -llevel - 1;
/*
* Find the prediction error for each possible predictor. Uses the
* vector method.
*/
min = 1e30; optimalp = 0;
for (k=0; k<npredictors; k++){
/*
* Set up previous frame outputs - use quantized outputs
*/
for (i=0; i<order; i++)
inVector[i] = state[FRAMESIZE-order+i];
for (i=0; i<VECTORSIZE; i++){
prediction[i] = inner_product(order+i, coefTable[k][i], inVector);
inVector[i+order] = inBuffer[i] - prediction[i];
e[i] = (float) inVector[i+order];
}
/*
* Set up previous vector outputs, for next lot of VECTORSIZE
*/
for (i=0; i<order; i++)
inVector[i] = prediction[VECTORSIZE-order+i]+inVector[VECTORSIZE+i];
for (i=0; i<VECTORSIZE; i++){
prediction[i+VECTORSIZE] = inner_product(order+i, coefTable[k][i], inVector);
inVector[i+order] = inBuffer[VECTORSIZE+i] - prediction[i+VECTORSIZE];
e[i+VECTORSIZE] = (float) inVector[i+order];
}
/*
* Now find the error measure for this frame and check
* against the current best.
*/
se = 0;
for (j=0; j<FRAMESIZE; j++)
se += e[j]*e[j];
if (se<min){
min=se;
optimalp = k;
}
}
/*
* Re-calculate final prediction error using optimal predictor
*/
for (i=0; i<order; i++)
inVector[i] = state[FRAMESIZE-order+i];
for (i=0; i<VECTORSIZE; i++){
prediction[i] = inner_product(order+i, coefTable[optimalp][i], inVector);
inVector[i+order] = inBuffer[i] - prediction[i];
e[i] = (float) inVector[i+order];
}
for (i=0; i<order; i++)
inVector[i] = prediction[VECTORSIZE-order+i]+inVector[VECTORSIZE+i];
for (i=0; i<VECTORSIZE; i++){
prediction[i+VECTORSIZE] = inner_product(order+i, coefTable[optimalp][i], inVector);
inVector[i+order] = inBuffer[VECTORSIZE+i] - prediction[i+VECTORSIZE];
e[i+VECTORSIZE] = (float) inVector[i+order];
}
/*
* Find range value
*/
clamp(FRAMESIZE, e, ie, 16);
max = 0;
for (i=0; i<FRAMESIZE; i++)
if (fabs(ie[i])>fabs(max))
max = ie[i];
for (scale=0; scale<=MAXSCALE; scale++){
if ((max<=ulevel) && (max>=llevel))
break;
max /= 2;
}
/*
* Final prediction error with a quantizer in the loop
*/
for (i=0; i<FRAMESIZE; i++)
saveState[i] = state[i];
scale--;
nIter = 0;
do {
nIter++;
maxClip = 0;
scale++;
if (scale > MAXSCALE)
scale = MAXSCALE;
for (i=0; i<order; i++)
inVector[i] = saveState[FRAMESIZE-order+i];
for (i=0; i<VECTORSIZE; i++){
prediction[i] = inner_product(order+i, coefTable[optimalp][i], inVector);
se = (float) inBuffer[i] - prediction[i];
ix[i] = qsample(se, 1<<scale);
cV = (short) clip((int) ix[i], llevel, ulevel) - ix[i];
if (abs(cV) > maxClip)
maxClip = abs(cV);
ix[i] += cV;
inVector[i+order] = ix[i]*(1<<scale);
/*
* Decoder output
*/
state[i] = prediction[i] + inVector[i+order];
}
for (i=0; i<order; i++)
inVector[i] = state[VECTORSIZE-order+i];
for (i=0; i<VECTORSIZE; i++){
prediction[i+VECTORSIZE] = inner_product(order+i, coefTable[optimalp][i], inVector);
se = (float) inBuffer[VECTORSIZE+i] - prediction[VECTORSIZE+i];
ix[VECTORSIZE+i] = qsample(se, 1<<scale);
cV = (short) clip((int) ix[i+VECTORSIZE], llevel, ulevel) - ix[i+VECTORSIZE];
if (abs(cV) > maxClip)
maxClip = abs(cV);
ix[i+VECTORSIZE] += cV;
inVector[i+order] = ix[VECTORSIZE+i]*(1<<scale);
state[VECTORSIZE+i] = prediction[i+VECTORSIZE] + inVector[i+order];
}
}
while ((maxClip > MAXCLIP) && (nIter < MAXITER));
/*
* Write frame header and bitstream
*/
header = (scale<<4) | ((optimalp) & 0xf);
fwrite(&header, 1, 1, ofile);
for (i=0; i<FRAMESIZE; i+=2){
c = (ix[i]<<4) | (ix[i+1] & 0xf);
fwrite(&c, 1, 1, ofile);
}
}