Mercurial > mplayer.hg
annotate mp3lib/sr1.c @ 1132:80a0f8aa2360
Added for development only this is in pre-alpha state, do not use!
author | atmosfear |
---|---|
date | Fri, 15 Jun 2001 16:31:19 +0000 |
parents | 2ee9a18ed6f3 |
children | 95f6f14176c6 |
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 | |
199 fr->error_protection = ((newhead>>16)&0x1)^0x1; | |
200 fr->bitrate_index = ((newhead>>12)&0xf); | |
201 fr->padding = ((newhead>>9)&0x1); | |
202 fr->extension = ((newhead>>8)&0x1); | |
203 fr->mode = ((newhead>>6)&0x3); | |
204 fr->mode_ext = ((newhead>>4)&0x3); | |
205 fr->copyright = ((newhead>>3)&0x1); | |
206 fr->original = ((newhead>>2)&0x1); | |
207 fr->emphasis = newhead & 0x3; | |
208 | |
209 MP3_channels = fr->stereo = (fr->mode == MPG_MD_MONO) ? 1 : 2; | |
210 | |
211 if(!fr->bitrate_index){ | |
212 // fprintf(stderr,"Free format not supported.\n"); | |
213 return FALSE; | |
214 } | |
215 | |
216 switch(fr->lay){ | |
217 case 2: | |
218 MP3_bitrate=tabsel_123[fr->lsf][1][fr->bitrate_index]; | |
219 MP3_samplerate=freqs[fr->sampling_frequency]; | |
220 fr->framesize = (long) MP3_bitrate * 144000; | |
221 fr->framesize /= MP3_samplerate; | |
222 MP3_framesize=fr->framesize; | |
223 fr->framesize += fr->padding - 4; | |
224 break; | |
225 case 3: | |
226 if(fr->lsf) | |
227 ssize = (fr->stereo == 1) ? 9 : 17; | |
228 else | |
229 ssize = (fr->stereo == 1) ? 17 : 32; | |
230 if(fr->error_protection) ssize += 2; | |
231 | |
232 MP3_bitrate=tabsel_123[fr->lsf][2][fr->bitrate_index]; | |
233 MP3_samplerate=freqs[fr->sampling_frequency]; | |
234 fr->framesize = (long) MP3_bitrate * 144000; | |
235 fr->framesize /= MP3_samplerate<<(fr->lsf); | |
236 MP3_framesize=fr->framesize; | |
237 fr->framesize += fr->padding - 4; | |
238 break; | |
239 default: | |
240 // fprintf(stderr,"Sorry, unsupported layer type.\n"); | |
241 return 0; | |
242 } | |
243 if(fr->framesize<=0 || fr->framesize>MAXFRAMESIZE) return FALSE; | |
244 | |
245 return 1; | |
246 } | |
247 | |
248 | |
249 LOCAL int stream_read_frame_body(int size){ | |
250 | |
251 /* flip/init buffer for Layer 3 */ | |
252 bsbufold = bsbuf; | |
253 bsbuf = bsspace[bsnum]+512; | |
254 bsnum = (bsnum + 1) & 1; | |
255 | |
256 if( mp3_read(bsbuf,size) != size) return 0; // broken frame | |
257 | |
258 bitindex = 0; | |
259 wordpointer = (unsigned char *) bsbuf; | |
260 bitsleft=8*size; | |
261 | |
262 return 1; | |
263 } | |
264 | |
265 | |
266 /***************************************************************** | |
267 * read next frame return number of frames read. | |
268 */ | |
269 LOCAL int read_frame(struct frame *fr){ | |
270 unsigned long newhead; | |
271 unsigned char hbuf[8]; | |
272 int skipped,resyncpos; | |
273 int frames=0; | |
274 | |
275 resync: | |
276 skipped=MP3_fpos; | |
277 resyncpos=MP3_fpos; | |
278 | |
279 set_pointer(512); | |
280 fsizeold=fr->framesize; /* for Layer3 */ | |
281 if(!stream_head_read(hbuf,&newhead)) return 0; | |
282 if(!decode_header(fr,newhead)){ | |
283 // invalid header! try to resync stream! | |
284 #ifdef DEBUG_RESYNC | |
285 printf("ReSync: searching for a valid header... (pos=%X)\n",MP3_fpos); | |
286 #endif | |
287 retry1: | |
288 while(!decode_header(fr,newhead)){ | |
289 if(!stream_head_shift(hbuf,&newhead)) return 0; | |
290 } | |
291 resyncpos=MP3_fpos-4; | |
292 // found valid header | |
293 #ifdef DEBUG_RESYNC | |
294 printf("ReSync: found valid hdr at %X fsize=%ld ",resyncpos,fr->framesize); | |
295 #endif | |
296 if(!stream_read_frame_body(fr->framesize)) return 0; // read body | |
297 set_pointer(512); | |
298 fsizeold=fr->framesize; /* for Layer3 */ | |
299 if(!stream_head_read(hbuf,&newhead)) return 0; | |
300 if(!decode_header(fr,newhead)){ | |
301 // invalid hdr! go back... | |
302 #ifdef DEBUG_RESYNC | |
303 printf("INVALID\n"); | |
304 #endif | |
305 // mp3_seek(resyncpos+1); | |
306 if(!stream_head_read(hbuf,&newhead)) return 0; | |
307 goto retry1; | |
308 } | |
309 #ifdef DEBUG_RESYNC | |
310 printf("OK!\n"); | |
311 ++frames; | |
312 #endif | |
313 } | |
314 | |
315 skipped=resyncpos-skipped; | |
316 // if(skipped && !MP3_resync) printf("\r%d bad bytes skipped (resync at 0x%X) \n",skipped,resyncpos); | |
317 | |
318 // 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":""); | |
319 | |
320 /* read main data into memory */ | |
321 if(!stream_read_frame_body(fr->framesize)){ | |
322 printf("\nBroken frame at 0x%X \n",resyncpos); | |
323 return 0; | |
324 } | |
325 ++frames; | |
326 | |
327 if(MP3_resync){ | |
328 MP3_resync=0; | |
329 if(frames==1) goto resync; | |
330 } | |
331 | |
332 return frames; | |
333 } | |
334 | |
335 #include "layer2.c" | |
336 #include "layer3.c" | |
337 | |
338 /******************************************************************************/ | |
339 /* PUBLIC FUNCTIONS */ | |
340 /******************************************************************************/ | |
341 | |
342 static int tables_done_flag=0; | |
343 | |
344 // Init decoder tables. Call first, once! | |
732
e14114170e01
applied 'fakemono' patch by Bryan Chan scorpio@acm.org
arpi_esp
parents:
73
diff
changeset
|
345 #ifdef USE_FAKE_MONO |
e14114170e01
applied 'fakemono' patch by Bryan Chan scorpio@acm.org
arpi_esp
parents:
73
diff
changeset
|
346 void MP3_Init(int fakemono){ |
e14114170e01
applied 'fakemono' patch by Bryan Chan scorpio@acm.org
arpi_esp
parents:
73
diff
changeset
|
347 #else |
1 | 348 void MP3_Init(){ |
732
e14114170e01
applied 'fakemono' patch by Bryan Chan scorpio@acm.org
arpi_esp
parents:
73
diff
changeset
|
349 #endif |
1 | 350 _CpuID=CpuDetect(); |
351 _i586=ipentium(); | |
352 #ifdef HAVE_3DNOW | |
353 _3dnow=a3dnow(); | |
354 #endif | |
355 | |
356 printf( "mp3lib: Processor ID: %x\n",_CpuID ); | |
357 printf( "mp3lib: i586 processor %sdetected.\n",(_i586?"":"not ") ); | |
358 #ifdef HAVE_3DNOW | |
73 | 359 printf( "mp3lib: AMD 3dnow! extension %sdetected.\n",(_3dnow?"":"not ") ); |
1 | 360 #endif |
735 | 361 #ifdef HAVE_3DNOWEX |
362 printf( "mp3lib: AMD 3dnow-dsp! extension %sdetected.\n",(_3dnow>1?"":"not ") ); | |
363 #endif | |
1 | 364 |
365 make_decode_tables(outscale); | |
732
e14114170e01
applied 'fakemono' patch by Bryan Chan scorpio@acm.org
arpi_esp
parents:
73
diff
changeset
|
366 #ifdef USE_FAKE_MONO |
e14114170e01
applied 'fakemono' patch by Bryan Chan scorpio@acm.org
arpi_esp
parents:
73
diff
changeset
|
367 if (fakemono == 1) |
e14114170e01
applied 'fakemono' patch by Bryan Chan scorpio@acm.org
arpi_esp
parents:
73
diff
changeset
|
368 fr.synth=synth_1to1_l; |
e14114170e01
applied 'fakemono' patch by Bryan Chan scorpio@acm.org
arpi_esp
parents:
73
diff
changeset
|
369 else if (fakemono == 2) |
e14114170e01
applied 'fakemono' patch by Bryan Chan scorpio@acm.org
arpi_esp
parents:
73
diff
changeset
|
370 fr.synth=synth_1to1_r; |
e14114170e01
applied 'fakemono' patch by Bryan Chan scorpio@acm.org
arpi_esp
parents:
73
diff
changeset
|
371 else |
e14114170e01
applied 'fakemono' patch by Bryan Chan scorpio@acm.org
arpi_esp
parents:
73
diff
changeset
|
372 fr.synth=synth_1to1; |
e14114170e01
applied 'fakemono' patch by Bryan Chan scorpio@acm.org
arpi_esp
parents:
73
diff
changeset
|
373 #else |
1 | 374 fr.synth=synth_1to1; |
732
e14114170e01
applied 'fakemono' patch by Bryan Chan scorpio@acm.org
arpi_esp
parents:
73
diff
changeset
|
375 #endif |
1 | 376 fr.synth_mono=synth_1to1_mono2stereo; |
377 fr.down_sample=0; | |
378 fr.down_sample_sblimit = SBLIMIT>>(fr.down_sample); | |
379 init_layer2(); | |
380 init_layer3(fr.down_sample_sblimit); | |
381 tables_done_flag=1; | |
382 } | |
383 | |
384 #if 0 | |
385 | |
386 void MP3_Close(){ | |
387 MP3_eof=1; | |
388 if(mp3_file) fclose(mp3_file); | |
389 mp3_file=NULL; | |
390 } | |
391 | |
392 // Open a file, init buffers. Call once per file! | |
393 int MP3_Open(char *filename,int buffsize){ | |
394 MP3_eof=1; // lock decoding | |
395 MP3_pause=1; // lock playing | |
396 if(mp3_file) MP3_Close(); // close prev. file | |
397 MP3_frames=0; | |
398 | |
399 mp3_file=fopen(filename,"rb"); | |
400 // printf("MP3_Open: file='%s'",filename); | |
401 // if(!mp3_file){ printf(" not found!\n"); return 0;} else printf("Ok!\n"); | |
402 if(!mp3_file) return 0; | |
403 | |
404 MP3_filesize=MP3_PrintTAG(); | |
405 fseek(mp3_file,0,SEEK_SET); | |
406 | |
407 MP3_InitBuffers(buffsize); | |
408 if(!tables_done_flag) MP3_Init(); | |
409 MP3_eof=0; // allow decoding | |
410 MP3_pause=0; // allow playing | |
411 return MP3_filesize; | |
412 } | |
413 | |
414 #endif | |
415 | |
416 // Read & decode a single frame. Called by sound driver. | |
417 int MP3_DecodeFrame(unsigned char *hova,short single){ | |
418 pcm_sample = hova; | |
419 pcm_point = 0; | |
420 if(!read_frame(&fr))return(0); | |
421 if(single==-2){ set_pointer(512); return(1); } | |
422 if(fr.error_protection) getbits(16); /* skip crc */ | |
423 fr.single=single; | |
424 switch(fr.lay){ | |
425 case 2: do_layer2(&fr,single);break; | |
426 case 3: do_layer3(&fr,single);break; | |
427 } | |
428 // ++MP3_frames; | |
429 return(pcm_point?pcm_point:2); | |
430 } | |
431 | |
432 #if 0 | |
433 | |
434 // Prints last frame header in ascii. | |
435 void MP3_PrintHeader(){ | |
436 static char *modes[4] = { "Stereo", "Joint-Stereo", "Dual-Channel", "Single-Channel" }; | |
437 static char *layers[4] = { "???" , "I", "II", "III" }; | |
438 | |
439 printf("\rMPEG %s, Layer %s, %ld Hz %d kbit %s, BPF : %ld\n", | |
440 fr.mpeg25 ? "2.5" : (fr.lsf ? "2.0" : "1.0"), | |
441 layers[fr.lay],freqs[fr.sampling_frequency], | |
442 tabsel_123[fr.lsf][fr.lay-1][fr.bitrate_index], | |
443 modes[fr.mode],fr.framesize+4); | |
444 printf("Channels: %d, copyright: %s, original: %s, CRC: %s, emphasis: %d.\n\n", | |
445 fr.stereo,fr.copyright?"Yes":"No", | |
446 fr.original?"Yes":"No",fr.error_protection?"Yes":"No", | |
447 fr.emphasis); | |
448 } | |
449 | |
450 #include "genre.h" | |
451 | |
452 // Read & print ID3 TAG. Do not call when playing!!! returns filesize. | |
453 int MP3_PrintTAG(){ | |
454 struct id3tag { | |
455 char tag[3]; | |
456 char title[30]; | |
457 char artist[30]; | |
458 char album[30]; | |
459 char year[4]; | |
460 char comment[30]; | |
461 unsigned char genre; | |
462 }; | |
463 struct id3tag tag; | |
464 char title[31]={0,}; | |
465 char artist[31]={0,}; | |
466 char album[31]={0,}; | |
467 char year[5]={0,}; | |
468 char comment[31]={0,}; | |
469 char genre[31]={0,}; | |
470 int fsize; | |
471 int ret; | |
472 | |
473 fseek(mp3_file,0,SEEK_END); | |
474 fsize=ftell(mp3_file); | |
475 if(fseek(mp3_file,-128,SEEK_END)) return fsize; | |
476 ret=fread(&tag,128,1,mp3_file); | |
477 if(ret!=1 || tag.tag[0]!='T' || tag.tag[1]!='A' || tag.tag[2]!='G') return fsize; | |
478 | |
479 strncpy(title,tag.title,30); | |
480 strncpy(artist,tag.artist,30); | |
481 strncpy(album,tag.album,30); | |
482 strncpy(year,tag.year,4); | |
483 strncpy(comment,tag.comment,30); | |
484 | |
485 if ( tag.genre <= sizeof(genre_table)/sizeof(*genre_table) ) { | |
486 strncpy(genre, genre_table[tag.genre], 30); | |
487 } else { | |
488 strncpy(genre,"Unknown",30); | |
489 } | |
490 | |
491 // printf("\n"); | |
492 printf("Title : %30s Artist: %s\n",title,artist); | |
493 printf("Album : %30s Year : %4s\n",album,year); | |
494 printf("Comment: %30s Genre : %s\n",comment,genre); | |
495 printf("\n"); | |
496 return fsize-128; | |
497 } | |
498 | |
499 #endif |