Patch by Stefan Huehner / stefan % huehner ! org \
[mplayer/glamo.git] / mp3lib / sr1.c
blob9ac4bf4065b446b50f5859a79f7ce43a6b282b66
1 // #define NEWBUFFERING
2 //#define DEBUG_RESYNC
4 /* 1 frame = 4608 byte PCM */
6 #ifdef __GNUC__
7 #define LOCAL static inline
8 #else
9 #define LOCAL static _inline
10 #endif
12 //#undef LOCAL
13 //#define LOCAL
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include <string.h>
18 #include <signal.h>
19 #include <math.h>
21 #define real float
22 // #define int long
24 #include "mpg123.h"
25 #include "huffman.h"
26 #include "mp3.h"
27 #include "bswap.h"
28 #include "cpudetect.h"
29 //#include "liba52/mm_accel.h"
30 #include "mp_msg.h"
32 #include "fastmemcpy.h"
34 #ifdef ARCH_X86
35 #define CAN_COMPILE_X86_ASM
36 #endif
38 //static FILE* mp3_file=NULL;
40 int MP3_frames=0;
41 int MP3_eof=0;
42 int MP3_pause=0;
43 int MP3_filesize=0;
44 int MP3_fpos=0; // current file position
45 int MP3_framesize=0; // current framesize
46 int MP3_bitrate=0; // current bitrate
47 int MP3_samplerate=0; // current samplerate
48 int MP3_resync=0;
49 int MP3_channels=0;
50 int MP3_bps=2;
52 static long outscale = 32768;
53 #include "tabinit.c"
55 #if 1
56 extern int mplayer_audio_read(char *buf,int size);
58 LOCAL int mp3_read(char *buf,int size){
59 // int len=fread(buf,1,size,mp3_file);
60 int len=mplayer_audio_read(buf,size);
61 if(len>0) MP3_fpos+=len;
62 // if(len!=size) MP3_eof=1;
63 return len;
65 #else
66 extern int mp3_read(char *buf,int size);
67 #endif
69 * Modified for use with MPlayer, for details see the CVS changelog at
70 * http://www.mplayerhq.hu/cgi-bin/cvsweb.cgi/main/
71 * $Id$
75 //void mp3_seek(int pos){
76 // fseek(mp3_file,pos,SEEK_SET);
77 // return (MP3_fpos=ftell(mp3_file));
78 //}
80 /* Frame reader */
82 #define MAXFRAMESIZE 1280
83 #define MAXFRAMESIZE2 (512+MAXFRAMESIZE)
85 static int fsizeold=0,ssize=0;
86 static unsigned char bsspace[2][MAXFRAMESIZE2]; /* !!!!! */
87 static unsigned char *bsbufold=bsspace[0]+512;
88 static unsigned char *bsbuf=bsspace[1]+512;
89 static int bsnum=0;
91 static int bitindex;
92 static unsigned char *wordpointer;
93 static int bitsleft;
95 static unsigned char *pcm_sample; /* outbuffer address */
96 static int pcm_point = 0; /* outbuffer offset */
98 static struct frame fr;
100 static int tabsel_123[2][3][16] = {
101 { {0,32,64,96,128,160,192,224,256,288,320,352,384,416,448,},
102 {0,32,48,56, 64, 80, 96,112,128,160,192,224,256,320,384,},
103 {0,32,40,48, 56, 64, 80, 96,112,128,160,192,224,256,320,} },
105 { {0,32,48,56,64,80,96,112,128,144,160,176,192,224,256,},
106 {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,},
107 {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,} }
110 static long freqs[9] = { 44100, 48000, 32000, 22050, 24000, 16000 , 11025 , 12000 , 8000 };
112 LOCAL unsigned int getbits(short number_of_bits)
114 unsigned long rval;
115 // if(MP3_frames>=7741) printf("getbits: bits=%d bitsleft=%d wordptr=%x\n",number_of_bits,bitsleft,wordpointer);
116 if((bitsleft-=number_of_bits)<0) return 0;
117 if(!number_of_bits) return 0;
118 rval = wordpointer[0];
119 rval <<= 8;
120 rval |= wordpointer[1];
121 rval <<= 8;
122 rval |= wordpointer[2];
123 rval <<= bitindex;
124 rval &= 0xffffff;
125 bitindex += number_of_bits;
126 rval >>= (24-number_of_bits);
127 wordpointer += (bitindex>>3);
128 bitindex &= 7;
129 return rval;
133 LOCAL unsigned int getbits_fast(short number_of_bits)
135 unsigned long rval;
136 // if(MP3_frames>=7741) printf("getbits_fast: bits=%d bitsleft=%d wordptr=%x\n",number_of_bits,bitsleft,wordpointer);
137 if((bitsleft-=number_of_bits)<0) return 0;
138 if(!number_of_bits) return 0;
139 #if defined(CAN_COMPILE_X86_ASM)
140 rval = bswap_16(*((unsigned short *)wordpointer));
141 #else
143 * we may not be able to address unaligned 16-bit data on non-x86 cpus.
144 * Fall back to some portable code.
146 rval = wordpointer[0] << 8 | wordpointer[1];
147 #endif
148 rval <<= bitindex;
149 rval &= 0xffff;
150 bitindex += number_of_bits;
151 rval >>= (16-number_of_bits);
152 wordpointer += (bitindex>>3);
153 bitindex &= 7;
154 return rval;
157 LOCAL unsigned int get1bit(void)
159 unsigned char rval;
160 // if(MP3_frames>=7741) printf("get1bit: bitsleft=%d wordptr=%x\n",bitsleft,wordpointer);
161 if((--bitsleft)<0) return 0;
162 rval = *wordpointer << bitindex;
163 bitindex++;
164 wordpointer += (bitindex>>3);
165 bitindex &= 7;
166 return ((rval>>7)&1);
169 LOCAL void set_pointer(long backstep)
171 // if(backstep!=512 && backstep>fsizeold)
172 // printf("\rWarning! backstep (%d>%d) \n",backstep,fsizeold);
173 wordpointer = bsbuf + ssize - backstep;
174 if (backstep) memcpy(wordpointer,bsbufold+fsizeold-backstep,backstep);
175 bitindex = 0;
176 bitsleft+=8*backstep;
177 // printf("Backstep %d (bitsleft=%d)\n",backstep,bitsleft);
180 LOCAL int stream_head_read(unsigned char *hbuf,unsigned long *newhead){
181 if(mp3_read(hbuf,4) != 4) return FALSE;
182 #if defined(CAN_COMPILE_X86_ASM)
183 *newhead = bswap_32(*((unsigned long *)hbuf));
184 #else
186 * we may not be able to address unaligned 32-bit data on non-x86 cpus.
187 * Fall back to some portable code.
189 *newhead =
190 hbuf[0] << 24 |
191 hbuf[1] << 16 |
192 hbuf[2] << 8 |
193 hbuf[3];
194 #endif
195 return TRUE;
198 LOCAL int stream_head_shift(unsigned char *hbuf,unsigned long *head){
199 *((unsigned long *)hbuf) >>= 8;
200 if(mp3_read(hbuf+3,1) != 1) return 0;
201 *head <<= 8;
202 *head |= hbuf[3];
203 return 1;
207 * decode a header and write the information
208 * into the frame structure
210 LOCAL int decode_header(struct frame *fr,unsigned long newhead){
212 // head_check:
213 if( (newhead & 0xffe00000) != 0xffe00000 ||
214 (newhead & 0x0000fc00) == 0x0000fc00) return FALSE;
216 fr->lay = 4-((newhead>>17)&3);
217 // if(fr->lay!=3) return FALSE;
219 if( newhead & ((long)1<<20) ) {
220 fr->lsf = (newhead & ((long)1<<19)) ? 0x0 : 0x1;
221 fr->mpeg25 = 0;
222 } else {
223 fr->lsf = 1;
224 fr->mpeg25 = 1;
227 if(fr->mpeg25)
228 fr->sampling_frequency = 6 + ((newhead>>10)&0x3);
229 else
230 fr->sampling_frequency = ((newhead>>10)&0x3) + (fr->lsf*3);
232 if(fr->sampling_frequency>8) return FALSE; // valid: 0..8
234 fr->error_protection = ((newhead>>16)&0x1)^0x1;
235 fr->bitrate_index = ((newhead>>12)&0xf);
236 fr->padding = ((newhead>>9)&0x1);
237 fr->extension = ((newhead>>8)&0x1);
238 fr->mode = ((newhead>>6)&0x3);
239 fr->mode_ext = ((newhead>>4)&0x3);
240 fr->copyright = ((newhead>>3)&0x1);
241 fr->original = ((newhead>>2)&0x1);
242 fr->emphasis = newhead & 0x3;
244 MP3_channels = fr->stereo = (fr->mode == MPG_MD_MONO) ? 1 : 2;
246 if(!fr->bitrate_index){
247 // fprintf(stderr,"Free format not supported.\n");
248 return FALSE;
251 switch(fr->lay){
252 case 2:
253 MP3_bitrate=tabsel_123[fr->lsf][1][fr->bitrate_index];
254 MP3_samplerate=freqs[fr->sampling_frequency];
255 fr->framesize = (long) MP3_bitrate * 144000;
256 fr->framesize /= MP3_samplerate;
257 MP3_framesize=fr->framesize;
258 fr->framesize += fr->padding - 4;
259 break;
260 case 3:
261 if(fr->lsf)
262 ssize = (fr->stereo == 1) ? 9 : 17;
263 else
264 ssize = (fr->stereo == 1) ? 17 : 32;
265 if(fr->error_protection) ssize += 2;
267 MP3_bitrate=tabsel_123[fr->lsf][2][fr->bitrate_index];
268 MP3_samplerate=freqs[fr->sampling_frequency];
269 fr->framesize = (long) MP3_bitrate * 144000;
270 fr->framesize /= MP3_samplerate<<(fr->lsf);
271 MP3_framesize=fr->framesize;
272 fr->framesize += fr->padding - 4;
273 break;
274 case 1:
275 // fr->jsbound = (fr->mode == MPG_MD_JOINT_STEREO) ? (fr->mode_ext<<2)+4 : 32;
276 MP3_bitrate=tabsel_123[fr->lsf][0][fr->bitrate_index];
277 MP3_samplerate=freqs[fr->sampling_frequency];
278 fr->framesize = (long) MP3_bitrate * 12000;
279 fr->framesize /= MP3_samplerate;
280 MP3_framesize = ((fr->framesize+fr->padding)<<2);
281 fr->framesize = MP3_framesize-4;
282 // printf("framesize=%d\n",fr->framesize);
283 break;
284 default:
285 MP3_framesize=fr->framesize=0;
286 // fprintf(stderr,"Sorry, unsupported layer type.\n");
287 return 0;
289 if(fr->framesize<=0 || fr->framesize>MAXFRAMESIZE) return FALSE;
291 return 1;
295 LOCAL int stream_read_frame_body(int size){
297 /* flip/init buffer for Layer 3 */
298 bsbufold = bsbuf;
299 bsbuf = bsspace[bsnum]+512;
300 bsnum = (bsnum + 1) & 1;
302 if( mp3_read(bsbuf,size) != size) return 0; // broken frame
304 bitindex = 0;
305 wordpointer = (unsigned char *) bsbuf;
306 bitsleft=8*size;
308 return 1;
312 /*****************************************************************
313 * read next frame return number of frames read.
315 LOCAL int read_frame(struct frame *fr){
316 unsigned long newhead;
317 unsigned char hbuf[8];
318 int skipped,resyncpos;
319 int frames=0;
321 resync:
322 skipped=MP3_fpos;
323 resyncpos=MP3_fpos;
325 set_pointer(512);
326 fsizeold=fr->framesize; /* for Layer3 */
327 if(!stream_head_read(hbuf,&newhead)) return 0;
328 if(!decode_header(fr,newhead)){
329 // invalid header! try to resync stream!
330 #ifdef DEBUG_RESYNC
331 printf("ReSync: searching for a valid header... (pos=%X)\n",MP3_fpos);
332 #endif
333 retry1:
334 while(!decode_header(fr,newhead)){
335 if(!stream_head_shift(hbuf,&newhead)) return 0;
337 resyncpos=MP3_fpos-4;
338 // found valid header
339 #ifdef DEBUG_RESYNC
340 printf("ReSync: found valid hdr at %X fsize=%ld ",resyncpos,fr->framesize);
341 #endif
342 if(!stream_read_frame_body(fr->framesize)) return 0; // read body
343 set_pointer(512);
344 fsizeold=fr->framesize; /* for Layer3 */
345 if(!stream_head_read(hbuf,&newhead)) return 0;
346 if(!decode_header(fr,newhead)){
347 // invalid hdr! go back...
348 #ifdef DEBUG_RESYNC
349 printf("INVALID\n");
350 #endif
351 // mp3_seek(resyncpos+1);
352 if(!stream_head_read(hbuf,&newhead)) return 0;
353 goto retry1;
355 #ifdef DEBUG_RESYNC
356 printf("OK!\n");
357 ++frames;
358 #endif
361 skipped=resyncpos-skipped;
362 // if(skipped && !MP3_resync) printf("\r%d bad bytes skipped (resync at 0x%X) \n",skipped,resyncpos);
364 // 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":"");
366 /* read main data into memory */
367 if(!stream_read_frame_body(fr->framesize)){
368 printf("\nBroken frame at 0x%X \n",resyncpos);
369 return 0;
371 ++frames;
373 if(MP3_resync){
374 MP3_resync=0;
375 if(frames==1) goto resync;
378 return frames;
381 static int _has_mmx = 0; // used by layer2.c, layer3.c to pre-scale coeffs
383 #include "layer2.c"
384 #include "layer3.c"
385 #include "layer1.c"
387 /******************************************************************************/
388 /* PUBLIC FUNCTIONS */
389 /******************************************************************************/
391 /* It's hidden from gcc in assembler */
392 extern void dct64_MMX(real *, real *, real *);
393 extern void dct64_MMX_3dnow(real *, real *, real *);
394 extern void dct64_MMX_3dnowex(real *, real *, real *);
395 void (*dct64_MMX_func)(real *, real *, real *);
397 #include "cpudetect.h"
399 // Init decoder tables. Call first, once!
400 #ifdef USE_FAKE_MONO
401 void MP3_Init(int fakemono){
402 #else
403 void MP3_Init(){
404 #endif
406 //gCpuCaps.hasMMX=gCpuCaps.hasMMX2=gCpuCaps.hasSSE=0; // for testing!
408 _has_mmx = 0;
409 dct36_func = dct36;
411 make_decode_tables(outscale);
413 #ifdef CAN_COMPILE_X86_ASM
415 if (gCpuCaps.hasMMX)
417 _has_mmx = 1;
418 make_decode_tables_MMX(outscale);
419 mp_msg(MSGT_DECAUDIO,MSGL_V,"mp3lib: made decode tables with MMX optimization\n");
420 synth_func = synth_1to1_MMX;
423 if (gCpuCaps.has3DNowExt)
425 dct36_func=dct36_3dnowex;
426 dct64_MMX_func= (gCpuCaps.hasMMX2) ? dct64_MMX_3dnowex : dct64_MMX_3dnow;
427 mp_msg(MSGT_DECAUDIO,MSGL_V,"mp3lib: using 3DNow!Ex optimized decore!\n");
429 else
430 if (gCpuCaps.has3DNow)
432 dct36_func = dct36_3dnow;
433 dct64_MMX_func = dct64_MMX_3dnow;
434 mp_msg(MSGT_DECAUDIO,MSGL_V,"mp3lib: using 3DNow! optimized decore!\n");
436 else
437 if (gCpuCaps.hasMMX)
439 dct64_MMX_func = dct64_MMX;
440 mp_msg(MSGT_DECAUDIO,MSGL_V,"mp3lib: using MMX optimized decore!\n");
442 else
443 if (gCpuCaps.cpuType >= CPUTYPE_I586)
445 synth_func = synth_1to1_pent;
446 mp_msg(MSGT_DECAUDIO,MSGL_V,"mp3lib: using Pentium optimized decore!\n");
448 else
449 #endif
451 synth_func = NULL; /* use default c version */
452 mp_msg(MSGT_DECAUDIO,MSGL_V,"mp3lib: using generic C decore!\n");
455 #ifdef USE_FAKE_MONO
456 if (fakemono == 1)
457 fr.synth=synth_1to1_l;
458 else if (fakemono == 2)
459 fr.synth=synth_1to1_r;
460 else
461 fr.synth=synth_1to1;
462 #else
463 fr.synth=synth_1to1;
464 #endif
465 fr.synth_mono=synth_1to1_mono2stereo;
466 fr.down_sample=0;
467 fr.down_sample_sblimit = SBLIMIT>>(fr.down_sample);
469 init_layer2();
470 init_layer3(fr.down_sample_sblimit);
471 mp_msg(MSGT_DECAUDIO,MSGL_V,"MP3lib: init layer2&3 finished, tables done\n");
474 #if 0
476 void MP3_Close(){
477 MP3_eof=1;
478 if(mp3_file) fclose(mp3_file);
479 mp3_file=NULL;
482 // Open a file, init buffers. Call once per file!
483 int MP3_Open(char *filename,int buffsize){
484 MP3_eof=1; // lock decoding
485 MP3_pause=1; // lock playing
486 if(mp3_file) MP3_Close(); // close prev. file
487 MP3_frames=0;
489 mp3_file=fopen(filename,"rb");
490 // printf("MP3_Open: file='%s'",filename);
491 // if(!mp3_file){ printf(" not found!\n"); return 0;} else printf("Ok!\n");
492 if(!mp3_file) return 0;
494 MP3_filesize=MP3_PrintTAG();
495 fseek(mp3_file,0,SEEK_SET);
497 MP3_InitBuffers(buffsize);
498 if(!tables_done_flag) MP3_Init();
499 MP3_eof=0; // allow decoding
500 MP3_pause=0; // allow playing
501 return MP3_filesize;
504 #endif
506 // Read & decode a single frame. Called by sound driver.
507 int MP3_DecodeFrame(unsigned char *hova,short single){
508 pcm_sample = hova;
509 pcm_point = 0;
510 if(!read_frame(&fr))return(0);
511 if(single==-2){ set_pointer(512); return(1); }
512 if(fr.error_protection) getbits(16); /* skip crc */
513 fr.single=single;
514 switch(fr.lay){
515 case 2: do_layer2(&fr,single);break;
516 case 3: do_layer3(&fr,single);break;
517 case 1: do_layer1(&fr,single);break;
518 default:
519 return 0; // unsupported
521 // ++MP3_frames;
522 return(pcm_point?pcm_point:2);
525 // Prints last frame header in ascii.
526 void MP3_PrintHeader(void){
527 static char *modes[4] = { "Stereo", "Joint-Stereo", "Dual-Channel", "Single-Channel" };
528 static char *layers[4] = { "???" , "I", "II", "III" };
530 mp_msg(MSGT_DECAUDIO,MSGL_V,"\rMPEG %s, Layer %s, %ld Hz %d kbit %s, BPF: %ld\n",
531 fr.mpeg25 ? "2.5" : (fr.lsf ? "2.0" : "1.0"),
532 layers[fr.lay],freqs[fr.sampling_frequency],
533 tabsel_123[fr.lsf][fr.lay-1][fr.bitrate_index],
534 modes[fr.mode],fr.framesize+4);
535 mp_msg(MSGT_DECAUDIO,MSGL_V,"Channels: %d, copyright: %s, original: %s, CRC: %s, emphasis: %d\n",
536 fr.stereo,fr.copyright?"Yes":"No",
537 fr.original?"Yes":"No",fr.error_protection?"Yes":"No",
538 fr.emphasis);
541 #if 0
542 #include "genre.h"
544 // Read & print ID3 TAG. Do not call when playing!!! returns filesize.
545 int MP3_PrintTAG(){
546 struct id3tag {
547 char tag[3];
548 char title[30];
549 char artist[30];
550 char album[30];
551 char year[4];
552 char comment[30];
553 unsigned char genre;
555 struct id3tag tag;
556 char title[31]={0,};
557 char artist[31]={0,};
558 char album[31]={0,};
559 char year[5]={0,};
560 char comment[31]={0,};
561 char genre[31]={0,};
562 int fsize;
563 int ret;
565 fseek(mp3_file,0,SEEK_END);
566 fsize=ftell(mp3_file);
567 if(fseek(mp3_file,-128,SEEK_END)) return fsize;
568 ret=fread(&tag,128,1,mp3_file);
569 if(ret!=1 || tag.tag[0]!='T' || tag.tag[1]!='A' || tag.tag[2]!='G') return fsize;
571 strncpy(title,tag.title,30);
572 strncpy(artist,tag.artist,30);
573 strncpy(album,tag.album,30);
574 strncpy(year,tag.year,4);
575 strncpy(comment,tag.comment,30);
577 if ( tag.genre <= sizeof(genre_table)/sizeof(*genre_table) ) {
578 strncpy(genre, genre_table[tag.genre], 30);
579 } else {
580 strncpy(genre,"Unknown",30);
583 // printf("\n");
584 printf("Title : %30s Artist: %s\n",title,artist);
585 printf("Album : %30s Year : %4s\n",album,year);
586 printf("Comment: %30s Genre : %s\n",comment,genre);
587 printf("\n");
588 return fsize-128;
591 #endif