21
#define MAP_FAILED ( (void *) -1 )
29
int tabsel_123[2][3][16] = {
30
{ {0,32,64,96,128,160,192,224,256,288,320,352,384,416,448,},
31
{0,32,48,56, 64, 80, 96,112,128,160,192,224,256,320,384,},
32
{0,32,40,48, 56, 64, 80, 96,112,128,160,192,224,256,320,} },
34
{ {0,32,48,56,64,80,96,112,128,144,160,176,192,224,256,},
35
{0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,},
36
{0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,} }
39
long freqs[9] = { 44100, 48000, 32000, 22050, 24000, 16000 , 11025 , 12000 , 8000 };
41
struct bitstream_info bsi;
43
static int bsbufend[2]= { 0,0 };
44
static int bsbufold_end;
45
static unsigned char bsspace[2][MAXFRAMESIZE+512]; /* MAXFRAMESIZE */
46
static unsigned char *bsbuf=bsspace[1],*bsbufold;
49
static int skip_riff(struct reader *);
50
static int skip_new_id3(struct reader *);
52
unsigned char *pcm_sample;
54
int audiobufsize = AUDIOBUFSIZE;
56
static int decode_header(struct frame *fr,unsigned long newhead);
58
void safewrite(int fd, const void *buf, size_t count) {
60
while(donesofar < count) {
62
char *p = (char*) buf + donesofar;
63
retval = write(fd,(void*) p,(count-donesofar));
65
if((errno != EINTR) && (errno != EAGAIN))
66
exit(fprintf(stderr,"exception on output!\n"));
72
void audio_flush(int outmode, struct audio_info_struct *ai)
76
#ifndef NO_DECODE_FILE
78
safewrite (OutputDescriptor, pcm_sample, pcm_point);
81
#ifndef NO_DECODE_AUDIO
83
audio_play_samples (ai, pcm_sample, pcm_point);
88
safewrite (buffer_fd[1], pcm_sample, pcm_point);
95
wav_write(pcm_sample, pcm_point);
105
#if !defined(WIN32) && !defined(GENERIC)
106
void (*catchsignal(int signum, void(*handler)()))()
108
struct sigaction new_sa;
109
struct sigaction old_sa;
111
#ifdef DONT_CATCH_SIGNALS
112
printf ("Not catching any signals.\n");
113
return ((void (*)()) -1);
116
new_sa.sa_handler = handler;
117
sigemptyset(&new_sa.sa_mask);
119
if (sigaction(signum, &new_sa, &old_sa) == -1)
120
return ((void (*)()) -1);
121
return (old_sa.sa_handler);
125
void read_frame_init (struct frame *fr)
129
fr->freeformatsize = 0;
132
int head_check(unsigned long head)
134
if( (head & 0xffe00000) != 0xffe00000)
138
if( ((head>>12)&0xf) == 0xf)
140
if( ((head>>10)&0x3) == 0x3 )
147
* return 0: EOF or other stream error
151
#define MAX_INPUT_FRAMESIZE 1920
152
#define SYNC_HEAD_MASK 0xffff0000
153
#define SYNC_HEAD_MASK_FF 0x0000f000
154
#define LOOK_AHEAD_NUM 3
155
#define SCAN_LENGTH 16384
157
#define CHECK_FOR_RIFF 0x0001
158
#define CHECK_FOR_ID3_V1 0x0002
159
#define CHECK_FOR_ID3_V2 0x0004
161
int sync_stream(struct reader *rds,struct frame *fr,int flags,int *skipped)
164
unsigned long firsthead,nexthead;
165
struct frame frameInfo,nextInfo;
166
unsigned char dummybuf[MAX_INPUT_FRAMESIZE];
168
int freeformatsize=0;
170
for(i=0;i<SCAN_LENGTH;i++) {
172
readers_mark_pos(rds); /* store our current position */
174
if(!rds->head_read(rds,&firsthead))
177
/* first a few simple checks */
178
if( !head_check(firsthead) || !decode_header(&frameInfo,firsthead) ) {
180
/* Check for RIFF Headers */
181
if( (flags & CHECK_FOR_RIFF) && firsthead == ('R'<<24)+('I'<<16)+('F'<<8)+'F') {
182
fprintf(stderr,"Found RIFF Header\n");
183
ret = skip_riff(rds);
184
if(ret > 0) { /* RIFF was OK continue with next byte */
192
/* Check for old ID3 Header (or better Footer ;) */
193
if( (flags & CHECK_FOR_ID3_V1) && (firsthead>>8) == ('T'<<16)+('A'<<8)+'G') {
194
fprintf(stderr,"Found old ID3 Header\n");
197
/* Check for new ID3 header */
198
if( (flags & CHECK_FOR_ID3_V2) && (firsthead>>8) == ('I'<<16)+('D'<<8)+'3') {
199
if( (firsthead & 0xff) != 0xff) {
200
fprintf(stderr,"Found new ID3 Header\n");
201
ret = skip_new_id3(rds);
211
readers_goto_mark(rds); /* reset to old mark and continue */
212
if(!rds->read_frame_body(rds,dummybuf,1))
223
* At the first free format paket we do not know the size
225
if(frameInfo.bitrate_index == 0) {
226
int maxframesize = MAX_INPUT_FRAMESIZE; /* FIXME depends on layer and sampling freq */
228
fprintf(stderr,"Searching for next FF header\n");
230
if(!rds->head_read(rds,&nexthead))
233
for(j=0;j<maxframesize;j++) {
234
if(head_check(nexthead) && (nexthead & (SYNC_HEAD_MASK|SYNC_HEAD_MASK_FF) ) == (firsthead & (SYNC_HEAD_MASK|SYNC_HEAD_MASK_FF)) &&
235
decode_header(&nextInfo,nexthead) ) {
237
/* fprintf(stderr,"j: %d %d %d\n",j,frameInfo.padsize,nextInfo.padsize); */
239
freeformatsize = j - frameInfo.padsize;
243
if(!rds->head_shift(rds,&nexthead))
248
if(!rds->read_frame_body(rds,dummybuf,frameInfo.framesize))
251
if(!rds->head_read(rds,&nexthead))
255
fprintf(stderr,"S: %08lx %08lx %d %d %d %d\n",firsthead,nexthead, head_check(nexthead),(nexthead & SYNC_HEAD_MASK) == firsthead,(nexthead & SYNC_HEAD_MASK_FF) != 0x0,decode_header(&nextInfo,nexthead));
258
if( head_check(nexthead) && (nexthead & SYNC_HEAD_MASK) == (firsthead & SYNC_HEAD_MASK) &&
259
(nexthead & SYNC_HEAD_MASK_FF) != 0x0 && decode_header(&nextInfo,nexthead)) {
265
readers_goto_mark(rds); /* reset to old mark and continue */
266
if(!rds->read_frame_body(rds,dummybuf,1))
273
fprintf(stderr,"s: %08lx %08lx %d %d %d\n",firsthead,nexthead,frameInfo.framesize,nextInfo.framesize,freeformatsize);
276
/* check some more frames */
277
for(l=0;l<LOOK_AHEAD_NUM;l++) {
280
if( freeformatsize > 0 ) {
281
size = freeformatsize + nextInfo.padsize;
284
size = nextInfo.framesize;
287
if(!rds->read_frame_body(rds,dummybuf,size))
290
if(!rds->head_read(rds,&nexthead))
293
if(!head_check(nexthead) ||
294
(nexthead & SYNC_HEAD_MASK) != (firsthead & SYNC_HEAD_MASK) ||
295
!decode_header(&nextInfo,nexthead) ) {
299
if( freeformatsize > 0) {
300
if( ( nexthead & SYNC_HEAD_MASK_FF ) != 0x0) {
306
if( (nexthead & SYNC_HEAD_MASK_FF) == 0x0) {
316
readers_goto_mark(rds); /* reset to old mark and continue */
317
if(!rds->read_frame_body(rds,dummybuf,1)) /* skip first byte */
326
readers_goto_mark(rds);
327
fr->freeformatsize = freeformatsize;
328
fr->firsthead = firsthead;
335
* skips the RIFF header at the beginning
337
* returns: 0 = read-error
338
* -1/-2 = illegal RIFF header (= -2 backstep not valid)
339
* 1 = skipping succeeded
341
static int skip_riff(struct reader *rds)
343
unsigned long length;
344
unsigned char buf[16];
346
if(!rds->read_frame_body(rds,buf,16)) /* read header information */
349
if( strncmp("WAVEfmt ",(char *)buf+4,8) ) /* check 2. signature */
352
length = (unsigned long) buf[12] + /* decode the header length */
353
(((unsigned long) buf[13])<<8) +
354
(((unsigned long) buf[14])<<16) +
355
(((unsigned long) buf[15])<<24);
357
if(!rds->skip_bytes(rds,length)) /* will not store data in backbuff! */
360
if(!rds->read_frame_body(rds,buf,8)) /* skip "data" plus length */
363
if(strncmp("data",(char *)buf,4))
370
* skips the ID3 header at the beginning
372
* returns: 0 = read-error
373
* -1 = illegal ID3 header
374
* 1 = skipping succeeded
376
static int skip_new_id3(struct reader *rds)
378
unsigned long length;
379
unsigned char buf[6];
381
if(!rds->read_frame_body(rds,buf,6)) /* read more header information */
387
if( (buf[2]|buf[3]|buf[4]|buf[5]) & 0x80)
390
length = (unsigned long) buf[2] & 0x7f;
392
length += (unsigned long) buf[3] & 0x7f;
394
length += (unsigned long) buf[4] & 0x7f;
396
length += (unsigned long) buf[5] & 0x7f;
398
if(!rds->skip_bytes(rds,length)) /* will not store data in backbuff! */
409
/*****************************************************************
412
int read_frame(struct reader *rds,struct frame *fr)
414
unsigned long newhead,oldhead;
415
static unsigned char ssave[34];
417
oldhead = fr->thishead;
419
if (param.halfspeed) {
420
static int halfphase = 0;
423
bsi.wordpointer = (unsigned char *) bsbuf;
425
memcpy (bsbuf, ssave, fr->sideInfoSize);
429
halfphase = param.halfspeed - 1;
434
if(!rds->head_read(rds,&newhead))
438
fprintf(stderr,"n %08lx",newhead);
441
if( !head_check(newhead) || !decode_header(fr,newhead) ) {
443
fprintf(stderr,"Illegal Audio-MPEG-Header 0x%08lx at offset 0x%lx.\n",
444
newhead,rds->tell(rds)-4);
446
if(param.tryresync) {
448
readers_pushback_header(rds,newhead);
449
if(sync_stream(rds,fr,0xffff,&try) <= 0)
452
fprintf (stderr, "Skipped %d bytes in input.\n", try);
462
fprintf(stderr,"N %08lx",newhead);
465
fr->header_change = 2;
467
if((oldhead & 0xc00) == (fr->thishead & 0xc00)) {
468
if( (oldhead & 0xc0) == 0 && (fr->thishead & 0xc0) == 0)
469
fr->header_change = 1;
470
else if( (oldhead & 0xc0) > 0 && (fr->thishead & 0xc0) > 0)
471
fr->header_change = 1;
476
if(!fr->bitrate_index) {
477
fr->framesize = fr->freeformatsize + fr->padsize;
481
fprintf(stderr,"Reading %d\n",fr->framesize);
484
/* flip/init buffer for Layer 3 */
485
/* FIXME for reentrance */
487
bsbufold_end = bsbufend[bsnum];
488
bsbuf = bsspace[bsnum]+512;
489
bsnum = (bsnum + 1) & 1;
490
bsbufend[bsnum] = fr->framesize;
492
/* read main data into memory */
493
if(!rds->read_frame_body(rds,bsbuf,fr->framesize))
498
static struct vbrHeader head;
499
static int vbr = 0; /* FIXME */
501
getVBRHeader(&head,bsbuf,fr);
507
bsi.wordpointer = (unsigned char *) bsbuf;
509
if (param.halfspeed && fr->lay == 3)
510
memcpy (ssave, bsbuf, fr->sideInfoSize);
516
* decode a header and write the information
517
* into the frame structure
519
static int decode_header(struct frame *fr,unsigned long newhead)
521
if(!head_check(newhead)) {
522
fprintf(stderr,"Oopps header is wrong %08lx\n",newhead);
526
if( newhead & (1<<20) ) {
527
fr->lsf = (newhead & (1<<19)) ? 0x0 : 0x1;
536
* CHECKME: should be add more consistency checks here ?
537
* changed layer, changed CRC bit, changed sampling frequency
540
fr->lay = 4-((newhead>>17)&3);
541
if( ((newhead>>10)&0x3) == 0x3) {
542
fprintf(stderr,"Stream error\n");
546
fr->sampling_frequency = 6 + ((newhead>>10)&0x3);
549
fr->sampling_frequency = ((newhead>>10)&0x3) + (fr->lsf*3);
550
fr->error_protection = ((newhead>>16)&0x1)^0x1;
553
fr->bitrate_index = ((newhead>>12)&0xf);
554
fr->padding = ((newhead>>9)&0x1);
555
fr->extension = ((newhead>>8)&0x1);
556
fr->mode = ((newhead>>6)&0x3);
557
fr->mode_ext = ((newhead>>4)&0x3);
558
fr->copyright = ((newhead>>3)&0x1);
559
fr->original = ((newhead>>2)&0x1);
560
fr->emphasis = newhead & 0x3;
562
fr->stereo = (fr->mode == MPG_MD_MONO) ? 1 : 2;
566
fr->framesize = (long) tabsel_123[fr->lsf][0][fr->bitrate_index] * 12000;
567
fr->framesize /= freqs[fr->sampling_frequency];
568
fr->framesize = ((fr->framesize+fr->padding)<<2)-4;
569
fr->sideInfoSize = 0;
570
fr->padsize = fr->padding << 2;
573
fr->framesize = (long) tabsel_123[fr->lsf][1][fr->bitrate_index] * 144000;
574
fr->framesize /= freqs[fr->sampling_frequency];
575
fr->framesize += fr->padding - 4;
576
fr->sideInfoSize = 0;
577
fr->padsize = fr->padding;
581
fr->sideInfoSize = (fr->stereo == 1) ? 9 : 17;
583
fr->sideInfoSize = (fr->stereo == 1) ? 17 : 32;
584
if(fr->error_protection)
585
fr->sideInfoSize += 2;
586
fr->framesize = (long) tabsel_123[fr->lsf][2][fr->bitrate_index] * 144000;
587
fr->framesize /= freqs[fr->sampling_frequency]<<(fr->lsf);
588
fr->framesize = fr->framesize + fr->padding - 4;
589
fr->padsize = fr->padding;
592
fprintf(stderr,"Sorry, unknown layer type.\n");
596
if(!fr->bitrate_index) {
597
/* fprintf(stderr,"Warning, Free format not heavily tested: (head %08lx)\n",newhead); */
600
fr->thishead = newhead;
606
void print_rheader(struct frame *fr)
608
static char *modes[4] = { "Stereo", "Joint-Stereo", "Dual-Channel", "Single-Channel" };
609
static char *layers[4] = { "Unknown" , "I", "II", "III" };
610
static char *mpeg_type[2] = { "1.0" , "2.0" };
612
/* version, layer, freq, mode, channels, bitrate, BPF */
613
fprintf(stderr,"@I %s %s %ld %s %d %d %d\n",
614
mpeg_type[fr->lsf],layers[fr->lay],freqs[fr->sampling_frequency],
615
modes[fr->mode],fr->stereo,
616
tabsel_123[fr->lsf][fr->lay-1][fr->bitrate_index],
621
void print_header(struct frame *fr)
623
static char *modes[4] = { "Stereo", "Joint-Stereo", "Dual-Channel", "Single-Channel" };
624
static char *layers[4] = { "Unknown" , "I", "II", "III" };
626
fprintf(stderr,"MPEG %s, Layer: %s, Freq: %ld, mode: %s, modext: %d, BPF : %d\n",
627
fr->mpeg25 ? "2.5" : (fr->lsf ? "2.0" : "1.0"),
628
layers[fr->lay],freqs[fr->sampling_frequency],
629
modes[fr->mode],fr->mode_ext,fr->framesize+4);
630
fprintf(stderr,"Channels: %d, copyright: %s, original: %s, CRC: %s, emphasis: %d.\n",
631
fr->stereo,fr->copyright?"Yes":"No",
632
fr->original?"Yes":"No",fr->error_protection?"Yes":"No",
634
fprintf(stderr,"Bitrate: %d Kbits/s, Extension value: %d\n",
635
tabsel_123[fr->lsf][fr->lay-1][fr->bitrate_index],fr->extension);
638
void print_header_compact(struct frame *fr)
640
static char *modes[4] = { "stereo", "joint-stereo", "dual-channel", "mono" };
641
static char *layers[4] = { "Unknown" , "I", "II", "III" };
643
fprintf(stderr,"MPEG %s layer %s, %d kbit/s, %ld Hz %s\n",
644
fr->mpeg25 ? "2.5" : (fr->lsf ? "2.0" : "1.0"),
646
tabsel_123[fr->lsf][fr->lay-1][fr->bitrate_index],
647
freqs[fr->sampling_frequency], modes[fr->mode]);
650
void print_id3_tag(unsigned char *buf)
661
struct id3tag *tag = (struct id3tag *) buf;
663
char artist[31]={0,};
666
char comment[31]={0,};
672
strncpy(title,tag->title,30);
673
strncpy(artist,tag->artist,30);
674
strncpy(album,tag->album,30);
675
strncpy(year,tag->year,4);
676
strncpy(comment,tag->comment,30);
678
if ( tag->genre < sizeof(genre_table)/sizeof(*genre_table) ) {
679
strncpy(genre, genre_table[tag->genre], 30);
681
strncpy(genre,"Unknown",30);
684
fprintf(stderr,"Title : %-30s Artist: %s\n",title,artist);
685
fprintf(stderr,"Album : %-30s Year : %4s\n",album,year);
686
fprintf(stderr,"Comment: %-30s Genre : %s\n",comment,genre);
690
/* removed the strndup for better portability */
692
* Allocate space for a new string containing the first
693
* "num" characters of "src". The resulting string is
694
* always zero-terminated. Returns NULL if malloc fails.
696
char *strndup (const char *src, int num)
700
if (!(dst = (char *) malloc(num+1)))
703
return (strncpy(dst, src, num));
708
* Split "path" into directory and filename components.
710
* Return value is 0 if no directory was specified (i.e.
711
* "path" does not contain a '/'), OR if the directory
712
* is the same as on the previous call to this function.
714
* Return value is 1 if a directory was specified AND it
715
* is different from the previous one (if any).
718
int split_dir_file (const char *path, char **dname, char **fname)
720
static char *lastdir = NULL;
723
if ((slashpos = strrchr(path, '/'))) {
724
*fname = slashpos + 1;
725
*dname = strdup(path); /* , 1 + slashpos - path); */
730
(*dname)[1 + slashpos - path] = 0;
731
if (lastdir && !strcmp(lastdir, *dname)) {
732
/*** same as previous directory ***/
738
/*** different directory ***/
746
/*** no directory specified ***/
752
*fname = (char *)path;
757
void set_pointer(int ssize,long backstep)
759
bsi.wordpointer = bsbuf + ssize - backstep;
761
memcpy(bsi.wordpointer,bsbufold+bsbufold_end-backstep,backstep);
765
/********************************/
767
double compute_bpf(struct frame *fr)
771
if(!fr->bitrate_index) {
772
return fr->freeformatsize + 4;
777
bpf = tabsel_123[fr->lsf][0][fr->bitrate_index];
778
bpf *= 12000.0 * 4.0;
779
bpf /= freqs[fr->sampling_frequency] <<(fr->lsf);
783
bpf = tabsel_123[fr->lsf][fr->lay-1][fr->bitrate_index];
785
bpf /= freqs[fr->sampling_frequency] << (fr->lsf);
794
double compute_tpf(struct frame *fr)
796
static int bs[4] = { 0,384,1152,1152 };
799
tpf = (double) bs[fr->lay];
800
tpf /= freqs[fr->sampling_frequency] << (fr->lsf);
805
* Returns number of frames queued up in output buffer, i.e.
806
* offset between currently played and currently decoded frame.
810
long compute_buffer_offset(struct frame *fr)
815
* buffermem->buf[0] holds output sampling rate,
816
* buffermem->buf[1] holds number of channels,
817
* buffermem->buf[2] holds audio format of output.
820
if(!param.usebuffer || !(bufsize=xfermem_get_usedspace(buffermem))
821
|| !buffermem->buf[0] || !buffermem->buf[1])
824
bufsize = (long)((double) bufsize / buffermem->buf[0] /
825
buffermem->buf[1] / compute_tpf(fr));
827
if((buffermem->buf[2] & AUDIO_FORMAT_MASK) == AUDIO_FORMAT_16)
834
void print_stat(struct reader *rds,struct frame *fr,int no,long buffsize,struct audio_info_struct *ai)
836
double bpf,tpf,tim1,tim2;
850
int n,errfd = fileno(stderr);
852
t.tv_sec=t.tv_usec=0;
856
n = select(errfd+1,NULL,&serr,NULL,&t);
862
bpf = compute_bpf(fr);
863
tpf = compute_tpf(fr);
865
if(buffsize > 0 && ai && ai->rate > 0 && ai->channels > 0) {
866
dt = (double) buffsize / ai->rate / ai->channels;
867
if( (ai->format & AUDIO_FORMAT_MASK) == AUDIO_FORMAT_16)
873
if(rds->filelen >= 0) {
874
long t = rds->tell(rds);
875
rno = (int)((double)(rds->filelen-t)/bpf);
876
sno = (int)((double)t/bpf);
879
sprintf(outbuf+strlen(outbuf),"\rFrame# %5d [%5d], ",sno,rno);
884
tim1 = tim1 < 0 ? 0.0 : tim1;
886
tim2 = tim2 < 0 ? 0.0 : tim2;
888
sprintf(outbuf+strlen(outbuf),"Time: %02u:%02u.%02u [%02u:%02u.%02u], ",
889
(unsigned int)tim1/60,
890
(unsigned int)tim1%60,
891
(unsigned int)(tim1*100)%100,
892
(unsigned int)tim2/60,
893
(unsigned int)tim2%60,
894
(unsigned int)(tim2*100)%100);
897
sprintf(outbuf+strlen(outbuf),"[%8ld] ",(long)buffsize);
898
write(fileno(stderr),outbuf,strlen(outbuf));
900
fflush(out); /* hmm not really nec. */
904
int get_songlen(struct reader *rds,struct frame *fr,int no)
912
if(!rds || rds->filelen < 0)
914
no = (double) rds->filelen / compute_bpf(fr);
917
tpf = compute_tpf(fr);