Fix vf_tcdump's compilation
[mplayer/kovensky.git] / mp3lib / sr1.c
blob3b3a2af2040e15a271e330f4b775fe5517ade6e5
1 // #define NEWBUFFERING
2 //#define DEBUG_RESYNC
4 /* 1 frame = 4608 byte PCM */
6 #define LOCAL static inline
8 //#undef LOCAL
9 //#define LOCAL
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <string.h>
14 #include <math.h>
16 #include "mpg123.h"
17 #include "huffman.h"
18 #include "mp3.h"
19 #include "mpbswap.h"
20 #include "cpudetect.h"
21 //#include "liba52/mm_accel.h"
22 #include "mp_msg.h"
23 #include "libmpcodecs/ad_mp3lib.h"
24 #include "libvo/fastmemcpy.h"
26 #include "libavutil/common.h"
28 #if ARCH_X86_64
29 // 3DNow! and 3DNow!Ext routines don't compile under AMD64
30 #undef HAVE_AMD3DNOW
31 #undef HAVE_AMD3DNOWEXT
32 #define HAVE_AMD3DNOW 0
33 #define HAVE_AMD3DNOWEXT 0
34 #endif
36 //static FILE* mp3_file=NULL;
38 int MP3_frames=0;
39 int MP3_eof=0;
40 int MP3_pause=0;
41 int MP3_filesize=0;
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
46 int MP3_resync=0;
47 int MP3_channels=0;
48 int MP3_bps=2;
50 static long outscale = 32768;
51 #include "tabinit.c"
53 #if 1
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;
59 return len;
61 #else
62 int mp3_read(char *buf,int size);
63 #endif
65 * Modified for use with MPlayer, for details see the changelog at
66 * http://svn.mplayerhq.hu/mplayer/trunk/
67 * $Id$
71 //void mp3_seek(int pos){
72 // fseek(mp3_file,pos,SEEK_SET);
73 // return MP3_fpos = ftell(mp3_file);
74 //}
76 /* Frame reader */
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;
85 static int bsnum=0;
87 static int bitindex;
88 static unsigned char *wordpointer;
89 static int bitsleft;
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)
110 unsigned rval;
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];
115 rval <<= 8;
116 rval |= wordpointer[1];
117 rval <<= 8;
118 rval |= wordpointer[2];
119 rval <<= bitindex;
120 rval &= 0xffffff;
121 bitindex += number_of_bits;
122 rval >>= (24-number_of_bits);
123 wordpointer += (bitindex>>3);
124 bitindex &= 7;
125 return rval;
129 LOCAL unsigned int getbits_fast(short number_of_bits)
131 unsigned rval;
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;
135 #if ARCH_X86
136 rval = bswap_16(*((uint16_t *)wordpointer));
137 #else
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];
143 #endif
144 rval <<= bitindex;
145 rval &= 0xffff;
146 bitindex += number_of_bits;
147 rval >>= (16-number_of_bits);
148 wordpointer += (bitindex>>3);
149 bitindex &= 7;
150 return rval;
153 LOCAL unsigned int get1bit(void)
155 unsigned char rval;
156 // if(MP3_frames>=7741) printf("get1bit: bitsleft=%d wordptr=%x\n",bitsleft,wordpointer);
157 if((--bitsleft)<0) return 0;
158 rval = *wordpointer << bitindex;
159 bitindex++;
160 wordpointer += (bitindex>>3);
161 bitindex &= 7;
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);
171 bitindex = 0;
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;
178 #if ARCH_X86
179 *newhead = bswap_32(*((uint32_t*)hbuf));
180 #else
182 * we may not be able to address unaligned 32-bit data on non-x86 cpus.
183 * Fall back to some portable code.
185 *newhead =
186 hbuf[0] << 24 |
187 hbuf[1] << 16 |
188 hbuf[2] << 8 |
189 hbuf[3];
190 #endif
191 return TRUE;
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;
197 *head <<= 8;
198 *head |= hbuf[3];
199 return 1;
203 * decode a header and write the information
204 * into the frame structure
206 LOCAL int decode_header(struct frame *fr,uint32_t newhead){
208 // head_check:
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;
217 fr->mpeg25 = 0;
218 } else {
219 fr->lsf = 1;
220 fr->mpeg25 = 1;
223 if(fr->mpeg25)
224 fr->sampling_frequency = 6 + ((newhead>>10)&0x3);
225 else
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");
244 return FALSE;
247 switch(fr->lay){
248 case 2:
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;
255 break;
256 case 3:
257 if(fr->lsf)
258 ssize = (fr->stereo == 1) ? 9 : 17;
259 else
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;
269 break;
270 case 1:
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);
279 break;
280 default:
281 MP3_framesize=fr->framesize=0;
282 // fprintf(stderr,"Sorry, unsupported layer type.\n");
283 return 0;
285 if(fr->framesize<=0 || fr->framesize>MAXFRAMESIZE) return FALSE;
287 return 1;
291 LOCAL int stream_read_frame_body(int size){
293 /* flip/init buffer for Layer 3 */
294 bsbufold = bsbuf;
295 bsbuf = bsspace[bsnum]+512;
296 bsnum = (bsnum + 1) & 1;
298 if( mp3_read(bsbuf,size) != size) return 0; // broken frame
300 bitindex = 0;
301 wordpointer = (unsigned char *) bsbuf;
302 bitsleft=8*size;
304 return 1;
308 /*****************************************************************
309 * read next frame return number of frames read.
311 LOCAL int read_frame(struct frame *fr){
312 uint32_t newhead;
313 union {
314 unsigned char buf[8];
315 unsigned long dummy; // for alignment
316 } hbuf;
317 int skipped,resyncpos;
318 int frames=0;
320 resync:
321 skipped=MP3_fpos;
322 resyncpos=MP3_fpos;
324 set_pointer(512);
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!
329 #ifdef DEBUG_RESYNC
330 printf("ReSync: searching for a valid header... (pos=%X)\n",MP3_fpos);
331 #endif
332 retry1:
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
338 #ifdef DEBUG_RESYNC
339 printf("ReSync: found valid hdr at %X fsize=%ld ",resyncpos,fr->framesize);
340 #endif
341 if(!stream_read_frame_body(fr->framesize)) return 0; // read body
342 set_pointer(512);
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...
347 #ifdef DEBUG_RESYNC
348 printf("INVALID\n");
349 #endif
350 // mp3_seek(resyncpos+1);
351 if(!stream_head_read(hbuf.buf,&newhead)) return 0;
352 goto retry1;
354 #ifdef DEBUG_RESYNC
355 printf("OK!\n");
356 ++frames;
357 #endif
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);
368 return 0;
370 ++frames;
372 if(MP3_resync){
373 MP3_resync=0;
374 if(frames==1) goto resync;
377 return frames;
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 *);
388 #include "layer2.c"
389 #include "layer3.c"
390 #include "layer1.c"
392 #include "cpudetect.h"
394 // Init decoder tables. Call first, once!
395 #ifdef CONFIG_FAKE_MONO
396 void MP3_Init(int fakemono){
397 #else
398 void MP3_Init(void){
399 #endif
401 //gCpuCaps.hasMMX=gCpuCaps.hasMMX2=gCpuCaps.hasSSE=0; // for testing!
403 _has_mmx = 0;
404 dct36_func = dct36;
406 make_decode_tables(outscale);
408 #if HAVE_MMX
409 if (gCpuCaps.hasMMX)
411 _has_mmx = 1;
412 synth_func = synth_1to1_MMX;
414 #endif
416 #if HAVE_AMD3DNOWEXT
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");
423 else
424 #endif
425 #if HAVE_AMD3DNOW
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");
432 else
433 #endif
434 #if HAVE_SSE
435 if (gCpuCaps.hasSSE)
437 dct64_MMX_func = dct64_sse;
438 mp_msg(MSGT_DECAUDIO,MSGL_V,"mp3lib: using SSE optimized decore!\n");
440 else
441 #endif
442 #if ARCH_X86_32
443 #if HAVE_MMX
444 if (gCpuCaps.hasMMX)
446 dct64_MMX_func = dct64_MMX;
447 mp_msg(MSGT_DECAUDIO,MSGL_V,"mp3lib: using MMX optimized decore!\n");
449 else
450 #endif
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");
456 else
457 #endif /* ARCH_X86_32 */
458 #if HAVE_ALTIVEC
459 if (gCpuCaps.hasAltiVec)
461 mp_msg(MSGT_DECAUDIO,MSGL_V,"mp3lib: using AltiVec optimized decore!\n");
463 else
464 #endif
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
471 if (fakemono == 1)
472 fr.synth=synth_1to1_l;
473 else if (fakemono == 2)
474 fr.synth=synth_1to1_r;
475 else
476 fr.synth=synth_1to1;
477 #else
478 fr.synth=synth_1to1;
479 #endif
480 fr.synth_mono=synth_1to1_mono2stereo;
481 fr.down_sample=0;
482 fr.down_sample_sblimit = SBLIMIT>>(fr.down_sample);
484 init_layer2();
485 init_layer3(fr.down_sample_sblimit);
486 mp_msg(MSGT_DECAUDIO,MSGL_V,"MP3lib: init layer2&3 finished, tables done\n");
489 #if 0
491 void MP3_Close(void){
492 MP3_eof=1;
493 if(mp3_file) fclose(mp3_file);
494 mp3_file=NULL;
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
502 MP3_frames=0;
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
516 return MP3_filesize;
519 #endif
521 // Read & decode a single frame. Called by sound driver.
522 int MP3_DecodeFrame(unsigned char *hova,short single){
523 pcm_sample = hova;
524 pcm_point = 0;
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 */
528 fr.single=single;
529 switch(fr.lay){
530 case 2: do_layer2(&fr,single);break;
531 case 3: do_layer3(&fr,single);break;
532 case 1: do_layer1(&fr,single);break;
533 default:
534 return 0; // unsupported
536 // ++MP3_frames;
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",
553 fr.emphasis);
556 #if 0
557 #include "genre.h"
559 // Read & print ID3 TAG. Do not call when playing!!! returns filesize.
560 int MP3_PrintTAG(void){
561 struct id3tag {
562 char tag[3];
563 char title[30];
564 char artist[30];
565 char album[30];
566 char year[4];
567 char comment[30];
568 unsigned char genre;
570 struct id3tag tag;
571 char title[31]={0,};
572 char artist[31]={0,};
573 char album[31]={0,};
574 char year[5]={0,};
575 char comment[31]={0,};
576 char genre[31]={0,};
577 int fsize;
578 int ret;
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);
594 } else {
595 strncpy(genre,"Unknown",30);
598 // printf("\n");
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);
602 printf("\n");
603 return fsize-128;
606 #endif