Debian Codegraph @

plugins/libmpeg_audiodec/mpegsound.h

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /* MPEG/WAVE Sound library
2
3 (C) 1997 by Woo-jae Jung */
4
5 // Mpegsound.h
6 // This is typeset for functions in MPEG/WAVE Sound library.
7 // Now, it's for only linux-pc-?86
8
9 /************************************/
10 /* Include default library packages */
11 /************************************/
12
13 #ifndef _L__SOUND__
14 #define _L__SOUND__
15
16 #include "audiodecoder.h"
17 #include <stdio.h>
18 #include <sys/types.h>
19
20 AVM_BEGIN_NAMESPACE;
21
22 /****************/
23 /* Sound Errors */
24 /****************/
25 // General error
26 #define SOUND_ERROR_OK 0
27 #define SOUND_ERROR_FINISH -1
28
29 // Device error (for player)
30 #define SOUND_ERROR_DEVOPENFAIL 1
31 #define SOUND_ERROR_DEVBUSY 2
32 #define SOUND_ERROR_DEVBADBUFFERSIZE 3
33 #define SOUND_ERROR_DEVCTRLERROR 4
34
35 // Sound file (for reader)
36 #define SOUND_ERROR_FILEOPENFAIL 5
37 #define SOUND_ERROR_FILEREADFAIL 6
38
39 // Network
40 #define SOUND_ERROR_UNKNOWNPROXY 7
41 #define SOUND_ERROR_UNKNOWNHOST 8
42 #define SOUND_ERROR_SOCKET 9
43 #define SOUND_ERROR_CONNECT 10
44 #define SOUND_ERROR_FDOPEN 11
45 #define SOUND_ERROR_HTTPFAIL 12
46 #define SOUND_ERROR_HTTPWRITEFAIL 13
47 #define SOUND_ERROR_TOOMANYRELOC 14
48
49 // Miscellneous (for translater)
50 #define SOUND_ERROR_MEMORYNOTENOUGH 15
51 #define SOUND_ERROR_EOF 16
52 #define SOUND_ERROR_BAD 17
53
54 #define SOUND_ERROR_THREADFAIL 18
55
56 #define SOUND_ERROR_UNKNOWN 19
57
58
59 /**************************/
60 /* Define values for MPEG */
61 /**************************/
62 #define SCALEBLOCK 12
63 #define CALCBUFFERSIZE 512
64 #define MAXSUBBAND 32
65 #define MAXCHANNEL 2
66 #define MAXTABLE 2
67 #define SCALE 32768
68 #define MAXSCALE (SCALE-1)
69 #define MINSCALE (-SCALE)
70 #define RAWDATASIZE (2*2*32*SSLIMIT)
71
72 enum { LS = 0, RS = 1 };
73
74 #define SSLIMIT 18
75 #define SBLIMIT 32
76
77 #define WINDOWSIZE 4096
78
79 // Huffmancode
80 #define HTN 34
81
82
83 /*******************************************/
84 /* Define values for Microsoft WAVE format */
85 /*******************************************/
86 #define RIFF 0x46464952
87 #define WAVE 0x45564157
88 #define FMT 0x20746D66
89 #define DATA 0x61746164
90 #define PCM_CODE 1
91 #define WAVE_MONO 1
92 #define WAVE_STEREO 2
93
94 #define MODE_MONO 0
95 #define MODE_STEREO 1
96
97 /********************/
98 /* Type definitions */
99 /********************/
100 typedef float REAL;
101 #if defined(__sun__) && defined(__svr4__)
102 typedef unsigned int u_int32_t;
103 typedef unsigned short u_int16_t;
104 #endif
105
106 typedef struct _waveheader {
107 u_int32_t main_chunk; // 'RIFF'
108 u_int32_t length; // filelen
109 u_int32_t chunk_type; // 'WAVE'
110
111 u_int32_t sub_chunk; // 'fmt '
112 u_int32_t sc_len; // length of sub_chunk, =16
113 u_int16_t format; // should be 1 for PCM-code
114 u_int16_t modus; // 1 Mono, 2 Stereo
115 u_int32_t sample_fq; // frequence of sample
116 u_int32_t byte_p_sec;
117 u_int16_t byte_p_spl; // samplesize; 1 or 2 bytes
118 u_int16_t bit_p_spl; // 8, 12 or 16 bit
119
120 u_int32_t data_chunk; // 'data'
121 u_int32_t data_length; // samplecount
122 }WAVEHEADER;
123
124 typedef struct
125 {
126 bool generalflag;
127 unsigned int part2_3_length;
128 unsigned int big_values;
129 unsigned int global_gain;
130 unsigned int scalefac_compress;
131 unsigned int window_switching_flag;
132 unsigned int block_type;
133 unsigned int mixed_block_flag;
134 unsigned int table_select[3];
135 unsigned int subblock_gain[3];
136 unsigned int region0_count;
137 unsigned int region1_count;
138 unsigned int preflag;
139 unsigned int scalefac_scale;
140 unsigned int count1table_select;
141 }layer3grinfo;
142
143 typedef struct
144 {
145 unsigned main_data_begin;
146 unsigned private_bits;
147 struct
148 {
149 unsigned scfsi[4];
150 layer3grinfo gr[2];
151 }ch[2];
152 }layer3sideinfo;
153
154 typedef struct
155 {
156 int l[23]; /* [cb] */
157 int s[3][13]; /* [window][cb] */
158 }layer3scalefactor; /* [ch] */
159
160 typedef struct
161 {
162 int tablename;
163 unsigned int xlen,ylen;
164 unsigned int linbits;
165 unsigned int treelen;
166 const unsigned int (*val)[2];
167 }HUFFMANCODETABLE;
168
169 /*********************************/
170 /* Sound input interface classes */
171 /*********************************/
172 // Superclass for Inputbitstream // Yet, Temporary
173
174
175
176
177 // Class for Mpeg layer3
178 class Mpegbitwindow
179 {
180 public:
181 Mpegbitwindow(){bitindex=point=0;};
182
183 void initialize(void) {bitindex=point=0;};
184 int gettotalbit(void) const {return bitindex;};
185 void putbyte(int c) {buffer[point&(WINDOWSIZE-1)]=c;point++;};
186 void wrap(void) {
187 int p=bitindex>>3;
188 point&=(WINDOWSIZE-1);
189
190 if(p>=point)
191 {
192 for(register int i=4;i<point;i++)
193 buffer[WINDOWSIZE+i]=buffer[i];
194 }
195 *((int *)(buffer+WINDOWSIZE))=*((int *)buffer);
196 }
197 void rewind(int bits) {bitindex-=bits;};
198 void forward(int bits) {bitindex+=bits;};
199 int getbits(int bits);
200
201 int getbit(void)
202 {
203 // register int r=(buffer[(bitindex>>3)&(WINDOWSIZE-1)]>>(7-(bitindex&7)))&1;
204 register int r=(buffer[bitindex>>3]>>(7-(bitindex&7)))&1;
205 bitindex++;
206 return r;
207 }
208 int getbits9(int bits)
209 {
210 register unsigned short a;
211
212 //#ifndef WORDS_BIGENDIAN
213 {
214 // int offset=(bitindex>>3)&(WINDOWSIZE-1);
215 int offset=bitindex>>3;
216
217 a = (unsigned char)buffer[offset] << 8;
218 a |= (unsigned char)buffer[offset+1];
219 }
220 //#else
221 //wrong a=((unsigned short *)(buffer+((bixindex>>3)&(WINDOWSIZE-1))));
222 // a=*((unsigned short *)(buffer+((bitindex>>3))));
223 //#endif
224
225 a<<=(bitindex&7);
226 bitindex+=bits;
227 return (int)((unsigned int)(a>>(16-bits)));
228 }
229
230
231
232 private:
233 int point,bitindex;
234 char buffer[2*WINDOWSIZE];
235 };
236 // Class for converting mpeg format to raw format
237 class Mpegtoraw : public IAudioDecoder, public IRtConfig
238 {
239 private:
240 /*********************************/
241 /* this was added for AVI player */
242 /*********************************/
243 const unsigned char* local_src_buffer;
244 int local_src_buffer_size;
245 unsigned char* local_dest_buffer;
246 int local_dest_buffer_size;
247 int gain_add;
248 bool flushed;
249 public:
250 /* returns number of bytes written to output_buffer */
251 /* input_size contains number of bytes used in input_buffer */
252 /* if format!=0, writes info about audio stream into it */
253 /* output_buffer MUST be large enough !!! */
254 int Convert(const void* in_data, size_t in_size,
255 void* out_data, size_t out_size,
256 size_t* size_read, size_t* size_written);
257 void Flush() { flushed = true; }
258 IRtConfig* GetRtConfig() { return this; }
259 const avm::vector<AttributeInfo>& GetAttrs() const;
260 int GetValue(const char* name, int* value) const;
261 int SetValue(const char* name, int value);
262
263 // this mpeg lib seems to have problems whitout this
264 //uint_t GetMinSize() const { return IAudioDecoder::GetMinSize() * 2; }
265
266 /*****************************/
267 /* Constant tables for layer */
268 /*****************************/
269
270 private:
271 static const int bitrate[2][3][15],frequencies[3][3];
272 static const REAL scalefactorstable[64];
273 static const HUFFMANCODETABLE ht[HTN];
274 static const REAL filter[512];
275 static REAL hcos_64[16],hcos_32[8],hcos_16[4],hcos_8[2],hcos_4;
276
277 /*************************/
278 /* MPEG header variables */
279 /*************************/
280 private:
281 int layer,protection,bitrateindex,padding,extendedmode;
282 enum _mpegversion {mpeg1,mpeg2} version;
283 enum _mode {fullstereo,joint,dual,single} mode;
284 enum _frequency {frequency44100,frequency48000,frequency32000} frequency;
285
286 /*******************************************/
287 /* Functions getting MPEG header variables */
288 /*******************************************/
289 public:
290 // General
291 int getversion(void) const {return version;};
292 int getlayer(void) const {return layer;};
293 bool getcrccheck(void) const {return (!protection);};
294 // Stereo or not
295 int getmode(void) const {return mode;};
296 bool isstereo(void) const {return (getmode()!=single);};
297 // Frequency and bitrate
298 int getfrequency(void) const {return frequencies[version][frequency];};
299 int getbitrate(void) const {return bitrate[version][layer-1][bitrateindex];};
300
301 /***************************************/
302 /* Interface for setting music quality */
303 /***************************************/
304 private:
305 bool forcetomonoflag;
306 int downfrequency;
307
308 public:
309 void setforcetomono(bool flag);
310 void setdownfrequency(int value);
311
312 /******************************************/
313 /* Functions getting other MPEG variables */
314 /******************************************/
315 public:
316 bool getforcetomono(void);
317 int getdownfrequency(void);
318 int getpcmperframe(void);
319
320 /******************************/
321 /* Frame management variables */
322 /******************************/
323 private:
324 int currentframe,totalframe;
325 int decodeframe;
326 // int *frameoffsets;
327
328 /******************************/
329 /* Frame management functions */
330 /******************************/
331 public:
332 int getcurrentframe(void) const {return currentframe;};
333 int gettotalframe(void) const {return totalframe;};
334 void setframe(int framenumber);
335
336 /***************************************/
337 /* Variables made by MPEG-Audio header */
338 /***************************************/
339 private:
340 int tableindex,channelbitrate;
341 int stereobound,subbandnumber,inputstereo,outputstereo;
342 REAL scalefactor;
343 int framesize;
344 bool mpeg25;
345
346 /********************/
347 /* Song information */
348 /********************/
349 /*
350 private:
351 struct
352 {
353 char name [30+1];
354 char musican[30+1];
355 char type [30+1];
356 char year [ 7+1];
357 char etc [25+1];
358 }song;
359 */
360 /* Song information functions */
361 /*
362 const char *getsongname(void) const { return (const char *)song.name; };
363 const char *getmusican (void) const { return (const char *)song.musican;};
364 const char *getsongtype(void) const { return (const char *)song.type; };
365 const char *getsongyear(void) const { return (const char *)song.year; };
366 const char *getsongetc (void) const { return (const char *)song.etc; };
367 */
368 /*******************/
369 /* Mpegtoraw class */
370 /*******************/
371 public:
372 Mpegtoraw(const CodecInfo&, const WAVEFORMATEX*);
373 ~Mpegtoraw();
374 bool run(int frames);
375 int geterrorcode(void) {return __errorcode;};
376 void clearbuffer(void);
377
378 private:
379 int __errorcode;
380 bool seterrorcode(int errorno){__errorcode=errorno;return false;};
381
382 /*****************************/
383 /* Loading MPEG-Audio stream */
384 /*****************************/
385 private:
386 // Soundinputstream *loader; // Interface
387 union
388 {
389 unsigned char store[4];
390 unsigned int current;
391 }u;
392 // char buffer[4096];
393 const unsigned char* buffer;
394 int bitindex;
395 bool fillbuffer(unsigned size)
396 {
397 bitindex=0;
398 if(local_src_buffer_size<(int)size)
399 return false;
400 else
401 {
402 buffer=local_src_buffer;
403 local_src_buffer+=size;
404 local_src_buffer_size-=size;
405 return true;
406 }
407 };
408 void unwind(int z)
409 {
410 local_src_buffer_size+=z;
411 local_src_buffer-=z;
412 }
413 void sync(void) {bitindex=(bitindex+7)&0xFFFFFFF8;};
414 bool issync(void){return (bitindex&7);};
415 int getbits(int bits);
416 int getbyte(void)
417 {
418 int r=(unsigned char)buffer[bitindex>>3];
419
420 bitindex+=8;
421 return r;
422 }
423 int getbits9(int bits)
424 {
425 register unsigned short a;
426 //#ifndef WORDS_BIGENDIAN
427 {
428 int offset=bitindex>>3;
429
430 a = (unsigned char)buffer[offset] << 8;
431 a |= (unsigned char)buffer[offset+1];
432 }
433 //#else
434 // a=*((unsigned short *)(buffer+((bitindex>>3))));
435 //#endif
436
437 a<<=(bitindex&7);
438 bitindex+=bits;
439 return (int)((unsigned int)(a>>(16-bits)));
440 }
441 int getbits8(void)
442 {
443 register unsigned short a;
444
445 //#ifndef WORDS_BIGENDIAN
446 {
447 int offset=bitindex>>3;
448
449 a = (unsigned char)buffer[offset] << 8;
450 a |= (unsigned char)buffer[offset+1];
451 }
452 //#else
453 // a=*((unsigned short *)(buffer+((bitindex>>3))));
454 //#endif
455
456 a<<=(bitindex&7);
457 bitindex+=8;
458 return (int)((unsigned int)(a>>8));
459 }
460 int getbit(void)
461 {
462 register int r=(buffer[bitindex>>3]>>(7-(bitindex&7)))&1;
463
464 bitindex++;
465 return r;
466 }
467
468 /********************/
469 /* Global variables */
470 /********************/
471 private:
472 int lastfrequency,laststereo;
473
474 // for Layer3
475 int layer3slots,layer3framestart,layer3part2start;
476 REAL prevblck[2][2][SBLIMIT][SSLIMIT];
477 int currentprevblock;
478 layer3sideinfo sideinfo;
479 layer3scalefactor scalefactors[2];
480 //#define MPEGNOINSTRUMENTFUNCTION __attribute__ ((no_instrument_function))
481 #define MPEGNOINSTRUMENTFUNCTION
482 Mpegbitwindow bitwindow;
483
484 int wgetbit(void) MPEGNOINSTRUMENTFUNCTION { return bitwindow.getbit(); }
485 int wgetbits9(int bits) MPEGNOINSTRUMENTFUNCTION { return bitwindow.getbits9(bits); }
486 int wgetbits (int bits) MPEGNOINSTRUMENTFUNCTION { return bitwindow.getbits (bits); }
487
488 /*************************************/
489 /* Decoding functions for each layer */
490 /*************************************/
491 private:
492 bool loadheader(void);
493
494 //
495 // Subbandsynthesis
496 //
497 REAL calcbufferL[2][CALCBUFFERSIZE],calcbufferR[2][CALCBUFFERSIZE];
498 int currentcalcbuffer,calcbufferoffset;
499
500 void computebuffer(REAL *fraction,REAL buffer[2][CALCBUFFERSIZE]);
501 void generatesingle(void);
502 void generate(void) MPEGNOINSTRUMENTFUNCTION;
503 #undef MPEGNOINSTRUMENTFUNCTION
504 void subbandsynthesis(REAL *fractionL,REAL *fractionR);
505
506 void computebuffer_2(REAL *fraction,REAL buffer[2][CALCBUFFERSIZE]);
507 void generatesingle_2(void);
508 void generate_2(void);
509 void subbandsynthesis_2(REAL *fractionL,REAL *fractionR);
510
511 // Extractor
512 void extractlayer1(void); // MPEG-1
513 void extractlayer2(void);
514 void extractlayer3(void);
515 void extractlayer3_2(void); // MPEG-2
516
517
518 // Functions for layer 3
519 void layer3initialize(void);
520 bool layer3getsideinfo(void);
521 bool layer3getsideinfo_2(void);
522 void layer3getscalefactors(int ch,int gr);
523 void layer3getscalefactors_2(int ch);
524 void layer3huffmandecode(int ch,int gr,int out[SBLIMIT][SSLIMIT]);
525 void layer3dequantizesample(int ch,int gr,int in[SBLIMIT][SSLIMIT],
526 REAL out[SBLIMIT][SSLIMIT]);
527 void layer3fixtostereo(int gr,REAL in[2][SBLIMIT][SSLIMIT]);
528 void layer3reorderandantialias(int ch,int gr,REAL in[SBLIMIT][SSLIMIT],
529 REAL out[SBLIMIT][SSLIMIT]);
530
531 void layer3hybrid(int ch,int gr,REAL in[SBLIMIT][SSLIMIT],
532 REAL out[SSLIMIT][SBLIMIT]);
533
534 void huffmandecoder_1(const HUFFMANCODETABLE *h,int *x,int *y);
535 void huffmandecoder_2(const HUFFMANCODETABLE *h,int *x,int *y,int *v,int *w);
536
537 /********************/
538 /* Playing raw data */
539 /********************/
540 private:
541 int getbytedirect()
542 {
543 local_src_buffer_size--;
544 return (local_src_buffer_size>=0)?(*local_src_buffer++):(-1);
545 }
546 int eof() { return (local_src_buffer_size<0); }
547
548 void clearrawdata(void) { rawdataoffset=0; }
549 void putraw(short int pcm) { rawdata[rawdataoffset++]=pcm; }
550 int flushrawdata(void);
551
552 int rawdataoffset;
553 short int rawdata[RAWDATASIZE];
554 };
555
556 AVM_END_NAMESPACE;
557
558 #endif // _L__SOUND__