#include #include #include "mp3play.h" #define MAXFRAMESIZE 1792 #define SKIP_JUNK 1 int table_select[2][3][16] = { { {0,32,64,96,128,160,192,224,256,288,320,352,384,416,448,}, {0,32,48,56, 64, 80, 96,112,128,160,192,224,256,320,384,}, {0,32,40,48, 56, 64, 80, 96,112,128,160,192,224,256,320,} }, { {0,32,48,56,64,80,96,112,128,144,160,176,192,224,256,}, {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,}, {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,} } }; long freqs[7] = { 44100, 48000, 32000, 22050, 24000, 16000 , 11025 }; static int bitindex; static unsigned char *wordpointer; static int fsize=0,fsizeold=0,ssize; static unsigned char bsspace[2][MAXFRAMESIZE+512]; /* MAXFRAMESIZE */ static unsigned char *bsbuf=bsspace[1],*bsbufold; static int bsnum=0; unsigned char *pcm_sample; int pcm_point = 0; int audiobufsize = AUDIOBUFSIZE; static FILE *filept; static int filept_opened; void audio_flush(int outmode, struct audio_info_struct *ai) { if (pcm_point) { audio_play_samples (ai, pcm_sample, pcm_point); pcm_point = 0; } } static unsigned long oldhead = 0; static unsigned long firsthead=0; void read_frame_init (void) { oldhead = 0; firsthead = 0; } #define HDRCMPMASK 0xfffffd00 #if 0 #define HDRCMPMASK 0xfffffdft #endif int read_frame(struct frame *fr) { static unsigned long newhead; static unsigned char ssave[34]; unsigned char hbuf[8]; static int framesize; static int halfphase = 0; int l; int try = 0; read_again: if(fread(hbuf,1,4,filept) != 4) return 0; newhead = ((unsigned long) hbuf[0] << 24) | ((unsigned long) hbuf[1] << 16) | ((unsigned long) hbuf[2] << 8) | (unsigned long) hbuf[3]; if(oldhead != newhead || !oldhead) { fr->header_change = 1; init_resync: if( (newhead & 0xffe00000) != 0xffe00000) { if(!firsthead) { int i; /* I even saw RIFF headers at the beginning of MPEG streams ;( */ if(newhead == ('R'<<24)+('I'<<16)+('F'<<8)+'F') { char buf[40]; fprintf(stderr,"Skipped RIFF header\n"); fread(buf,1,68,filept); goto read_again; } /* give up after 1024 bytes */ for(i=0;i<1024;i++) { memmove (&hbuf[0], &hbuf[1], 3); if(fread(hbuf+3,1,1,filept) != 1) return 0; newhead <<= 8; newhead |= hbuf[3]; newhead &= 0xffffffff; goto init_resync; } fprintf(stderr,"Giving up searching valid MPEG header\n"); return 0; } fprintf(stderr,"Illegal Audio-MPEG-Header 0x%08lx at offset 0x%lx.\n", newhead,ftell(filept)-4); if (tryresync) { /* Read more bytes until we find something that looks reasonably like a valid header. This is not a perfect strategy, but it should get us back on the track within a short time (and hopefully without too much distortion in the audio output). */ do { try++; memmove (&hbuf[0], &hbuf[1], 7); if (fread(&hbuf[3],1,1,filept) != 1) return 0; /* This is faster than combining newhead from scratch */ newhead = ((newhead << 8) | hbuf[3]) & 0xffffffff; if (!oldhead) goto init_resync; /* "considered harmful", eh? */ } while ((newhead & HDRCMPMASK) != (oldhead & HDRCMPMASK) && (newhead & HDRCMPMASK) != (firsthead & HDRCMPMASK)); fprintf (stderr, "Skipped %d bytes in input.\n", try); } else return (0); } if (!firsthead) firsthead = newhead; if( newhead & (1<<20) ) { fr->lsf = (newhead & (1<<19)) ? 0x0 : 0x1; fr->mpeg25 = 0; } else { fr->lsf = 1; fr->mpeg25 = 1; } if (!tryresync || !oldhead) { /* If "tryresync" is true, assume that certain parameters do not change within the stream! */ fr->lay = 4-((newhead>>17)&3); fr->bitrate_index = ((newhead>>12)&0xf); if( ((newhead>>10)&0x3) == 0x3) { fprintf(stderr,"Stream error\n"); return -1; } if(fr->mpeg25) fr->sampling_frequency = 6; else fr->sampling_frequency = ((newhead>>10)&0x3) + (fr->lsf*3); fr->error_protection = ((newhead>>16)&0x1)^0x1; } fr->padding = ((newhead>>9)&0x1); fr->extension = ((newhead>>8)&0x1); fr->mode = ((newhead>>6)&0x3); fr->mode_ext = ((newhead>>4)&0x3); fr->copyright = ((newhead>>3)&0x1); fr->original = ((newhead>>2)&0x1); fr->emphasis = newhead & 0x3; fr->stereo = (fr->mode == MPG_MD_MONO) ? 1 : 2; oldhead = newhead; if(!fr->bitrate_index) { fprintf(stderr,"Free format not supported.\n"); return (0); } if (fr->lay != 3) { fprintf(stderr,"Non MP3 file format detected.\nExiting...\n"); exit(1); } fr->do_layer = do_layer3; if(fr->lsf) ssize = (fr->stereo == 1) ? 9 : 17; else ssize = (fr->stereo == 1) ? 17 : 32; if(fr->error_protection) ssize += 2; framesize = (long) table_select[fr->lsf][2][fr->bitrate_index] * 144000; framesize /= freqs[fr->sampling_frequency]<<(fr->lsf); framesize = framesize + fr->padding - 4; } else fr->header_change = 0; fsizeold=fsize; bsbufold = bsbuf; bsbuf = bsspace[bsnum]+512; bsnum = (bsnum + 1) & 1; fsize = framesize; if( (l=fread(bsbuf,1,fsize,filept)) != fsize) { if(l <= 0) return 0; memset(bsbuf+l,0,fsize-l); } bitindex = 0; wordpointer = (unsigned char *) bsbuf; return 1; } void print_header(struct frame *fr) { static char *modes[4] = { "Stereo", "Joint-Stereo", "Dual-Channel", "Single-Channel" }; static char *layers[4] = { "Unknown" , "I", "II", "III" }; fprintf(stderr,"MPEG %s, Layer: %s, Freq: %ld, mode: %s, modext: %d, BPF: %d\n", fr->mpeg25 ? "2.5" : (fr->lsf ? "2.0" : "1.0"), layers[fr->lay],freqs[fr->sampling_frequency], modes[fr->mode],fr->mode_ext,fsize+4); fprintf(stderr,"Channels: %d, copyright: %s, original: %s, CRC: %s, emphasis: %d.\n", fr->stereo,fr->copyright?"Yes":"No", fr->original?"Yes":"No",fr->error_protection?"Yes":"No", fr->emphasis); fprintf(stderr,"Bitrate: %d Kbits/s, Extension value: %d\n", table_select[fr->lsf][fr->lay-1][fr->bitrate_index],fr->extension); } void print_header_compact(struct frame *fr) { static char *modes[4] = { "stereo", "joint-stereo", "dual-channel", "mono" }; static char *layers[4] = { "Unknown" , "I", "II", "III" }; fprintf(stderr,"MPEG %s layer %s, %d kbit/s, %ld Hz %s\n", fr->mpeg25 ? "2.5" : (fr->lsf ? "2.0" : "1.0"), layers[fr->lay], table_select[fr->lsf][fr->lay-1][fr->bitrate_index], freqs[fr->sampling_frequency], modes[fr->mode]); } char *strndup (const char *src, int num) { char *dst; if (!(dst = (char *) malloc(num+1))) return (NULL); dst[num] = '\0'; return (strncpy(dst, src, num)); } /* open the device to read the bit stream from it */ int open_stream(const char *bs_filenam,int fd) { filept_opened = 1; if (!bs_filenam) { if(fd < 0) { filept = stdin; filept_opened = 0; } else filept = fdopen(fd,"r"); } else if (!(filept = fopen(bs_filenam, "rb"))) { perror (bs_filenam); return -1; } return 0; } /*close the device containing the bit stream after a read process*/ void close_stream(void) { if (filept_opened) fclose(filept); } unsigned int getbits(int number_of_bits) { unsigned long rval; if(!number_of_bits) return 0; { rval = wordpointer[0]; rval <<= 8; rval |= wordpointer[1]; rval <<= 8; rval |= wordpointer[2]; rval <<= bitindex; rval &= 0xffffff; bitindex += number_of_bits; rval >>= (24-number_of_bits); wordpointer += (bitindex>>3); bitindex &= 7; } return rval; } unsigned int getbits_fast(int number_of_bits) { unsigned long rval; { rval = wordpointer[0]; rval <<= 8; rval |= wordpointer[1]; rval <<= bitindex; rval &= 0xffff; bitindex += number_of_bits; rval >>= (16-number_of_bits); wordpointer += (bitindex>>3); bitindex &= 7; } return rval; } unsigned int get1bit(void) { unsigned char rval; rval = *wordpointer << bitindex; bitindex++; wordpointer += (bitindex>>3); bitindex &= 7; return rval>>7; } void set_pointer(long backstep) { wordpointer = bsbuf + ssize - backstep; if (backstep) memcpy(wordpointer,bsbufold+fsizeold-backstep,backstep); bitindex = 0; }