Mercurial > mplayer.hg
annotate mp3lib/sr1.c @ 1195:d45ff719db7a
Maybe the problem with including wine headers from /usr/local/include/ instead of from mplayer source has been eliminated
author | lgb |
---|---|
date | Fri, 22 Jun 2001 20:23:14 +0000 |
parents | 95f6f14176c6 |
children | 03b7e2955a20 |
rev | line source |
---|---|
1 | 1 // #define NEWBUFFERING |
2 //#define DEBUG_RESYNC | |
3 | |
4 /* 1 frame = 4608 byte PCM */ | |
5 | |
6 #ifdef __GNUC__ | |
7 #define LOCAL static inline | |
8 #else | |
9 #define LOCAL static _inline | |
10 #endif | |
11 | |
12 //#undef LOCAL | |
13 //#define LOCAL | |
14 | |
15 #include <stdlib.h> | |
16 #include <stdio.h> | |
17 #include <string.h> | |
18 #include <signal.h> | |
19 #include <math.h> | |
20 | |
21 #define real float | |
22 // #define int long | |
23 | |
24 #include "mpg123.h" | |
25 #include "huffman.h" | |
26 #include "mp3.h" | |
1040 | 27 #include "bswap.h" |
1 | 28 #include "d_cpu.h" |
29 | |
1045 | 30 #include "fastmemcpy.h" |
1 | 31 //static FILE* mp3_file=NULL; |
32 | |
33 int MP3_frames=0; | |
34 int MP3_eof=0; | |
35 int MP3_pause=0; | |
36 int MP3_filesize=0; | |
37 int MP3_fpos=0; // current file position | |
38 int MP3_framesize=0; // current framesize | |
39 int MP3_bitrate=0; // current bitrate | |
40 int MP3_samplerate=0; // current samplerate | |
41 int MP3_resync=0; | |
42 int MP3_channels=0; | |
43 int MP3_bps=2; | |
44 | |
45 static long outscale = 32768; | |
46 #include "tabinit.c" | |
47 | |
48 extern int mplayer_audio_read(char *buf,int size); | |
49 | |
50 #if 1 | |
51 LOCAL int mp3_read(char *buf,int size){ | |
52 // int len=fread(buf,1,size,mp3_file); | |
53 int len=mplayer_audio_read(buf,size); | |
54 if(len>0) MP3_fpos+=len; | |
55 // if(len!=size) MP3_eof=1; | |
56 return len; | |
57 } | |
58 #else | |
59 extern int mp3_read(char *buf,int size); | |
60 #endif | |
61 | |
62 //void mp3_seek(int pos){ | |
63 // fseek(mp3_file,pos,SEEK_SET); | |
64 // return (MP3_fpos=ftell(mp3_file)); | |
65 //} | |
66 | |
67 /* Frame reader */ | |
68 | |
69 #define MAXFRAMESIZE 1280 | |
70 #define MAXFRAMESIZE2 (512+MAXFRAMESIZE) | |
71 | |
72 static int fsizeold=0,ssize=0; | |
73 static unsigned char bsspace[2][MAXFRAMESIZE2]; /* !!!!! */ | |
74 static unsigned char *bsbufold=bsspace[0]+512; | |
75 static unsigned char *bsbuf=bsspace[1]+512; | |
76 static int bsnum=0; | |
77 | |
78 static int bitindex; | |
79 static unsigned char *wordpointer; | |
80 static int bitsleft; | |
81 | |
82 unsigned char *pcm_sample; /* az outbuffer CIME */ | |
83 int pcm_point = 0; /* ez az outbuffer pozicioja */ | |
84 | |
85 static struct frame fr; | |
86 | |
87 static int tabsel_123[2][3][16] = { | |
88 { {0,32,64,96,128,160,192,224,256,288,320,352,384,416,448,}, | |
89 {0,32,48,56, 64, 80, 96,112,128,160,192,224,256,320,384,}, | |
90 {0,32,40,48, 56, 64, 80, 96,112,128,160,192,224,256,320,} }, | |
91 | |
92 { {0,32,48,56,64,80,96,112,128,144,160,176,192,224,256,}, | |
93 {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,}, | |
94 {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,} } | |
95 }; | |
96 | |
97 static long freqs[9] = { 44100, 48000, 32000, 22050, 24000, 16000 , 11025 , 12000 , 8000 }; | |
98 | |
99 LOCAL unsigned int getbits(short number_of_bits) | |
100 { | |
101 unsigned long rval; | |
102 // if(MP3_frames>=7741) printf("getbits: bits=%d bitsleft=%d wordptr=%x\n",number_of_bits,bitsleft,wordpointer); | |
103 if((bitsleft-=number_of_bits)<0) return 0; | |
104 if(!number_of_bits) return 0; | |
105 rval = wordpointer[0]; | |
106 rval <<= 8; | |
107 rval |= wordpointer[1]; | |
108 rval <<= 8; | |
109 rval |= wordpointer[2]; | |
110 rval <<= bitindex; | |
111 rval &= 0xffffff; | |
112 bitindex += number_of_bits; | |
113 rval >>= (24-number_of_bits); | |
114 wordpointer += (bitindex>>3); | |
115 bitindex &= 7; | |
116 return rval; | |
117 } | |
118 | |
119 | |
120 LOCAL unsigned int getbits_fast(short number_of_bits) | |
121 { | |
122 unsigned long rval; | |
123 // if(MP3_frames>=7741) printf("getbits_fast: bits=%d bitsleft=%d wordptr=%x\n",number_of_bits,bitsleft,wordpointer); | |
124 if((bitsleft-=number_of_bits)<0) return 0; | |
125 if(!number_of_bits) return 0; | |
1040 | 126 rval = bswap_16(*((unsigned short *)wordpointer)); |
1 | 127 rval <<= bitindex; |
128 rval &= 0xffff; | |
129 bitindex += number_of_bits; | |
130 rval >>= (16-number_of_bits); | |
131 wordpointer += (bitindex>>3); | |
132 bitindex &= 7; | |
133 return rval; | |
134 } | |
135 | |
136 LOCAL unsigned int get1bit(void) | |
137 { | |
138 unsigned char rval; | |
139 // if(MP3_frames>=7741) printf("get1bit: bitsleft=%d wordptr=%x\n",bitsleft,wordpointer); | |
140 if((--bitsleft)<0) return 0; | |
141 rval = *wordpointer << bitindex; | |
142 bitindex++; | |
143 wordpointer += (bitindex>>3); | |
144 bitindex &= 7; | |
145 return ((rval>>7)&1); | |
146 } | |
147 | |
148 LOCAL void set_pointer(long backstep) | |
149 { | |
150 // if(backstep!=512 && backstep>fsizeold) | |
151 // printf("\rWarning! backstep (%d>%d) \n",backstep,fsizeold); | |
152 wordpointer = bsbuf + ssize - backstep; | |
153 if (backstep) memcpy(wordpointer,bsbufold+fsizeold-backstep,backstep); | |
154 bitindex = 0; | |
155 bitsleft+=8*backstep; | |
156 // printf("Backstep %d (bitsleft=%d)\n",backstep,bitsleft); | |
157 } | |
158 | |
159 LOCAL int stream_head_read(unsigned char *hbuf,unsigned long *newhead){ | |
160 if(mp3_read(hbuf,4) != 4) return FALSE; | |
1040 | 161 *newhead = bswap_32(*((unsigned long *)hbuf)); |
162 return TRUE; | |
1 | 163 } |
164 | |
165 LOCAL int stream_head_shift(unsigned char *hbuf,unsigned long *head){ | |
1040 | 166 *((unsigned long *)hbuf) >>= 8; |
1 | 167 if(mp3_read(hbuf+3,1) != 1) return 0; |
168 *head <<= 8; | |
169 *head |= hbuf[3]; | |
170 return 1; | |
171 } | |
172 | |
173 /* | |
174 * decode a header and write the information | |
175 * into the frame structure | |
176 */ | |
177 LOCAL int decode_header(struct frame *fr,unsigned long newhead){ | |
178 | |
179 // head_check: | |
1040 | 180 if( (newhead & 0xffe00000) != 0xffe00000 || |
1045 | 181 (newhead & 0x0000fc00) == 0x0000fc00) return FALSE; |
1 | 182 |
183 fr->lay = 4-((newhead>>17)&3); | |
184 // if(fr->lay!=3) return FALSE; | |
185 | |
186 if( newhead & ((long)1<<20) ) { | |
187 fr->lsf = (newhead & ((long)1<<19)) ? 0x0 : 0x1; | |
188 fr->mpeg25 = 0; | |
189 } else { | |
190 fr->lsf = 1; | |
191 fr->mpeg25 = 1; | |
192 } | |
193 | |
194 if(fr->mpeg25) | |
195 fr->sampling_frequency = 6 + ((newhead>>10)&0x3); | |
196 else | |
197 fr->sampling_frequency = ((newhead>>10)&0x3) + (fr->lsf*3); | |
198 | |
1166
95f6f14176c6
fr->sampling_frequency limitation (thanx to pl <p_l@tfz.net>)
arpi_esp
parents:
1045
diff
changeset
|
199 if(fr->sampling_frequency>8) return FALSE; // valid: 0..8 |
95f6f14176c6
fr->sampling_frequency limitation (thanx to pl <p_l@tfz.net>)
arpi_esp
parents:
1045
diff
changeset
|
200 |
1 | 201 fr->error_protection = ((newhead>>16)&0x1)^0x1; |
202 fr->bitrate_index = ((newhead>>12)&0xf); | |
203 fr->padding = ((newhead>>9)&0x1); | |
204 fr->extension = ((newhead>>8)&0x1); | |
205 fr->mode = ((newhead>>6)&0x3); | |
206 fr->mode_ext = ((newhead>>4)&0x3); | |
207 fr->copyright = ((newhead>>3)&0x1); | |
208 fr->original = ((newhead>>2)&0x1); | |
209 fr->emphasis = newhead & 0x3; | |
210 | |
211 MP3_channels = fr->stereo = (fr->mode == MPG_MD_MONO) ? 1 : 2; | |
212 | |
213 if(!fr->bitrate_index){ | |
214 // fprintf(stderr,"Free format not supported.\n"); | |
215 return FALSE; | |
216 } | |
217 | |
218 switch(fr->lay){ | |
219 case 2: | |
220 MP3_bitrate=tabsel_123[fr->lsf][1][fr->bitrate_index]; | |
221 MP3_samplerate=freqs[fr->sampling_frequency]; | |
222 fr->framesize = (long) MP3_bitrate * 144000; | |
223 fr->framesize /= MP3_samplerate; | |
224 MP3_framesize=fr->framesize; | |
225 fr->framesize += fr->padding - 4; | |
226 break; | |
227 case 3: | |
228 if(fr->lsf) | |
229 ssize = (fr->stereo == 1) ? 9 : 17; | |
230 else | |
231 ssize = (fr->stereo == 1) ? 17 : 32; | |
232 if(fr->error_protection) ssize += 2; | |
233 | |
234 MP3_bitrate=tabsel_123[fr->lsf][2][fr->bitrate_index]; | |
235 MP3_samplerate=freqs[fr->sampling_frequency]; | |
236 fr->framesize = (long) MP3_bitrate * 144000; | |
237 fr->framesize /= MP3_samplerate<<(fr->lsf); | |
238 MP3_framesize=fr->framesize; | |
239 fr->framesize += fr->padding - 4; | |
240 break; | |
241 default: | |
242 // fprintf(stderr,"Sorry, unsupported layer type.\n"); | |
243 return 0; | |
244 } | |
245 if(fr->framesize<=0 || fr->framesize>MAXFRAMESIZE) return FALSE; | |
246 | |
247 return 1; | |
248 } | |
249 | |
250 | |
251 LOCAL int stream_read_frame_body(int size){ | |
252 | |
253 /* flip/init buffer for Layer 3 */ | |
254 bsbufold = bsbuf; | |
255 bsbuf = bsspace[bsnum]+512; | |
256 bsnum = (bsnum + 1) & 1; | |
257 | |
258 if( mp3_read(bsbuf,size) != size) return 0; // broken frame | |
259 | |
260 bitindex = 0; | |
261 wordpointer = (unsigned char *) bsbuf; | |
262 bitsleft=8*size; | |
263 | |
264 return 1; | |
265 } | |
266 | |
267 | |
268 /***************************************************************** | |
269 * read next frame return number of frames read. | |
270 */ | |
271 LOCAL int read_frame(struct frame *fr){ | |
272 unsigned long newhead; | |
273 unsigned char hbuf[8]; | |
274 int skipped,resyncpos; | |
275 int frames=0; | |
276 | |
277 resync: | |
278 skipped=MP3_fpos; | |
279 resyncpos=MP3_fpos; | |
280 | |
281 set_pointer(512); | |
282 fsizeold=fr->framesize; /* for Layer3 */ | |
283 if(!stream_head_read(hbuf,&newhead)) return 0; | |
284 if(!decode_header(fr,newhead)){ | |
285 // invalid header! try to resync stream! | |
286 #ifdef DEBUG_RESYNC | |
287 printf("ReSync: searching for a valid header... (pos=%X)\n",MP3_fpos); | |
288 #endif | |
289 retry1: | |
290 while(!decode_header(fr,newhead)){ | |
291 if(!stream_head_shift(hbuf,&newhead)) return 0; | |
292 } | |
293 resyncpos=MP3_fpos-4; | |
294 // found valid header | |
295 #ifdef DEBUG_RESYNC | |
296 printf("ReSync: found valid hdr at %X fsize=%ld ",resyncpos,fr->framesize); | |
297 #endif | |
298 if(!stream_read_frame_body(fr->framesize)) return 0; // read body | |
299 set_pointer(512); | |
300 fsizeold=fr->framesize; /* for Layer3 */ | |
301 if(!stream_head_read(hbuf,&newhead)) return 0; | |
302 if(!decode_header(fr,newhead)){ | |
303 // invalid hdr! go back... | |
304 #ifdef DEBUG_RESYNC | |
305 printf("INVALID\n"); | |
306 #endif | |
307 // mp3_seek(resyncpos+1); | |
308 if(!stream_head_read(hbuf,&newhead)) return 0; | |
309 goto retry1; | |
310 } | |
311 #ifdef DEBUG_RESYNC | |
312 printf("OK!\n"); | |
313 ++frames; | |
314 #endif | |
315 } | |
316 | |
317 skipped=resyncpos-skipped; | |
318 // if(skipped && !MP3_resync) printf("\r%d bad bytes skipped (resync at 0x%X) \n",skipped,resyncpos); | |
319 | |
320 // printf("%8X [%08X] %d %d (%d)%s%s\n",MP3_fpos-4,newhead,fr->framesize,fr->mode,fr->mode_ext,fr->error_protection?" CRC":"",fr->padding?" PAD":""); | |
321 | |
322 /* read main data into memory */ | |
323 if(!stream_read_frame_body(fr->framesize)){ | |
324 printf("\nBroken frame at 0x%X \n",resyncpos); | |
325 return 0; | |
326 } | |
327 ++frames; | |
328 | |
329 if(MP3_resync){ | |
330 MP3_resync=0; | |
331 if(frames==1) goto resync; | |
332 } | |
333 | |
334 return frames; | |
335 } | |
336 | |
337 #include "layer2.c" | |
338 #include "layer3.c" | |
339 | |
340 /******************************************************************************/ | |
341 /* PUBLIC FUNCTIONS */ | |
342 /******************************************************************************/ | |
343 | |
344 static int tables_done_flag=0; | |
345 | |
346 // Init decoder tables. Call first, once! | |
732
e14114170e01
applied 'fakemono' patch by Bryan Chan scorpio@acm.org
arpi_esp
parents:
73
diff
changeset
|
347 #ifdef USE_FAKE_MONO |
e14114170e01
applied 'fakemono' patch by Bryan Chan scorpio@acm.org
arpi_esp
parents:
73
diff
changeset
|
348 void MP3_Init(int fakemono){ |
e14114170e01
applied 'fakemono' patch by Bryan Chan scorpio@acm.org
arpi_esp
parents:
73
diff
changeset
|
349 #else |
1 | 350 void MP3_Init(){ |
732
e14114170e01
applied 'fakemono' patch by Bryan Chan scorpio@acm.org
arpi_esp
parents:
73
diff
changeset
|
351 #endif |
1 | 352 _CpuID=CpuDetect(); |
353 _i586=ipentium(); | |
354 #ifdef HAVE_3DNOW | |
355 _3dnow=a3dnow(); | |
356 #endif | |
357 | |
358 printf( "mp3lib: Processor ID: %x\n",_CpuID ); | |
359 printf( "mp3lib: i586 processor %sdetected.\n",(_i586?"":"not ") ); | |
360 #ifdef HAVE_3DNOW | |
73 | 361 printf( "mp3lib: AMD 3dnow! extension %sdetected.\n",(_3dnow?"":"not ") ); |
1 | 362 #endif |
735 | 363 #ifdef HAVE_3DNOWEX |
364 printf( "mp3lib: AMD 3dnow-dsp! extension %sdetected.\n",(_3dnow>1?"":"not ") ); | |
365 #endif | |
1 | 366 |
367 make_decode_tables(outscale); | |
732
e14114170e01
applied 'fakemono' patch by Bryan Chan scorpio@acm.org
arpi_esp
parents:
73
diff
changeset
|
368 #ifdef USE_FAKE_MONO |
e14114170e01
applied 'fakemono' patch by Bryan Chan scorpio@acm.org
arpi_esp
parents:
73
diff
changeset
|
369 if (fakemono == 1) |
e14114170e01
applied 'fakemono' patch by Bryan Chan scorpio@acm.org
arpi_esp
parents:
73
diff
changeset
|
370 fr.synth=synth_1to1_l; |
e14114170e01
applied 'fakemono' patch by Bryan Chan scorpio@acm.org
arpi_esp
parents:
73
diff
changeset
|
371 else if (fakemono == 2) |
e14114170e01
applied 'fakemono' patch by Bryan Chan scorpio@acm.org
arpi_esp
parents:
73
diff
changeset
|
372 fr.synth=synth_1to1_r; |
e14114170e01
applied 'fakemono' patch by Bryan Chan scorpio@acm.org
arpi_esp
parents:
73
diff
changeset
|
373 else |
e14114170e01
applied 'fakemono' patch by Bryan Chan scorpio@acm.org
arpi_esp
parents:
73
diff
changeset
|
374 fr.synth=synth_1to1; |
e14114170e01
applied 'fakemono' patch by Bryan Chan scorpio@acm.org
arpi_esp
parents:
73
diff
changeset
|
375 #else |
1 | 376 fr.synth=synth_1to1; |
732
e14114170e01
applied 'fakemono' patch by Bryan Chan scorpio@acm.org
arpi_esp
parents:
73
diff
changeset
|
377 #endif |
1 | 378 fr.synth_mono=synth_1to1_mono2stereo; |
379 fr.down_sample=0; | |
380 fr.down_sample_sblimit = SBLIMIT>>(fr.down_sample); | |
381 init_layer2(); | |
382 init_layer3(fr.down_sample_sblimit); | |
383 tables_done_flag=1; | |
384 } | |
385 | |
386 #if 0 | |
387 | |
388 void MP3_Close(){ | |
389 MP3_eof=1; | |
390 if(mp3_file) fclose(mp3_file); | |
391 mp3_file=NULL; | |
392 } | |
393 | |
394 // Open a file, init buffers. Call once per file! | |
395 int MP3_Open(char *filename,int buffsize){ | |
396 MP3_eof=1; // lock decoding | |
397 MP3_pause=1; // lock playing | |
398 if(mp3_file) MP3_Close(); // close prev. file | |
399 MP3_frames=0; | |
400 | |
401 mp3_file=fopen(filename,"rb"); | |
402 // printf("MP3_Open: file='%s'",filename); | |
403 // if(!mp3_file){ printf(" not found!\n"); return 0;} else printf("Ok!\n"); | |
404 if(!mp3_file) return 0; | |
405 | |
406 MP3_filesize=MP3_PrintTAG(); | |
407 fseek(mp3_file,0,SEEK_SET); | |
408 | |
409 MP3_InitBuffers(buffsize); | |
410 if(!tables_done_flag) MP3_Init(); | |
411 MP3_eof=0; // allow decoding | |
412 MP3_pause=0; // allow playing | |
413 return MP3_filesize; | |
414 } | |
415 | |
416 #endif | |
417 | |
418 // Read & decode a single frame. Called by sound driver. | |
419 int MP3_DecodeFrame(unsigned char *hova,short single){ | |
420 pcm_sample = hova; | |
421 pcm_point = 0; | |
422 if(!read_frame(&fr))return(0); | |
423 if(single==-2){ set_pointer(512); return(1); } | |
424 if(fr.error_protection) getbits(16); /* skip crc */ | |
425 fr.single=single; | |
426 switch(fr.lay){ | |
427 case 2: do_layer2(&fr,single);break; | |
428 case 3: do_layer3(&fr,single);break; | |
429 } | |
430 // ++MP3_frames; | |
431 return(pcm_point?pcm_point:2); | |
432 } | |
433 | |
434 #if 0 | |
435 | |
436 // Prints last frame header in ascii. | |
437 void MP3_PrintHeader(){ | |
438 static char *modes[4] = { "Stereo", "Joint-Stereo", "Dual-Channel", "Single-Channel" }; | |
439 static char *layers[4] = { "???" , "I", "II", "III" }; | |
440 | |
441 printf("\rMPEG %s, Layer %s, %ld Hz %d kbit %s, BPF : %ld\n", | |
442 fr.mpeg25 ? "2.5" : (fr.lsf ? "2.0" : "1.0"), | |
443 layers[fr.lay],freqs[fr.sampling_frequency], | |
444 tabsel_123[fr.lsf][fr.lay-1][fr.bitrate_index], | |
445 modes[fr.mode],fr.framesize+4); | |
446 printf("Channels: %d, copyright: %s, original: %s, CRC: %s, emphasis: %d.\n\n", | |
447 fr.stereo,fr.copyright?"Yes":"No", | |
448 fr.original?"Yes":"No",fr.error_protection?"Yes":"No", | |
449 fr.emphasis); | |
450 } | |
451 | |
452 #include "genre.h" | |
453 | |
454 // Read & print ID3 TAG. Do not call when playing!!! returns filesize. | |
455 int MP3_PrintTAG(){ | |
456 struct id3tag { | |
457 char tag[3]; | |
458 char title[30]; | |
459 char artist[30]; | |
460 char album[30]; | |
461 char year[4]; | |
462 char comment[30]; | |
463 unsigned char genre; | |
464 }; | |
465 struct id3tag tag; | |
466 char title[31]={0,}; | |
467 char artist[31]={0,}; | |
468 char album[31]={0,}; | |
469 char year[5]={0,}; | |
470 char comment[31]={0,}; | |
471 char genre[31]={0,}; | |
472 int fsize; | |
473 int ret; | |
474 | |
475 fseek(mp3_file,0,SEEK_END); | |
476 fsize=ftell(mp3_file); | |
477 if(fseek(mp3_file,-128,SEEK_END)) return fsize; | |
478 ret=fread(&tag,128,1,mp3_file); | |
479 if(ret!=1 || tag.tag[0]!='T' || tag.tag[1]!='A' || tag.tag[2]!='G') return fsize; | |
480 | |
481 strncpy(title,tag.title,30); | |
482 strncpy(artist,tag.artist,30); | |
483 strncpy(album,tag.album,30); | |
484 strncpy(year,tag.year,4); | |
485 strncpy(comment,tag.comment,30); | |
486 | |
487 if ( tag.genre <= sizeof(genre_table)/sizeof(*genre_table) ) { | |
488 strncpy(genre, genre_table[tag.genre], 30); | |
489 } else { | |
490 strncpy(genre,"Unknown",30); | |
491 } | |
492 | |
493 // printf("\n"); | |
494 printf("Title : %30s Artist: %s\n",title,artist); | |
495 printf("Album : %30s Year : %4s\n",album,year); | |
496 printf("Comment: %30s Genre : %s\n",comment,genre); | |
497 printf("\n"); | |
498 return fsize-128; | |
499 } | |
500 | |
501 #endif |