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