1 // #define NEWBUFFERING
4 /* 1 frame = 4608 byte PCM */
6 #define LOCAL static inline
20 #include "cpudetect.h"
21 //#include "liba52/mm_accel.h"
23 #include "libmpcodecs/ad_mp3lib.h"
24 #include "libvo/fastmemcpy.h"
26 #include "libavutil/common.h"
29 // 3DNow! and 3DNow!Ext routines don't compile under AMD64
31 #undef HAVE_AMD3DNOWEXT
32 #define HAVE_AMD3DNOW 0
33 #define HAVE_AMD3DNOWEXT 0
36 //static FILE* mp3_file=NULL;
42 int MP3_fpos
=0; // current file position
43 int MP3_framesize
=0; // current framesize
44 int MP3_bitrate
=0; // current bitrate
45 int MP3_samplerate
=0; // current samplerate
50 static long outscale
= 32768;
54 LOCAL
int mp3_read(char *buf
,int size
){
55 // int len=fread(buf,1,size,mp3_file);
56 int len
=mplayer_audio_read(buf
,size
);
57 if(len
>0) MP3_fpos
+=len
;
58 // if(len!=size) MP3_eof=1;
62 int mp3_read(char *buf
,int size
);
65 * Modified for use with MPlayer, for details see the changelog at
66 * http://svn.mplayerhq.hu/mplayer/trunk/
71 //void mp3_seek(int pos){
72 // fseek(mp3_file,pos,SEEK_SET);
73 // return MP3_fpos = ftell(mp3_file);
78 #define MAXFRAMESIZE 1280
79 #define MAXFRAMESIZE2 (512+MAXFRAMESIZE)
81 static int fsizeold
=0,ssize
=0;
82 static unsigned char bsspace
[2][MAXFRAMESIZE2
]; /* !!!!! */
83 static unsigned char *bsbufold
=bsspace
[0]+512;
84 static unsigned char *bsbuf
=bsspace
[1]+512;
88 static unsigned char *wordpointer
;
91 static unsigned char *pcm_sample
; /* outbuffer address */
92 static int pcm_point
= 0; /* outbuffer offset */
94 static struct frame fr
;
96 static int tabsel_123
[2][3][16] = {
97 { {0,32,64,96,128,160,192,224,256,288,320,352,384,416,448,},
98 {0,32,48,56, 64, 80, 96,112,128,160,192,224,256,320,384,},
99 {0,32,40,48, 56, 64, 80, 96,112,128,160,192,224,256,320,} },
101 { {0,32,48,56,64,80,96,112,128,144,160,176,192,224,256,},
102 {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,},
103 {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,} }
106 static int freqs
[9] = { 44100, 48000, 32000, 22050, 24000, 16000 , 11025 , 12000 , 8000 };
108 LOCAL
unsigned int getbits(short number_of_bits
)
111 // if(MP3_frames>=7741) printf("getbits: bits=%d bitsleft=%d wordptr=%x\n",number_of_bits,bitsleft,wordpointer);
112 if((bitsleft
-=number_of_bits
)<0) return 0;
113 if(!number_of_bits
) return 0;
114 rval
= wordpointer
[0];
116 rval
|= wordpointer
[1];
118 rval
|= wordpointer
[2];
121 bitindex
+= number_of_bits
;
122 rval
>>= (24-number_of_bits
);
123 wordpointer
+= (bitindex
>>3);
129 LOCAL
unsigned int getbits_fast(short number_of_bits
)
132 // if(MP3_frames>=7741) printf("getbits_fast: bits=%d bitsleft=%d wordptr=%x\n",number_of_bits,bitsleft,wordpointer);
133 if((bitsleft
-=number_of_bits
)<0) return 0;
134 if(!number_of_bits
) return 0;
136 rval
= bswap_16(*((uint16_t *)wordpointer
));
139 * we may not be able to address unaligned 16-bit data on non-x86 cpus.
140 * Fall back to some portable code.
142 rval
= wordpointer
[0] << 8 | wordpointer
[1];
146 bitindex
+= number_of_bits
;
147 rval
>>= (16-number_of_bits
);
148 wordpointer
+= (bitindex
>>3);
153 LOCAL
unsigned int get1bit(void)
156 // if(MP3_frames>=7741) printf("get1bit: bitsleft=%d wordptr=%x\n",bitsleft,wordpointer);
157 if((--bitsleft
)<0) return 0;
158 rval
= *wordpointer
<< bitindex
;
160 wordpointer
+= (bitindex
>>3);
162 return (rval
>> 7) & 1;
165 LOCAL
void set_pointer(int backstep
)
167 // if(backstep!=512 && backstep>fsizeold)
168 // printf("\rWarning! backstep (%d>%d) \n",backstep,fsizeold);
169 wordpointer
= bsbuf
+ ssize
- backstep
;
170 if (backstep
) fast_memcpy(wordpointer
,bsbufold
+fsizeold
-backstep
,backstep
);
172 bitsleft
+=8*backstep
;
173 // printf("Backstep %d (bitsleft=%d)\n",backstep,bitsleft);
176 LOCAL
int stream_head_read(unsigned char *hbuf
,uint32_t *newhead
){
177 if(mp3_read(hbuf
,4) != 4) return FALSE
;
179 *newhead
= bswap_32(*((uint32_t*)hbuf
));
182 * we may not be able to address unaligned 32-bit data on non-x86 cpus.
183 * Fall back to some portable code.
194 LOCAL
int stream_head_shift(unsigned char *hbuf
,uint32_t *head
){
195 *((uint32_t*)hbuf
) >>= 8;
196 if(mp3_read(hbuf
+3,1) != 1) return 0;
203 * decode a header and write the information
204 * into the frame structure
206 LOCAL
int decode_header(struct frame
*fr
,uint32_t newhead
){
209 if( (newhead
& 0xffe00000) != 0xffe00000 ||
210 (newhead
& 0x0000fc00) == 0x0000fc00) return FALSE
;
212 fr
->lay
= 4-((newhead
>>17)&3);
213 // if(fr->lay!=3) return FALSE;
215 if( newhead
& (1<<20) ) {
216 fr
->lsf
= (newhead
& (1<<19)) ? 0x0 : 0x1;
224 fr
->sampling_frequency
= 6 + ((newhead
>>10)&0x3);
226 fr
->sampling_frequency
= ((newhead
>>10)&0x3) + (fr
->lsf
*3);
228 if(fr
->sampling_frequency
>8) return FALSE
; // valid: 0..8
230 fr
->error_protection
= ((newhead
>>16)&0x1)^0x1;
231 fr
->bitrate_index
= ((newhead
>>12)&0xf);
232 fr
->padding
= ((newhead
>>9)&0x1);
233 fr
->extension
= ((newhead
>>8)&0x1);
234 fr
->mode
= ((newhead
>>6)&0x3);
235 fr
->mode_ext
= ((newhead
>>4)&0x3);
236 fr
->copyright
= ((newhead
>>3)&0x1);
237 fr
->original
= ((newhead
>>2)&0x1);
238 fr
->emphasis
= newhead
& 0x3;
240 MP3_channels
= fr
->stereo
= (fr
->mode
== MPG_MD_MONO
) ? 1 : 2;
242 if(!fr
->bitrate_index
){
243 // fprintf(stderr,"Free format not supported.\n");
249 MP3_bitrate
=tabsel_123
[fr
->lsf
][1][fr
->bitrate_index
];
250 MP3_samplerate
=freqs
[fr
->sampling_frequency
];
251 fr
->framesize
= MP3_bitrate
* 144000;
252 fr
->framesize
/= MP3_samplerate
;
253 MP3_framesize
=fr
->framesize
;
254 fr
->framesize
+= fr
->padding
- 4;
258 ssize
= (fr
->stereo
== 1) ? 9 : 17;
260 ssize
= (fr
->stereo
== 1) ? 17 : 32;
261 if(fr
->error_protection
) ssize
+= 2;
263 MP3_bitrate
=tabsel_123
[fr
->lsf
][2][fr
->bitrate_index
];
264 MP3_samplerate
=freqs
[fr
->sampling_frequency
];
265 fr
->framesize
= MP3_bitrate
* 144000;
266 fr
->framesize
/= MP3_samplerate
<<(fr
->lsf
);
267 MP3_framesize
=fr
->framesize
;
268 fr
->framesize
+= fr
->padding
- 4;
271 // fr->jsbound = (fr->mode == MPG_MD_JOINT_STEREO) ? (fr->mode_ext<<2)+4 : 32;
272 MP3_bitrate
=tabsel_123
[fr
->lsf
][0][fr
->bitrate_index
];
273 MP3_samplerate
=freqs
[fr
->sampling_frequency
];
274 fr
->framesize
= MP3_bitrate
* 12000;
275 fr
->framesize
/= MP3_samplerate
;
276 MP3_framesize
= ((fr
->framesize
+fr
->padding
)<<2);
277 fr
->framesize
= MP3_framesize
-4;
278 // printf("framesize=%d\n",fr->framesize);
281 MP3_framesize
=fr
->framesize
=0;
282 // fprintf(stderr,"Sorry, unsupported layer type.\n");
285 if(fr
->framesize
<=0 || fr
->framesize
>MAXFRAMESIZE
) return FALSE
;
291 LOCAL
int stream_read_frame_body(int size
){
293 /* flip/init buffer for Layer 3 */
295 bsbuf
= bsspace
[bsnum
]+512;
296 bsnum
= (bsnum
+ 1) & 1;
298 if( mp3_read(bsbuf
,size
) != size
) return 0; // broken frame
301 wordpointer
= (unsigned char *) bsbuf
;
308 /*****************************************************************
309 * read next frame return number of frames read.
311 LOCAL
int read_frame(struct frame
*fr
){
314 unsigned char buf
[8];
315 unsigned long dummy
; // for alignment
317 int skipped
,resyncpos
;
325 fsizeold
=fr
->framesize
; /* for Layer3 */
326 if(!stream_head_read(hbuf
.buf
,&newhead
)) return 0;
327 if(!decode_header(fr
,newhead
)){
328 // invalid header! try to resync stream!
330 printf("ReSync: searching for a valid header... (pos=%X)\n",MP3_fpos
);
333 while(!decode_header(fr
,newhead
)){
334 if(!stream_head_shift(hbuf
.buf
,&newhead
)) return 0;
336 resyncpos
=MP3_fpos
-4;
337 // found valid header
339 printf("ReSync: found valid hdr at %X fsize=%ld ",resyncpos
,fr
->framesize
);
341 if(!stream_read_frame_body(fr
->framesize
)) return 0; // read body
343 fsizeold
=fr
->framesize
; /* for Layer3 */
344 if(!stream_head_read(hbuf
.buf
,&newhead
)) return 0;
345 if(!decode_header(fr
,newhead
)){
346 // invalid hdr! go back...
350 // mp3_seek(resyncpos+1);
351 if(!stream_head_read(hbuf
.buf
,&newhead
)) return 0;
360 skipped
=resyncpos
-skipped
;
361 // if(skipped && !MP3_resync) printf("\r%d bad bytes skipped (resync at 0x%X) \n",skipped,resyncpos);
363 // 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":"");
365 /* read main data into memory */
366 if(!stream_read_frame_body(fr
->framesize
)){
367 printf("\nBroken frame at 0x%X \n",resyncpos
);
374 if(frames
==1) goto resync
;
380 static int _has_mmx
= 0; // used by layer2.c, layer3.c to pre-scale coeffs
382 /******************************************************************************/
383 /* PUBLIC FUNCTIONS */
384 /******************************************************************************/
386 void (*dct64_MMX_func
)(short *, short *, real
*);
392 #include "cpudetect.h"
394 // Init decoder tables. Call first, once!
395 #ifdef CONFIG_FAKE_MONO
396 void MP3_Init(int fakemono
){
401 //gCpuCaps.hasMMX=gCpuCaps.hasMMX2=gCpuCaps.hasSSE=0; // for testing!
406 make_decode_tables(outscale
);
412 synth_func
= synth_1to1_MMX
;
417 if (gCpuCaps
.has3DNowExt
)
419 dct36_func
=dct36_3dnowex
;
420 dct64_MMX_func
= dct64_MMX_3dnowex
;
421 mp_msg(MSGT_DECAUDIO
,MSGL_V
,"mp3lib: using 3DNow!Ex optimized decore!\n");
426 if (gCpuCaps
.has3DNow
)
428 dct36_func
= dct36_3dnow
;
429 dct64_MMX_func
= dct64_MMX_3dnow
;
430 mp_msg(MSGT_DECAUDIO
,MSGL_V
,"mp3lib: using 3DNow! optimized decore!\n");
437 dct64_MMX_func
= dct64_sse
;
438 mp_msg(MSGT_DECAUDIO
,MSGL_V
,"mp3lib: using SSE optimized decore!\n");
446 dct64_MMX_func
= dct64_MMX
;
447 mp_msg(MSGT_DECAUDIO
,MSGL_V
,"mp3lib: using MMX optimized decore!\n");
451 if (gCpuCaps
.cpuType
>= CPUTYPE_I586
)
453 synth_func
= synth_1to1_pent
;
454 mp_msg(MSGT_DECAUDIO
,MSGL_V
,"mp3lib: using Pentium optimized decore!\n");
457 #endif /* ARCH_X86_32 */
459 if (gCpuCaps
.hasAltiVec
)
461 mp_msg(MSGT_DECAUDIO
,MSGL_V
,"mp3lib: using AltiVec optimized decore!\n");
466 synth_func
= NULL
; /* use default c version */
467 mp_msg(MSGT_DECAUDIO
,MSGL_V
,"mp3lib: using generic C decore!\n");
470 #ifdef CONFIG_FAKE_MONO
472 fr
.synth
=synth_1to1_l
;
473 else if (fakemono
== 2)
474 fr
.synth
=synth_1to1_r
;
480 fr
.synth_mono
=synth_1to1_mono2stereo
;
482 fr
.down_sample_sblimit
= SBLIMIT
>>(fr
.down_sample
);
485 init_layer3(fr
.down_sample_sblimit
);
486 mp_msg(MSGT_DECAUDIO
,MSGL_V
,"MP3lib: init layer2&3 finished, tables done\n");
491 void MP3_Close(void){
493 if(mp3_file
) fclose(mp3_file
);
497 // Open a file, init buffers. Call once per file!
498 int MP3_Open(char *filename
,int buffsize
){
499 MP3_eof
=1; // lock decoding
500 MP3_pause
=1; // lock playing
501 if(mp3_file
) MP3_Close(); // close prev. file
504 mp3_file
=fopen(filename
,"rb");
505 // printf("MP3_Open: file='%s'",filename);
506 // if(!mp3_file){ printf(" not found!\n"); return 0;} else printf("Ok!\n");
507 if(!mp3_file
) return 0;
509 MP3_filesize
=MP3_PrintTAG();
510 fseek(mp3_file
,0,SEEK_SET
);
512 MP3_InitBuffers(buffsize
);
513 if(!tables_done_flag
) MP3_Init();
514 MP3_eof
=0; // allow decoding
515 MP3_pause
=0; // allow playing
521 // Read & decode a single frame. Called by sound driver.
522 int MP3_DecodeFrame(unsigned char *hova
,short single
){
525 if(!read_frame(&fr
)) return 0;
526 if(single
==-2){ set_pointer(512); return 1; }
527 if(fr
.error_protection
) getbits(16); /* skip crc */
530 case 2: do_layer2(&fr
,single
);break;
531 case 3: do_layer3(&fr
,single
);break;
532 case 1: do_layer1(&fr
,single
);break;
534 return 0; // unsupported
537 return pcm_point
? pcm_point
: 2;
540 // Prints last frame header in ascii.
541 void MP3_PrintHeader(void){
542 static char *modes
[4] = { "Stereo", "Joint-Stereo", "Dual-Channel", "Single-Channel" };
543 static char *layers
[4] = { "???" , "I", "II", "III" };
545 mp_msg(MSGT_DECAUDIO
,MSGL_V
,"\rMPEG %s, Layer %s, %d Hz %d kbit %s, BPF: %d\n",
546 fr
.mpeg25
? "2.5" : (fr
.lsf
? "2.0" : "1.0"),
547 layers
[fr
.lay
],freqs
[fr
.sampling_frequency
],
548 tabsel_123
[fr
.lsf
][fr
.lay
-1][fr
.bitrate_index
],
549 modes
[fr
.mode
],fr
.framesize
+4);
550 mp_msg(MSGT_DECAUDIO
,MSGL_V
,"Channels: %d, copyright: %s, original: %s, CRC: %s, emphasis: %d\n",
551 fr
.stereo
,fr
.copyright
?"Yes":"No",
552 fr
.original
?"Yes":"No",fr
.error_protection
?"Yes":"No",
559 // Read & print ID3 TAG. Do not call when playing!!! returns filesize.
560 int MP3_PrintTAG(void){
572 char artist
[31]={0,};
575 char comment
[31]={0,};
580 fseek(mp3_file
,0,SEEK_END
);
581 fsize
=ftell(mp3_file
);
582 if(fseek(mp3_file
,-128,SEEK_END
)) return fsize
;
583 ret
=fread(&tag
,128,1,mp3_file
);
584 if(ret
!=1 || tag
.tag
[0]!='T' || tag
.tag
[1]!='A' || tag
.tag
[2]!='G') return fsize
;
586 strncpy(title
,tag
.title
,30);
587 strncpy(artist
,tag
.artist
,30);
588 strncpy(album
,tag
.album
,30);
589 strncpy(year
,tag
.year
,4);
590 strncpy(comment
,tag
.comment
,30);
592 if ( tag
.genre
<= sizeof(genre_table
)/sizeof(*genre_table
) ) {
593 strncpy(genre
, genre_table
[tag
.genre
], 30);
595 strncpy(genre
,"Unknown",30);
599 printf("Title : %30s Artist: %s\n",title
,artist
);
600 printf("Album : %30s Year : %4s\n",album
,year
);
601 printf("Comment: %30s Genre : %s\n",comment
,genre
);