298
|
1 #include <ctype.h>
|
|
2 #include <stdlib.h>
|
|
3 #include <signal.h>
|
|
4
|
|
5 #include "SDL_sound.h"
|
|
6
|
|
7 #define __SDL_SOUND_INTERNAL__
|
|
8 #include "SDL_sound_internal.h"
|
|
9
|
|
10 #include "mpg123_sdlsound.h"
|
|
11
|
|
12 struct parameter param = { 1 , 1 , 0 , 0 };
|
|
13
|
|
14 int tabsel_123[2][3][16] = {
|
|
15 { {0,32,64,96,128,160,192,224,256,288,320,352,384,416,448,},
|
|
16 {0,32,48,56, 64, 80, 96,112,128,160,192,224,256,320,384,},
|
|
17 {0,32,40,48, 56, 64, 80, 96,112,128,160,192,224,256,320,} },
|
|
18
|
|
19 { {0,32,48,56,64,80,96,112,128,144,160,176,192,224,256,},
|
|
20 {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,},
|
|
21 {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,} }
|
|
22 };
|
|
23
|
|
24 long mpglib_freqs[9] = { 44100, 48000, 32000,
|
|
25 22050, 24000, 16000 ,
|
|
26 11025 , 12000 , 8000 };
|
|
27
|
|
28 int bitindex;
|
|
29 unsigned char *wordpointer;
|
|
30 unsigned char *pcm_sample;
|
|
31 int pcm_point = 0;
|
|
32
|
|
33
|
|
34 #define HDRCMPMASK 0xfffffd00
|
|
35
|
|
36 #if 0
|
|
37 int head_check(unsigned long head)
|
|
38 {
|
|
39 if( (head & 0xffe00000) != 0xffe00000)
|
|
40 return FALSE;
|
|
41 if(!((head>>17)&3))
|
|
42 return FALSE;
|
|
43 if( ((head>>12)&0xf) == 0xf)
|
|
44 return FALSE;
|
|
45 if( ((head>>10)&0x3) == 0x3 )
|
|
46 return FALSE;
|
|
47 return TRUE;
|
|
48 }
|
|
49 #endif
|
|
50
|
|
51
|
|
52 /*
|
|
53 * the code a header and write the information
|
|
54 * into the frame structure
|
|
55 */
|
|
56 int decode_header(struct frame *fr,unsigned long newhead)
|
|
57 {
|
|
58 if( newhead & (1<<20) ) {
|
|
59 fr->lsf = (newhead & (1<<19)) ? 0x0 : 0x1;
|
|
60 fr->mpeg25 = 0;
|
|
61 }
|
|
62 else {
|
|
63 fr->lsf = 1;
|
|
64 fr->mpeg25 = 1;
|
|
65 }
|
|
66
|
|
67 fr->lay = 4-((newhead>>17)&3);
|
|
68 if( ((newhead>>10)&0x3) == 0x3) {
|
|
69 Sound_SetError("MPGLIB: Corrupted header");
|
|
70 return 0;
|
|
71 }
|
|
72 if(fr->mpeg25) {
|
|
73 fr->sampling_frequency = 6 + ((newhead>>10)&0x3);
|
|
74 }
|
|
75 else
|
|
76 fr->sampling_frequency = ((newhead>>10)&0x3) + (fr->lsf*3);
|
|
77 fr->error_protection = ((newhead>>16)&0x1)^0x1;
|
|
78
|
|
79 if(fr->mpeg25) /* allow Bitrate change for 2.5 ... */
|
|
80 fr->bitrate_index = ((newhead>>12)&0xf);
|
|
81
|
|
82 fr->bitrate_index = ((newhead>>12)&0xf);
|
|
83 fr->padding = ((newhead>>9)&0x1);
|
|
84 fr->extension = ((newhead>>8)&0x1);
|
|
85 fr->mode = ((newhead>>6)&0x3);
|
|
86 fr->mode_ext = ((newhead>>4)&0x3);
|
|
87 fr->copyright = ((newhead>>3)&0x1);
|
|
88 fr->original = ((newhead>>2)&0x1);
|
|
89 fr->emphasis = newhead & 0x3;
|
|
90
|
|
91 fr->stereo = (fr->mode == MPG_MD_MONO) ? 1 : 2;
|
|
92
|
|
93 if(!fr->bitrate_index)
|
|
94 {
|
|
95 Sound_SetError("MPGLIB: Free format not supported.");
|
|
96 return (0);
|
|
97 }
|
|
98
|
|
99 switch(fr->lay)
|
|
100 {
|
|
101 case 1:
|
|
102 #ifdef LAYER1
|
|
103 #if 0
|
|
104 fr->jsbound = (fr->mode == MPG_MD_JOINT_STEREO) ?
|
|
105 (fr->mode_ext<<2)+4 : 32;
|
|
106 #endif
|
|
107 fr->framesize = (long) tabsel_123[fr->lsf][0][fr->bitrate_index] * 12000;
|
|
108 fr->framesize /= mpglib_freqs[fr->sampling_frequency];
|
|
109 fr->framesize = ((fr->framesize+fr->padding)<<2)-4;
|
|
110 #else
|
|
111 Sound_SetError("MPGLIB: Not supported!");
|
|
112 #endif
|
|
113 break;
|
|
114 case 2:
|
|
115 #ifdef LAYER2
|
|
116 #if 0
|
|
117 fr->jsbound = (fr->mode == MPG_MD_JOINT_STEREO) ?
|
|
118 (fr->mode_ext<<2)+4 : fr->II_sblimit;
|
|
119 #endif
|
|
120 fr->framesize = (long) tabsel_123[fr->lsf][1][fr->bitrate_index] * 144000;
|
|
121 fr->framesize /= mpglib_freqs[fr->sampling_frequency];
|
|
122 fr->framesize += fr->padding - 4;
|
|
123 #else
|
|
124 Sound_SetError("MPGLIB: Not supported!");
|
|
125 #endif
|
|
126 break;
|
|
127 case 3:
|
|
128 #if 0
|
|
129 fr->do_layer = do_layer3;
|
|
130 if(fr->lsf)
|
|
131 ssize = (fr->stereo == 1) ? 9 : 17;
|
|
132 else
|
|
133 ssize = (fr->stereo == 1) ? 17 : 32;
|
|
134 #endif
|
|
135
|
|
136 #if 0
|
|
137 if(fr->error_protection)
|
|
138 ssize += 2;
|
|
139 #endif
|
|
140 fr->framesize = (long) tabsel_123[fr->lsf][2][fr->bitrate_index] * 144000;
|
|
141 fr->framesize /= mpglib_freqs[fr->sampling_frequency]<<(fr->lsf);
|
|
142 fr->framesize = fr->framesize + fr->padding - 4;
|
|
143 break;
|
|
144 default:
|
|
145 Sound_SetError("MPGLIB: Unknown layer type.");
|
|
146 return (0);
|
|
147 }
|
|
148 return 1;
|
|
149 }
|
|
150
|
|
151 #if 0
|
|
152 void print_header(struct frame *fr)
|
|
153 {
|
|
154 static char *modes[4] = { "Stereo", "Joint-Stereo", "Dual-Channel", "Single-Channel" };
|
|
155 static char *layers[4] = { "Unknown" , "I", "II", "III" };
|
|
156
|
|
157 fprintf(stderr,"MPEG %s, Layer: %s, Freq: %ld, mode: %s, modext: %d, BPF : %d\n",
|
|
158 fr->mpeg25 ? "2.5" : (fr->lsf ? "2.0" : "1.0"),
|
|
159 layers[fr->lay],mpglib_freqs[fr->sampling_frequency],
|
|
160 modes[fr->mode],fr->mode_ext,fr->framesize+4);
|
|
161 fprintf(stderr,"Channels: %d, copyright: %s, original: %s, CRC: %s, emphasis: %d.\n",
|
|
162 fr->stereo,fr->copyright?"Yes":"No",
|
|
163 fr->original?"Yes":"No",fr->error_protection?"Yes":"No",
|
|
164 fr->emphasis);
|
|
165 fprintf(stderr,"Bitrate: %d Kbits/s, Extension value: %d\n",
|
|
166 tabsel_123[fr->lsf][fr->lay-1][fr->bitrate_index],fr->extension);
|
|
167 }
|
|
168
|
|
169 void print_header_compact(struct frame *fr)
|
|
170 {
|
|
171 static char *modes[4] = { "stereo", "joint-stereo", "dual-channel", "mono" };
|
|
172 static char *layers[4] = { "Unknown" , "I", "II", "III" };
|
|
173
|
|
174 fprintf(stderr,"MPEG %s layer %s, %d kbit/s, %ld Hz %s\n",
|
|
175 fr->mpeg25 ? "2.5" : (fr->lsf ? "2.0" : "1.0"),
|
|
176 layers[fr->lay],
|
|
177 tabsel_123[fr->lsf][fr->lay-1][fr->bitrate_index],
|
|
178 mpglib_freqs[fr->sampling_frequency], modes[fr->mode]);
|
|
179 }
|
|
180
|
|
181 #endif
|
|
182
|
|
183 unsigned int getbits(int number_of_bits)
|
|
184 {
|
|
185 unsigned long rval;
|
|
186
|
|
187 if(!number_of_bits)
|
|
188 return 0;
|
|
189
|
|
190 {
|
|
191 rval = wordpointer[0];
|
|
192 rval <<= 8;
|
|
193 rval |= wordpointer[1];
|
|
194 rval <<= 8;
|
|
195 rval |= wordpointer[2];
|
|
196 rval <<= bitindex;
|
|
197 rval &= 0xffffff;
|
|
198
|
|
199 bitindex += number_of_bits;
|
|
200
|
|
201 rval >>= (24-number_of_bits);
|
|
202
|
|
203 wordpointer += (bitindex>>3);
|
|
204 bitindex &= 7;
|
|
205 }
|
|
206 return rval;
|
|
207 }
|
|
208
|
|
209 unsigned int getbits_fast(int number_of_bits)
|
|
210 {
|
|
211 unsigned long rval;
|
|
212
|
|
213 {
|
|
214 rval = wordpointer[0];
|
|
215 rval <<= 8;
|
|
216 rval |= wordpointer[1];
|
|
217 rval <<= bitindex;
|
|
218 rval &= 0xffff;
|
|
219 bitindex += number_of_bits;
|
|
220
|
|
221 rval >>= (16-number_of_bits);
|
|
222
|
|
223 wordpointer += (bitindex>>3);
|
|
224 bitindex &= 7;
|
|
225 }
|
|
226 return rval;
|
|
227 }
|
|
228
|
|
229 unsigned int get1bit(void)
|
|
230 {
|
|
231 unsigned char rval;
|
|
232 rval = *wordpointer << bitindex;
|
|
233
|
|
234 bitindex++;
|
|
235 wordpointer += (bitindex>>3);
|
|
236 bitindex &= 7;
|
|
237
|
|
238 return rval>>7;
|
|
239 }
|
|
240
|
|
241
|
|
242
|