forked from DanielGibson/amp11lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
amp11lib.cpp
471 lines (405 loc) · 11.9 KB
/
amp11lib.cpp
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
// amp11lib - library interface layer and asynchronious player
// Copyright (c) 1999-2000 Alen Ladavac
// See COPYING (GNU Library General Public License 2) for license
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "binfile/binfstd.h"
#include "binfile/binfarc.h"
#include "ampdec.h"
#include "amp11lib.h"
// ------------------------------------
// type of stream indentifier for each stream handle
enum StreamType {
ST_INVALID=0,
ST_BEFOREVALID, // low marker for valid values
ST_UNUSED,
ST_INPUT,
ST_DECODER,
ST_SUBFILE,
ST_AFTERVALID, // high marker for valid values
};
#define SLAVESPERSTREAM 2
#define SLAVE_REDIR 0 // base slave used for redirection
#define SLAVE_DECODERSOURCE 1 // mpeg decoder stream reads mpeg from another file
struct Stream {
ALsint32 st_nReferences; // reference counter
StreamType st_stType; // type of the stream, or unused
ALhandle st_ahSlaves[SLAVESPERSTREAM]; // slave streams referenced by this one
float st_fBytesPerSec; // bytes per second for decoder streams (used for seeking)
union { // holds pointer to the actual stream, NULL if unused
binfile *st_binfile;
sbinfile *st_sbinfile;
ampegdecoder *st_ampegdecoder;
abinfile *st_abinfile;
};
};
// stream structures are stored here
// stream number 0 is never used, so that handle 0 is used for errors
#define MAX_STREAMS 64
static struct Stream _astStreams[MAX_STREAMS];
// set between calls to alInitSystem() and alEndSystem()
static ALbool _bLibraryInitialized = ALfalse;
//------------------------------------
// check if a stream handle is valid
static ALbool IsStreamHandleValid(ALhandle hStream)
{
// if library is not initialized
if (!_bLibraryInitialized) {
// no streams are valid
assert(ALfalse);
return ALfalse;
}
// if handle is out of range
if (hStream<=0 || hStream>MAX_STREAMS) {
assert(ALfalse);
return ALfalse;
}
Stream &st = _astStreams[hStream];
// if handle is not used or invalid type
if (st.st_stType==ST_UNUSED||st.st_stType==ST_INVALID) {
assert(ALfalse);
return ALfalse;
}
// if bad pointer (paranoia check - this should not be external error)
if (st.st_binfile==NULL) {
assert(ALfalse);
return ALfalse;
}
// reference count should be valid (paranoia check)
if (st.st_nReferences<=0) {
assert(ALfalse);
return ALfalse;
}
return ALtrue;
}
// find an unused stream handle
static ALhandle FindFreeHandle(void)
{
// for each stream (note that stream 0 is not used!)
for(int ist=1; ist<MAX_STREAMS; ist++) {
assert(
_astStreams[ist].st_stType>ST_BEFOREVALID &&
_astStreams[ist].st_stType<ST_AFTERVALID);
// if unused
if (_astStreams[ist].st_stType == ST_UNUSED) {
// use that one
return ist;
}
}
// if not found, error
return 0;
}
// add one reference to a stream
static ALbool AddStreamReference(ALhandle hStream)
{
// if handle is invalid
if (!IsStreamHandleValid(hStream)) {
// it should not happen
assert(ALfalse);
// do nothing
return ALfalse;
}
// this just increases the reference count
_astStreams[hStream].st_nReferences++;
return ALtrue;
}
// remove one reference from a stream - if no references, stream is closed
static void RemStreamReference(ALhandle hStream)
{
// if handle is invalid
if (!IsStreamHandleValid(hStream)) {
// it should not happen
assert(ALfalse);
// do nothing
return;
}
Stream &st = _astStreams[hStream];
// decrease the reference count
st.st_nReferences--;
// if the stream is not used anymore
if (st.st_nReferences<=0) {
// delete the stream's binfile
delete _astStreams[hStream].st_binfile;
// mark as unused
_astStreams[hStream].st_binfile = NULL;
_astStreams[hStream].st_stType = ST_UNUSED;
// for each possible slave stream
for (int iSlave=0; iSlave<SLAVESPERSTREAM; iSlave++) {
// if there is a slave
if (_astStreams[hStream].st_ahSlaves[iSlave]!=0) {
// remove reference from the slave stream
RemStreamReference(_astStreams[hStream].st_ahSlaves[iSlave]);
_astStreams[hStream].st_ahSlaves[iSlave] = 0;
}
}
}
}
// add a slave stream to one stream
ALbool SetSlaveStream(ALhandle hStream, ALhandle hSlave, ALsint32 iSlave)
{
assert(iSlave>=0 && iSlave<SLAVESPERSTREAM);
// if handle is invalid
if (!IsStreamHandleValid(hStream)) {
// it should not happen
assert(ALfalse);
// do nothing
return ALfalse;
}
Stream &st = _astStreams[hStream];
// if already has that slave
if (st.st_ahSlaves[iSlave]!=0) {
// remove that reference
RemStreamReference(st.st_ahSlaves[iSlave]);
st.st_ahSlaves[iSlave] = 0;
}
// if setting slave to nothing
if (hSlave==0) {
// that's all
return ALtrue;
}
// if handle is invalid
if (!IsStreamHandleValid(hSlave)) {
// it should not happen
assert(ALfalse);
// do nothing
return ALfalse;
}
// add one reference to the slave
if (!AddStreamReference(hSlave)) {
return ALfalse;
}
// remember slave
st.st_ahSlaves[iSlave] = hSlave;
return ALtrue;
}
// ------------------------------------
// actual interface functions
// open a standard file with given filename for reading
AMP11LIB_API ALhandle WINAPI alOpenInputFile(const char *strFileName)
{
// get new stream
ALhandle hStream = FindFreeHandle();
// if none available
if (hStream==0) {
// report error
return 0;
}
Stream &st = _astStreams[hStream];
// initialize it as binary input file
st.st_stType = ST_INPUT;
st.st_nReferences = 1;
st.st_sbinfile = new sbinfile;
// if cannot open it
if (st.st_sbinfile->open(strFileName, sbinfile::openro) < 0) {
// close it
alClose(hStream);
// report error
return 0;
}
// if all fine, return the handle
return hStream;
}
// open mpeg decoder that reads from a file of given handle
AMP11LIB_API ALhandle WINAPI alOpenDecoder(ALhandle hFile)
{
// if the given file handle is invalid
if (!IsStreamHandleValid(hFile)) {
// report error
return 0;
}
// get the source stream
Stream &stFile = _astStreams[hFile];
// if it is not valid for input
if (stFile.st_stType!=ST_INPUT && stFile.st_stType!=ST_SUBFILE) {
// report error
return 0;
}
// get new stream
ALhandle hStream = FindFreeHandle();
// if none available
if (hStream==0) {
// report error
return 0;
}
Stream &st = _astStreams[hStream];
// initialize it as decoder
st.st_stType = ST_DECODER;
st.st_nReferences = 1;
st.st_ampegdecoder = new ampegdecoder;
// if cannot reference the source stream
if (!SetSlaveStream(hStream, hFile, SLAVE_DECODERSOURCE)) {
// close it
alClose(hStream);
// report error
return 0;
}
// if cannot open it
int freq,stereo;
if (st.st_ampegdecoder->open(*stFile.st_binfile, freq, stereo, 1, 0, 2) < 0) {
// close it
alClose(hStream);
// report error
return 0;
}
// calculate stream bandwidth
st.st_fBytesPerSec = (stereo?4:2)*freq;
// if all fine, return the handle
return hStream;
}
// open a sub-file inside an archive file
AMP11LIB_API ALhandle WINAPI alOpenSubFile(ALhandle hFile, ALsize sOffset, ALsize sSize)
{
// if the given file handle is invalid
if (!IsStreamHandleValid(hFile)) {
// report error
return 0;
}
// get the source stream
Stream &stFile = _astStreams[hFile];
// if it is not valid for input
if (stFile.st_stType!=ST_INPUT && stFile.st_stType!=ST_SUBFILE) {
// report error
return 0;
}
// get new stream
ALhandle hStream = FindFreeHandle();
// if none available
if (hStream==0) {
// report error
return 0;
}
Stream &st = _astStreams[hStream];
// initialize it as subfile
st.st_stType = ST_SUBFILE;
st.st_nReferences = 1;
st.st_abinfile = new abinfile;
// if cannot reference the source stream
if (!SetSlaveStream(hStream, hFile, SLAVE_DECODERSOURCE)) {
// close it
alClose(hStream);
// report error
return 0;
}
// if cannot open it
if (st.st_abinfile->open(*stFile.st_binfile, sOffset, sSize) < 0) {
// close it
alClose(hStream);
// report error
return 0;
}
// if all fine, return the handle
return hStream;
}
// get file header of mpx file from a file of given handle
AMP11LIB_API ALbool WINAPI alGetMPXHeader(ALhandle hFile, ALsint32 *piLayer,
ALsint32 *piVersion, ALsint32 *piFrequency, ALbool *pbStereo, ALsint32 *piRate)
{
// if the given file handle is invalid
if (!IsStreamHandleValid(hFile)) {
// report error
return 0;
}
// get the source stream
Stream &stFile = _astStreams[hFile];
// if it is not valid for input
if (stFile.st_stType!=ST_INPUT && stFile.st_stType!=ST_SUBFILE) {
// report error
return 0;
}
// get the header
return ampegdecoder::getheader(*stFile.st_binfile,
*piLayer, *piVersion, *piFrequency, *pbStereo, *piRate);
}
// close any open amp11 stream
AMP11LIB_API void WINAPI alClose(ALhandle hStream)
{
// just remove one reference from it - it will free it if not used anymore
RemStreamReference(hStream);
}
// read a chunk of bytes from given stream
AMP11LIB_API ALsize WINAPI alRead(ALhandle hStream, void *pvBuffer, ALsize size)
{
// if handle is invalid
if (!IsStreamHandleValid(hStream)) {
// do nothing
return 0;
}
return _astStreams[hStream].st_binfile->read(pvBuffer, size);
}
AMP11LIB_API void WINAPI alDecSeekAbs(ALhandle hDecoder, ALfloat fSeconds)
{
// if handle is invalid
if (!IsStreamHandleValid(hDecoder)) {
// do nothing
return;
}
Stream &st = _astStreams[hDecoder];
// if not a decoder
if (st.st_stType != ST_DECODER) {
// do nothing
return;
}
// set position
st.st_ampegdecoder->seek(int(st.st_fBytesPerSec*fSeconds));
}
AMP11LIB_API ALfloat WINAPI alDecGetLen(ALhandle hDecoder)
{
// if handle is invalid
if (!IsStreamHandleValid(hDecoder)) {
// do nothing
return 0.0f;
}
Stream &st = _astStreams[hDecoder];
// if not a decoder
if (st.st_stType != ST_DECODER) {
// do nothing
return 0.0f;
}
// read length
return st.st_ampegdecoder->length()/st.st_fBytesPerSec;
}
//----------------------------------------------
// initialize amp11lib before calling any of its functions
AMP11LIB_API void WINAPI alInitLibrary(void)
{
// if already initialized
if (_bLibraryInitialized) {
// do nothing
return;
}
// set all streams to unused
for(int ist=0; ist<MAX_STREAMS; ist++) {
_astStreams[ist].st_stType = ST_UNUSED;
_astStreams[ist].st_nReferences = 0;
_astStreams[ist].st_binfile = NULL;
for (int iSlave=0; iSlave<SLAVESPERSTREAM; iSlave++) {
_astStreams[ist].st_ahSlaves[iSlave] = 0;
}
}
// mark as initialized
_bLibraryInitialized = ALtrue;
}
// cleanup amp11lib when not needed anymore
// NOTE: do not try to call this using atexit(). it just won't work
// because all threads are force-killed _before_ any at-exit function
// is called. sigh.
AMP11LIB_API void WINAPI alEndLibrary(void)
{
// if not initialized
if (!_bLibraryInitialized) {
// do nothing
return;
}
// disable possible redirection if on
//alDisableRedirection(); // FIXME DG: I had to compile this out, is it useful?
// free all streams that are not freed
for(int ist=0; ist<MAX_STREAMS; ist++) {
if (_astStreams[ist].st_stType != ST_UNUSED) {
alClose(ist);
}
}
// check that reference counting did its job fine
// mark as not initialized
_bLibraryInitialized = ALfalse;
}