Skip to content

Latest commit

 

History

History
385 lines (322 loc) · 10.2 KB

File metadata and controls

385 lines (322 loc) · 10.2 KB
 
Dec 31, 2011
Dec 31, 2011
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
/*
SMPEG - SDL MPEG Player Library
Copyright (C) 1999 Loki Entertainment Software
- Modified by Michel Darricau from eProcess <mdarricau@eprocess.fr> for popcorn -
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/* A class based on the MPEG stream class, used to parse and play audio */
#ifndef _MPEGAUDIO_H_
#define _MPEGAUDIO_H_
#include "SDL.h"
#include "MPEGerror.h"
#include "MPEGaction.h"
#ifdef THREADED_AUDIO
#include "MPEGring.h"
#endif
void Play_MPEGaudioSDL(void *udata, Uint8 *stream, int len);
#ifdef THREADED_AUDIO
int Decode_MPEGaudio(void *udata);
#endif
class MPEGstream;
/* MPEG/WAVE Sound library
(C) 1997 by Woo-jae Jung */
/**************************/
/* Define values for MPEG */
/**************************/
#define SCALEBLOCK 12
#define CALCBUFFERSIZE 512
#define MAXSUBBAND 32
#define MAXCHANNEL 2
#define MAXTABLE 2
#define SCALE 32768
#define MAXSCALE (SCALE-1)
#define MINSCALE (-SCALE)
#define RAWDATASIZE (2*2*32*SSLIMIT)
#define LS 0
#define RS 1
#define SSLIMIT 18
#define SBLIMIT 32
#define WINDOWSIZE 4096
// Huffmancode
#define HTN 34
/********************/
/* Type definitions */
/********************/
typedef float REAL;
typedef struct
{
bool generalflag;
unsigned int part2_3_length;
unsigned int big_values;
unsigned int global_gain;
unsigned int scalefac_compress;
unsigned int window_switching_flag;
unsigned int block_type;
unsigned int mixed_block_flag;
unsigned int table_select[3];
unsigned int subblock_gain[3];
unsigned int region0_count;
unsigned int region1_count;
unsigned int preflag;
unsigned int scalefac_scale;
unsigned int count1table_select;
}layer3grinfo;
typedef struct
{
unsigned main_data_begin;
unsigned private_bits;
struct
{
unsigned scfsi[4];
layer3grinfo gr[2];
}ch[2];
}layer3sideinfo;
typedef struct
{
int l[23]; /* [cb] */
int s[3][13]; /* [window][cb] */
}layer3scalefactor; /* [ch] */
typedef struct
{
int tablename;
unsigned int xlen,ylen;
unsigned int linbits;
unsigned int treelen;
const unsigned int (*val)[2];
}HUFFMANCODETABLE;
// Class for Mpeg layer3
class Mpegbitwindow
{
public:
Mpegbitwindow(){bitindex=point=0;};
void initialize(void) {bitindex=point=0;};
int gettotalbit(void) const {return bitindex;};
void putbyte(int c) {buffer[point&(WINDOWSIZE-1)]=c;point++;};
void wrap(void);
void rewind(int bits) {bitindex-=bits;};
void forward(int bits) {bitindex+=bits;};
int getbit(void) {
register int r=(buffer[bitindex>>3]>>(7-(bitindex&7)))&1;
bitindex++;
return r;
}
int getbits9(int bits)
{
register unsigned short a;
{ int offset=bitindex>>3;
a=(((unsigned char)buffer[offset])<<8) | ((unsigned char)buffer[offset+1]);
}
a<<=(bitindex&7);
bitindex+=bits;
return (int)((unsigned int)(a>>(16-bits)));
}
int getbits(int bits);
private:
int point,bitindex;
char buffer[2*WINDOWSIZE];
};
/* The actual MPEG audio class */
class MPEGaudio : public MPEGerror, public MPEGaudioaction {
public:
MPEGaudio(MPEGstream *stream, bool initSDL = true);
virtual ~MPEGaudio();
/* MPEG actions */
bool GetAudioInfo(MPEG_AudioInfo *info);
double Time(void);
void Play(void);
void Stop(void);
void Rewind(void);
void ResetSynchro(double time);
void Skip(float seconds);
void Volume(int vol);
/* Michel Darricau from eProcess <mdarricau@eprocess.fr> conflict name in popcorn */
MPEGstatus GetStatus(void);
/* Returns the desired SDL audio spec for this stream */
bool WantedSpec(SDL_AudioSpec *wanted);
/* Inform SMPEG of the actual audio format if configuring SDL
outside of this class */
void ActualSpec(const SDL_AudioSpec *actual);
protected:
bool sdl_audio;
MPEGstream *mpeg;
int valid_stream;
bool stereo;
double rate_in_s;
Uint32 frags_playing;
Uint32 frag_time;
#ifdef THREADED_AUDIO
bool decoding;
SDL_Thread *decode_thread;
void StartDecoding(void);
void StopDecoding(void);
#endif
/* Code from splay 1.8.2 */
/*****************************/
/* Constant tables for layer */
/*****************************/
private:
static const int bitrate[2][3][15],frequencies[2][3];
static const REAL scalefactorstable[64];
static const HUFFMANCODETABLE ht[HTN];
static const REAL filter[512];
static REAL hcos_64[16],hcos_32[8],hcos_16[4],hcos_8[2],hcos_4;
/*************************/
/* MPEG header variables */
/*************************/
private:
int last_speed;
int layer,protection,bitrateindex,padding,extendedmode;
enum _mpegversion {mpeg1,mpeg2} version;
enum _mode {fullstereo,joint,dual,single} mode;
enum _frequency {frequency44100,frequency48000,frequency32000} frequency;
/***************************************/
/* Interface for setting music quality */
/***************************************/
private:
bool forcetomonoflag;
bool forcetostereoflag;
bool swapendianflag;
int downfrequency;
public:
void setforcetomono(bool flag);
void setdownfrequency(int value);
/******************************/
/* Frame management variables */
/******************************/
private:
int decodedframe,currentframe,totalframe;
/***************************************/
/* Variables made by MPEG-Audio header */
/***************************************/
private:
int tableindex,channelbitrate;
int stereobound,subbandnumber,inputstereo,outputstereo;
REAL scalefactor;
int framesize;
/*******************/
/* Mpegtoraw class */
/*******************/
public:
void initialize();
bool run(int frames, double *timestamp = NULL);
void clearbuffer(void);
/*****************************/
/* Loading MPEG-Audio stream */
/*****************************/
private:
Uint8 _buffer[4096];
Uint32 _buffer_pos;
int bitindex;
bool fillbuffer(int size);
void sync(void);
bool issync(void);
int getbyte(void);
int getbit(void);
int getbits8(void);
int getbits9(int bits);
int getbits(int bits);
/********************/
/* Global variables */
/********************/
private:
int lastfrequency,laststereo;
// for Layer3
int layer3slots,layer3framestart,layer3part2start;
REAL prevblck[2][2][SBLIMIT][SSLIMIT];
int currentprevblock;
layer3sideinfo sideinfo;
layer3scalefactor scalefactors[2];
Mpegbitwindow bitwindow;
int wgetbit (void) {return bitwindow.getbit (); }
int wgetbits9(int bits){return bitwindow.getbits9(bits);}
int wgetbits (int bits){return bitwindow.getbits (bits);}
/*************************************/
/* Decoding functions for each layer */
/*************************************/
private:
bool loadheader(void);
//
// Subbandsynthesis
//
REAL calcbufferL[2][CALCBUFFERSIZE],calcbufferR[2][CALCBUFFERSIZE];
int currentcalcbuffer,calcbufferoffset;
void computebuffer(REAL *fraction,REAL buffer[2][CALCBUFFERSIZE]);
void generatesingle(void);
void generate(void);
void subbandsynthesis(REAL *fractionL,REAL *fractionR);
void computebuffer_2(REAL *fraction,REAL buffer[2][CALCBUFFERSIZE]);
void generatesingle_2(void);
void generate_2(void);
void subbandsynthesis_2(REAL *fractionL,REAL *fractionR);
// Extarctor
void extractlayer1(void); // MPEG-1
void extractlayer2(void);
void extractlayer3(void);
void extractlayer3_2(void); // MPEG-2
// Functions for layer 3
void layer3initialize(void);
bool layer3getsideinfo(void);
bool layer3getsideinfo_2(void);
void layer3getscalefactors(int ch,int gr);
void layer3getscalefactors_2(int ch);
void layer3huffmandecode(int ch,int gr,int out[SBLIMIT][SSLIMIT]);
REAL layer3twopow2(int scale,int preflag,int pretab_offset,int l);
REAL layer3twopow2_1(int a,int b,int c);
void layer3dequantizesample(int ch,int gr,int in[SBLIMIT][SSLIMIT],
REAL out[SBLIMIT][SSLIMIT]);
void layer3fixtostereo(int gr,REAL in[2][SBLIMIT][SSLIMIT]);
void layer3reorderandantialias(int ch,int gr,REAL in[SBLIMIT][SSLIMIT],
REAL out[SBLIMIT][SSLIMIT]);
void layer3hybrid(int ch,int gr,REAL in[SBLIMIT][SSLIMIT],
REAL out[SSLIMIT][SBLIMIT]);
void huffmandecoder_1(const HUFFMANCODETABLE *h,int *x,int *y);
void huffmandecoder_2(const HUFFMANCODETABLE *h,int *x,int *y,int *v,int *w);
/********************/
/* Playing raw data */
/********************/
private:
int samplesperframe;
int rawdatareadoffset, rawdatawriteoffset;
Sint16 *rawdata;
#ifdef THREADED_AUDIO
MPEG_ring *ring;
#else
Sint16 spillover[ RAWDATASIZE ];
#endif
int volume;
void clearrawdata(void) {
rawdatareadoffset=0;
rawdatawriteoffset=0;
rawdata=NULL;
}
void putraw(short int pcm) {rawdata[rawdatawriteoffset++]=pcm;}
/********************/
/* Timestamp sync */
/********************/
public:
#define N_TIMESTAMPS 5
double timestamp[N_TIMESTAMPS];
/* Functions which access MPEGaudio internals */
friend void Play_MPEGaudioSDL(void *udata, Uint8 *stream, int len);
friend int Play_MPEGaudio(MPEGaudio *audio, Uint8 *stream, int len);
#ifdef THREADED_AUDIO
friend int Decode_MPEGaudio(void *udata);
#endif
};
/* Need to duplicate the prototypes, this is not a typo :) */
void Play_MPEGaudioSDL(void *udata, Uint8 *stream, int len);
int Play_MPEGaudio(MPEGaudio *audio, Uint8 *stream, int len);
#ifdef THREADED_AUDIO
int Decode_MPEGaudio(void *udata);
#endif
#endif /* _MPEGAUDIO_H_ */