vadpcm_dec.c
8.08 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
#include "vadpcm.h"
#include <signal.h>
static void
int_handler(int sig)
{
/*
* Exit nicely on ^C signal
*/
int flags = fcntl(0, F_GETFL, 0);
flags &= ~O_NDELAY;
fcntl(0, F_SETFL, flags);
exit(0);
}
static char usage[] = "bitfile";
static const struct sigaction int_act = {SA_RESTART, int_handler, 0};
main(int argc, char *argv[])
{
extern char *optarg;
extern int optind;
int c;
char
cc,
doloop = 0;
short
order,
version,
nloops,
npredictors;
int
flags,
***coefTable = NULL,
i,
j,
*outp,
*state,
done = 0,
num,
ts,
soundPointer,
cType,
offset,
currPos = 0,
nSamples,
framePos,
loopBegin,
left;
ALADPCMloop
*aloops;
Chunk
FormChunk;
ChunkHeader
Header;
CommonChunk
CommChunk;
SoundDataChunk
SndDChunk;
char
*ChunkName;
FILE
*ifile;
char *progname = argv[0];
if (argc<2){
fprintf(stderr,"%s %s\n",progname,usage);
exit(1);
}
while ((c=getopt(argc,argv,"l"))!=-1){
switch (c) {
case 'l':
doloop = 1;
break;
}
}
argv += optind-1;
if ((ifile = fopen(argv[1], "r")) == NULL){
fprintf(stderr,"%s: bitstream file [%s] could not be opened\n",
progname, argv[1]);
exit(1);
}
state = (int *) malloc(FRAMESIZE*sizeof(int));
for (i=0; i<FRAMESIZE; i++)
state[i] = 0;
/*
* Read the FORM chunk and check for AIFC file
*/
fread(&FormChunk, sizeof(Chunk), 1, ifile);
if ((FormChunk.ckID != IFF_ID_FORM) ||
(FormChunk.formType != IFF_ID_AIFC)) {
fprintf(stderr,"%s: [%s] is not an AIFF-C File\n",progname,argv[1]);
exit(1);
}
/*
* Scan the aifc file for a COMMON chunk, SSND chunk
* and application specific chunks defining a codebook
* and loop points
*/
while (!done) {
num = fread(&Header, sizeof(ChunkHeader), 1, ifile);
if (num < 1) {
done = 1;
break;
}
Header.ckSize = ++Header.ckSize & ~1;
switch (Header.ckID) {
case (IFF_ID_COMM):
offset = ftell(ifile);
num = fread (&CommChunk, sizeof(CommonChunk), 1, ifile);
if (num < 1) {
fprintf(stderr,"%s: error parsing file [%s]\n", progname, argv[1]);
done = 1;
}
/*
* Check for a valid file format
*/
cType = (CommChunk.compressionTypeH<<16) + CommChunk.compressionTypeL;
if (cType != 'VAPC'){
fprintf(stderr,"%s: file [%s] is of the wrong compression type.\n",
progname, argv[1]);
exit(1);
}
if (CommChunk.numChannels != 1){
fprintf(stderr,"%s: file [%s] contains %ld channels, only 1 channel supported.\n",
progname, argv[1], CommChunk.numChannels);
exit(1);
}
if (CommChunk.sampleSize != 16){
fprintf(stderr,"%s: file [%s] contains %ld bit samples, only 16 bit samples supported.\n",
progname, argv[1], CommChunk.sampleSize);
exit(1);
}
nSamples = (CommChunk.numFramesH<<16) + CommChunk.numFramesL;
fseek(ifile, offset + Header.ckSize, SEEK_SET);
break;
case (IFF_ID_SSND):
offset = ftell(ifile);
fread(&SndDChunk, sizeof(SndDChunk), 1, ifile);
assert(SndDChunk.offset == 0);
assert(SndDChunk.blockSize == 0);
soundPointer = ftell(ifile);
fseek(ifile, offset + Header.ckSize, SEEK_SET);
break;
case (IFF_ID_APPL):
offset = ftell(ifile);
fread(&ts, sizeof(long), 1, ifile);
if (ts == 'stoc'){
ChunkName = ReadPString(ifile);
if (!strcmp(CODE_NAME, ChunkName)){
fread(&version, sizeof(short), 1, ifile);
if (version != VERSION)
fprintf(stderr,"Non-identical codebook chunk versions\n");
readaifccodebook(ifile, &coefTable, &order, &npredictors);
} else
if (!strcmp(LOOP_NAME, ChunkName)){
fread(&version, sizeof(short), 1, ifile);
if (version != VERSION)
fprintf(stderr,"Non-identical loop chunk versions\n");
aloops = readlooppoints(ifile, &nloops);
}
}
fseek(ifile, offset + Header.ckSize, SEEK_SET);
break;
default:
/* skip chunks we don't know about */
fseek(ifile, Header.ckSize, SEEK_CUR);
break;
}
}
if (coefTable==NULL){
fprintf(stderr,"%s: Codebook missing from bitstream [%s]\n",argv[0], argv[1]);
exit(1);
}
outp = (int *) malloc(FRAMESIZE*sizeof(int));
for (i=0; i<order; i++)
outp[FRAMESIZE-1-i] = 0;
/*
* Move to the start of sound data
*/
fseek(ifile, soundPointer, SEEK_SET);
if (doloop && (nloops > 0)){
/*
* Set up non-blocking i/o
*/
sigaction(SIGINT, &int_act, NULL);
flags = fcntl(0, F_GETFL, 0);
flags |= O_NDELAY;
fcntl(0, F_SETFL, flags);
for (i=0; i<nloops; i++){
while(currPos<aloops[i].end){
left = aloops[i].end - currPos;
vdecodeframe(ifile, outp, order, coefTable);
writeout(stdout, (left<FRAMESIZE) ? left : FRAMESIZE, outp, outp, 1);
currPos += FRAMESIZE;
}
while (!read(0, &cc, 1)){
/*
* Go the the start point
*/
framePos = aloops[i].start/FRAMESIZE + 1;
fseek(ifile, soundPointer + framePos * FRAMEBYTES, SEEK_SET);
/*
* Restore state
*/
for (j=0; j<FRAMESIZE; j++)
outp[j] = aloops[i].state[j];
loopBegin = aloops[i].start % FRAMESIZE;
writeout(stdout, FRAMESIZE - loopBegin, outp+loopBegin, outp+loopBegin, 1);
currPos = framePos*FRAMESIZE;
while(currPos<aloops[i].end){
left = aloops[i].end - currPos;
vdecodeframe(ifile, outp, order, coefTable);
writeout(stdout, (left<FRAMESIZE) ? left : FRAMESIZE, outp, outp, 1);
currPos += FRAMESIZE;
}
}
left = FRAMESIZE - left;
if (left)
writeout(stdout, left, outp + left, outp + left, 1);
while(currPos<nSamples){
vdecodeframe(ifile, outp, order, coefTable);
left = nSamples - currPos;
writeout(stdout, (left<FRAMESIZE) ? left : FRAMESIZE, outp, outp, 1);
currPos += FRAMESIZE;
}
/*
* Switch back to blocking i/o
*/
flags = fcntl(0, F_GETFL, 0);
flags &= ~O_NDELAY;
fcntl(0, F_SETFL, flags);
}
}
else {
while(currPos<nSamples){
vdecodeframe(ifile, outp, order, coefTable);
writeout(stdout, FRAMESIZE, outp, outp, 1);
currPos += FRAMESIZE;
}
}
fclose(ifile);
}