~ubuntu-branches/ubuntu/hoary/kdemultimedia/hoary

« back to all changes in this revision

Viewing changes to mpg123_artsplugin/mpg123/common.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Schulze
  • Date: 2003-01-22 15:00:51 UTC
  • Revision ID: james.westby@ubuntu.com-20030122150051-uihwkdoxf15mi1tn
Tags: upstream-2.2.2
ImportĀ upstreamĀ versionĀ 2.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* GPL clean */
 
2
 
 
3
#include <ctype.h>
 
4
#include <stdlib.h>
 
5
#include <signal.h>
 
6
#include <errno.h>
 
7
 
 
8
#include <sys/types.h>
 
9
#include <sys/stat.h>
 
10
#ifdef WIN32
 
11
#include <time.h>
 
12
#else
 
13
#include <sys/time.h>
 
14
#endif
 
15
 
 
16
#include <fcntl.h>
 
17
 
 
18
#ifdef READ_MMAP
 
19
#include <sys/mman.h>
 
20
#ifndef MAP_FAILED
 
21
#define MAP_FAILED ( (void *) -1 )
 
22
#endif
 
23
#endif
 
24
 
 
25
#include "mpg123.h"
 
26
#include "genre.h"
 
27
#include "common.h"
 
28
 
 
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,} },
 
33
 
 
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,} }
 
37
};
 
38
 
 
39
long freqs[9] = { 44100, 48000, 32000, 22050, 24000, 16000 , 11025 , 12000 , 8000 };
 
40
 
 
41
struct bitstream_info bsi;
 
42
 
 
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;
 
47
static int bsnum=0;
 
48
 
 
49
static int skip_riff(struct reader *);
 
50
static int skip_new_id3(struct reader *);
 
51
 
 
52
unsigned char *pcm_sample;
 
53
int pcm_point = 0;
 
54
int audiobufsize = AUDIOBUFSIZE;
 
55
 
 
56
static int decode_header(struct frame *fr,unsigned long newhead);
 
57
 
 
58
void safewrite(int fd, const void *buf, size_t count) {
 
59
       int donesofar = 0;
 
60
       while(donesofar < count) {
 
61
               int retval;
 
62
               char *p = (char*) buf + donesofar;
 
63
               retval = write(fd,(void*) p,(count-donesofar));
 
64
               if(retval == -1) {
 
65
                       if((errno != EINTR) && (errno != EAGAIN))
 
66
                               exit(fprintf(stderr,"exception on output!\n"));
 
67
               } else
 
68
                       donesofar += retval;
 
69
       }
 
70
}
 
71
 
 
72
void audio_flush(int outmode, struct audio_info_struct *ai)
 
73
{
 
74
    if (pcm_point) {
 
75
        switch (outmode) {
 
76
#ifndef NO_DECODE_FILE
 
77
        case DECODE_FILE:
 
78
            safewrite (OutputDescriptor, pcm_sample, pcm_point);
 
79
            break;
 
80
#endif
 
81
#ifndef NO_DECODE_AUDIO
 
82
        case DECODE_AUDIO:
 
83
            audio_play_samples (ai, pcm_sample, pcm_point);
 
84
            break;
 
85
#endif
 
86
#ifndef NOXFERMEM
 
87
        case DECODE_BUFFER:
 
88
            safewrite (buffer_fd[1], pcm_sample, pcm_point);
 
89
            break;
 
90
#endif
 
91
#ifndef NO_DECODE_WAV
 
92
        case DECODE_WAV:
 
93
        case DECODE_CDR:
 
94
        case DECODE_AU:
 
95
            wav_write(pcm_sample, pcm_point);
 
96
            break;
 
97
#endif
 
98
        default:
 
99
            break;
 
100
        }
 
101
        pcm_point = 0;
 
102
    }
 
103
}
 
104
 
 
105
#if !defined(WIN32) && !defined(GENERIC)
 
106
void (*catchsignal(int signum, void(*handler)()))()
 
107
{
 
108
    struct sigaction new_sa;
 
109
    struct sigaction old_sa;
 
110
 
 
111
#ifdef DONT_CATCH_SIGNALS
 
112
    printf ("Not catching any signals.\n");
 
113
    return ((void (*)()) -1);
 
114
#endif
 
115
 
 
116
    new_sa.sa_handler = handler;
 
117
    sigemptyset(&new_sa.sa_mask);
 
118
    new_sa.sa_flags = 0;
 
119
    if (sigaction(signum, &new_sa, &old_sa) == -1)
 
120
        return ((void (*)()) -1);
 
121
    return (old_sa.sa_handler);
 
122
}
 
123
#endif
 
124
 
 
125
void read_frame_init (struct frame *fr)
 
126
{
 
127
    fr->firsthead = 0;
 
128
    fr->thishead = 0;
 
129
    fr->freeformatsize = 0;
 
130
}
 
131
 
 
132
int head_check(unsigned long head)
 
133
{
 
134
    if( (head & 0xffe00000) != 0xffe00000)
 
135
        return FALSE;
 
136
    if(!((head>>17)&3))
 
137
        return FALSE;
 
138
    if( ((head>>12)&0xf) == 0xf)
 
139
        return FALSE;
 
140
    if( ((head>>10)&0x3) == 0x3 )
 
141
        return FALSE;
 
142
 
 
143
    return TRUE;
 
144
}
 
145
 
 
146
/*
 
147
 * return 0: EOF or other stream error
 
148
 *       -1: giving up
 
149
 *        1: synched
 
150
 */
 
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
 
156
 
 
157
#define CHECK_FOR_RIFF   0x0001
 
158
#define CHECK_FOR_ID3_V1 0x0002
 
159
#define CHECK_FOR_ID3_V2 0x0004
 
160
 
 
161
int sync_stream(struct reader *rds,struct frame *fr,int flags,int *skipped)
 
162
{
 
163
   int i,j,l,ret;
 
164
   unsigned long firsthead,nexthead;
 
165
   struct frame frameInfo,nextInfo;
 
166
   unsigned char dummybuf[MAX_INPUT_FRAMESIZE];
 
167
   int found=0;
 
168
   int freeformatsize=0;
 
169
 
 
170
   for(i=0;i<SCAN_LENGTH;i++) {
 
171
 
 
172
     readers_mark_pos(rds);  /* store our current position */
 
173
 
 
174
     if(!rds->head_read(rds,&firsthead))
 
175
        return 0;
 
176
 
 
177
     /* first a few simple checks */ 
 
178
     if( !head_check(firsthead) || !decode_header(&frameInfo,firsthead) ) {
 
179
 
 
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 */
 
185
           *skipped += ret+4;
 
186
           continue;
 
187
         }
 
188
         if(ret == 0)
 
189
            return 0;
 
190
       }
 
191
  
 
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");
 
195
       }
 
196
 
 
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);
 
202
           if(!ret)
 
203
              return 0;
 
204
           if(ret > 0) {
 
205
              *skipped += ret+4;
 
206
              continue;
 
207
           }
 
208
         }
 
209
       }
 
210
 
 
211
       readers_goto_mark(rds); /* reset to old mark and continue */
 
212
       if(!rds->read_frame_body(rds,dummybuf,1))
 
213
         return 0;
 
214
 
 
215
       (*skipped)++;
 
216
       continue;
 
217
     }
 
218
 
 
219
     found = 0;
 
220
     freeformatsize = 0;
 
221
 
 
222
     /*
 
223
      * At the first free format paket we do not know the size
 
224
      */
 
225
     if(frameInfo.bitrate_index == 0) {
 
226
        int maxframesize = MAX_INPUT_FRAMESIZE; /* FIXME depends on layer and sampling freq */
 
227
 
 
228
fprintf(stderr,"Searching for next FF header\n");
 
229
 
 
230
        if(!rds->head_read(rds,&nexthead))
 
231
           return 0;
 
232
 
 
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) ) {
 
236
 
 
237
/* fprintf(stderr,"j: %d %d %d\n",j,frameInfo.padsize,nextInfo.padsize); */
 
238
 
 
239
               freeformatsize = j - frameInfo.padsize;
 
240
               found = 1;
 
241
               break;
 
242
           }
 
243
           if(!rds->head_shift(rds,&nexthead))
 
244
             return 0;
 
245
        }
 
246
     }
 
247
     else {
 
248
        if(!rds->read_frame_body(rds,dummybuf,frameInfo.framesize))
 
249
           return 0;
 
250
            
 
251
        if(!rds->head_read(rds,&nexthead))
 
252
           return 0;
 
253
 
 
254
/*
 
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));
 
256
*/
 
257
 
 
258
        if( head_check(nexthead) && (nexthead & SYNC_HEAD_MASK) == (firsthead & SYNC_HEAD_MASK) && 
 
259
            (nexthead & SYNC_HEAD_MASK_FF) != 0x0 && decode_header(&nextInfo,nexthead))  {
 
260
              found = 1;
 
261
        }
 
262
     }
 
263
 
 
264
     if(!found) {
 
265
       readers_goto_mark(rds); /* reset to old mark and continue */
 
266
       if(!rds->read_frame_body(rds,dummybuf,1))
 
267
         return 0;
 
268
       (*skipped)++;
 
269
       continue;
 
270
     }
 
271
 
 
272
/*
 
273
fprintf(stderr,"s: %08lx %08lx %d %d %d\n",firsthead,nexthead,frameInfo.framesize,nextInfo.framesize,freeformatsize);
 
274
*/
 
275
 
 
276
     /* check some more frames */
 
277
     for(l=0;l<LOOK_AHEAD_NUM;l++) {
 
278
        int size;
 
279
        
 
280
        if( freeformatsize > 0 ) {
 
281
          size = freeformatsize + nextInfo.padsize;
 
282
        }
 
283
        else
 
284
          size = nextInfo.framesize;
 
285
 
 
286
        /* step over data */
 
287
        if(!rds->read_frame_body(rds,dummybuf,size))
 
288
          return 0;
 
289
 
 
290
        if(!rds->head_read(rds,&nexthead))
 
291
           return 0;
 
292
 
 
293
        if(!head_check(nexthead) || 
 
294
           (nexthead & SYNC_HEAD_MASK) != (firsthead & SYNC_HEAD_MASK) ||
 
295
           !decode_header(&nextInfo,nexthead) )  {
 
296
           found = 0;
 
297
           break;
 
298
        }
 
299
        if( freeformatsize > 0) {
 
300
          if( ( nexthead & SYNC_HEAD_MASK_FF ) != 0x0) {
 
301
            found = 0;
 
302
            break;
 
303
          }
 
304
        }
 
305
        else {
 
306
          if( (nexthead & SYNC_HEAD_MASK_FF) == 0x0) {
 
307
            found = 0;
 
308
            break;
 
309
          } 
 
310
        }
 
311
     }
 
312
    
 
313
     if(found)
 
314
         break; 
 
315
 
 
316
     readers_goto_mark(rds); /* reset to old mark and continue */
 
317
     if(!rds->read_frame_body(rds,dummybuf,1)) /* skip first byte */
 
318
       return 0;
 
319
     (*skipped)++;
 
320
 
 
321
   }
 
322
 
 
323
   if(i == SCAN_LENGTH)
 
324
     return -1;
 
325
 
 
326
   readers_goto_mark(rds);
 
327
   fr->freeformatsize = freeformatsize;
 
328
   fr->firsthead = firsthead;
 
329
 
 
330
   return 1;
 
331
 
 
332
}
 
333
 
 
334
/*
 
335
 * skips the RIFF header at the beginning
 
336
 *
 
337
 * returns:  0    = read-error
 
338
 *          -1/-2 = illegal RIFF header (= -2 backstep not valid)
 
339
 *           1    = skipping succeeded
 
340
 */
 
341
static int skip_riff(struct reader *rds)
 
342
{
 
343
   unsigned long length;
 
344
   unsigned char buf[16];
 
345
   
 
346
   if(!rds->read_frame_body(rds,buf,16))       /* read header information */
 
347
     return 0;
 
348
 
 
349
   if( strncmp("WAVEfmt ",(char *)buf+4,8) )         /* check 2. signature */
 
350
     return -1;
 
351
 
 
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);
 
356
 
 
357
   if(!rds->skip_bytes(rds,length)) /* will not store data in backbuff! */
 
358
     return 0;
 
359
 
 
360
   if(!rds->read_frame_body(rds,buf,8))  /* skip "data" plus length */
 
361
     return 0;
 
362
 
 
363
   if(strncmp("data",(char *)buf,4))
 
364
     return -2;
 
365
 
 
366
   return length+8+16;
 
367
}
 
368
 
 
369
/*
 
370
 * skips the ID3 header at the beginning
 
371
 *
 
372
 * returns:  0 = read-error
 
373
 *          -1 = illegal ID3 header
 
374
 *           1 = skipping succeeded
 
375
 */
 
376
static int skip_new_id3(struct reader *rds)
 
377
{
 
378
   unsigned long length;
 
379
   unsigned char buf[6];
 
380
 
 
381
   if(!rds->read_frame_body(rds,buf,6))       /* read more header information */
 
382
     return 0;
 
383
 
 
384
   if(buf[0] == 0xff)
 
385
     return -1;
 
386
 
 
387
   if( (buf[2]|buf[3]|buf[4]|buf[5]) & 0x80)
 
388
     return -1;
 
389
 
 
390
   length  = (unsigned long) buf[2] & 0x7f;
 
391
   length <<= 7;
 
392
   length += (unsigned long) buf[3] & 0x7f;
 
393
   length <<= 7;
 
394
   length += (unsigned long) buf[4] & 0x7f;
 
395
   length <<= 7;
 
396
   length += (unsigned long) buf[5] & 0x7f;
 
397
 
 
398
   if(!rds->skip_bytes(rds,length)) /* will not store data in backbuff! */
 
399
     return 0;
 
400
 
 
401
   return length+6;
 
402
 
 
403
}
 
404
 
 
405
 
 
406
 
 
407
 
 
408
 
 
409
/*****************************************************************
 
410
 * read next frame
 
411
 */
 
412
int read_frame(struct reader *rds,struct frame *fr)
 
413
{
 
414
    unsigned long newhead,oldhead;
 
415
    static unsigned char ssave[34];
 
416
 
 
417
    oldhead = fr->thishead;
 
418
 
 
419
    if (param.halfspeed) {
 
420
        static int halfphase = 0;
 
421
        if (halfphase--) {
 
422
            bsi.bitindex = 0;
 
423
            bsi.wordpointer = (unsigned char *) bsbuf;
 
424
            if (fr->lay == 3)
 
425
                memcpy (bsbuf, ssave, fr->sideInfoSize);
 
426
            return 1;
 
427
        }
 
428
        else
 
429
            halfphase = param.halfspeed - 1;
 
430
    }
 
431
 
 
432
    while(1) {
 
433
 
 
434
        if(!rds->head_read(rds,&newhead))
 
435
            return FALSE;
 
436
 
 
437
/*
 
438
        fprintf(stderr,"n %08lx",newhead);
 
439
*/
 
440
 
 
441
        if( !head_check(newhead) || !decode_header(fr,newhead) ) {
 
442
            if (!param.quiet)
 
443
                fprintf(stderr,"Illegal Audio-MPEG-Header 0x%08lx at offset 0x%lx.\n",
 
444
                        newhead,rds->tell(rds)-4);
 
445
 
 
446
            if(param.tryresync) {
 
447
                int try = 0;
 
448
                readers_pushback_header(rds,newhead);
 
449
                if(sync_stream(rds,fr,0xffff,&try) <= 0)
 
450
                   return 0;
 
451
                if(!param.quiet)
 
452
                   fprintf (stderr, "Skipped %d bytes in input.\n", try);
 
453
            }
 
454
            else
 
455
                return (0);
 
456
        }
 
457
        else 
 
458
          break;
 
459
    }
 
460
 
 
461
/*
 
462
    fprintf(stderr,"N %08lx",newhead);
 
463
*/
 
464
 
 
465
    fr->header_change = 2;
 
466
    if(oldhead) {
 
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;
 
472
          }
 
473
    }
 
474
 
 
475
 
 
476
    if(!fr->bitrate_index) { 
 
477
       fr->framesize = fr->freeformatsize + fr->padsize;
 
478
    }
 
479
 
 
480
/*
 
481
fprintf(stderr,"Reading %d\n",fr->framesize);
 
482
*/
 
483
 
 
484
    /* flip/init buffer for Layer 3 */
 
485
    /* FIXME for reentrance */
 
486
    bsbufold = bsbuf;
 
487
    bsbufold_end = bsbufend[bsnum];
 
488
    bsbuf = bsspace[bsnum]+512;
 
489
    bsnum = (bsnum + 1) & 1;
 
490
    bsbufend[bsnum] = fr->framesize;
 
491
 
 
492
    /* read main data into memory */
 
493
    if(!rds->read_frame_body(rds,bsbuf,fr->framesize))
 
494
        return 0;
 
495
 
 
496
    { 
 
497
      /* Test */
 
498
      static struct vbrHeader head;
 
499
      static int vbr = 0; /* FIXME */
 
500
      if(!vbr) {
 
501
        getVBRHeader(&head,bsbuf,fr);
 
502
        vbr = 1;
 
503
      }
 
504
    }
 
505
 
 
506
    bsi.bitindex = 0;
 
507
    bsi.wordpointer = (unsigned char *) bsbuf;
 
508
 
 
509
    if (param.halfspeed && fr->lay == 3)
 
510
        memcpy (ssave, bsbuf, fr->sideInfoSize);
 
511
 
 
512
    return 1;
 
513
}
 
514
 
 
515
/*
 
516
 * decode a header and write the information
 
517
 * into the frame structure
 
518
 */
 
519
static int decode_header(struct frame *fr,unsigned long newhead)
 
520
{
 
521
    if(!head_check(newhead)) {
 
522
        fprintf(stderr,"Oopps header is wrong %08lx\n",newhead);
 
523
        return 0;
 
524
    }
 
525
 
 
526
    if( newhead & (1<<20) ) {
 
527
        fr->lsf = (newhead & (1<<19)) ? 0x0 : 0x1;
 
528
        fr->mpeg25 = 0;
 
529
    }
 
530
    else {
 
531
        fr->lsf = 1;
 
532
        fr->mpeg25 = 1;
 
533
    }
 
534
 
 
535
    /* 
 
536
     * CHECKME: should be add more consistency checks here ?  
 
537
     * changed layer, changed CRC bit, changed sampling frequency 
 
538
     */
 
539
    {
 
540
        fr->lay = 4-((newhead>>17)&3);
 
541
        if( ((newhead>>10)&0x3) == 0x3) {
 
542
            fprintf(stderr,"Stream error\n");
 
543
            return 0;
 
544
        }
 
545
        if(fr->mpeg25) {
 
546
            fr->sampling_frequency = 6 + ((newhead>>10)&0x3);
 
547
        }
 
548
        else
 
549
            fr->sampling_frequency = ((newhead>>10)&0x3) + (fr->lsf*3);
 
550
        fr->error_protection = ((newhead>>16)&0x1)^0x1;
 
551
    }
 
552
 
 
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;
 
561
 
 
562
    fr->stereo    = (fr->mode == MPG_MD_MONO) ? 1 : 2;
 
563
 
 
564
    switch(fr->lay) {
 
565
    case 1:
 
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;
 
571
        break;
 
572
    case 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;
 
578
        break;
 
579
    case 3:
 
580
        if(fr->lsf)
 
581
            fr->sideInfoSize = (fr->stereo == 1) ? 9 : 17;
 
582
        else
 
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;
 
590
        break; 
 
591
    default:
 
592
        fprintf(stderr,"Sorry, unknown layer type.\n"); 
 
593
        return (0);
 
594
    }
 
595
 
 
596
    if(!fr->bitrate_index) {
 
597
        /* fprintf(stderr,"Warning, Free format not heavily tested: (head %08lx)\n",newhead); */
 
598
        fr->framesize = 0;
 
599
    }
 
600
    fr->thishead = newhead;
 
601
 
 
602
    return 1;
 
603
}
 
604
 
 
605
#ifdef MPG123_REMOTE
 
606
void print_rheader(struct frame *fr)
 
607
{
 
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" };
 
611
 
 
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],
 
617
            fr->framesize+4);
 
618
}
 
619
#endif
 
620
 
 
621
void print_header(struct frame *fr)
 
622
{
 
623
    static char *modes[4] = { "Stereo", "Joint-Stereo", "Dual-Channel", "Single-Channel" };
 
624
    static char *layers[4] = { "Unknown" , "I", "II", "III" };
 
625
 
 
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",
 
633
            fr->emphasis);
 
634
    fprintf(stderr,"Bitrate: %d Kbits/s, Extension value: %d\n",
 
635
            tabsel_123[fr->lsf][fr->lay-1][fr->bitrate_index],fr->extension);
 
636
}
 
637
 
 
638
void print_header_compact(struct frame *fr)
 
639
{
 
640
    static char *modes[4] = { "stereo", "joint-stereo", "dual-channel", "mono" };
 
641
    static char *layers[4] = { "Unknown" , "I", "II", "III" };
 
642
 
 
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"),
 
645
            layers[fr->lay],
 
646
            tabsel_123[fr->lsf][fr->lay-1][fr->bitrate_index],
 
647
            freqs[fr->sampling_frequency], modes[fr->mode]);
 
648
}
 
649
 
 
650
void print_id3_tag(unsigned char *buf)
 
651
{
 
652
    struct id3tag {
 
653
        char tag[3];
 
654
        char title[30];
 
655
        char artist[30];
 
656
        char album[30];
 
657
        char year[4];
 
658
        char comment[30];
 
659
        unsigned char genre;
 
660
    };
 
661
    struct id3tag *tag = (struct id3tag *) buf;
 
662
    char title[31]={0,};
 
663
    char artist[31]={0,};
 
664
    char album[31]={0,};
 
665
    char year[5]={0,};
 
666
    char comment[31]={0,};
 
667
    char genre[31]={0,};
 
668
 
 
669
    if(param.quiet)
 
670
        return;
 
671
 
 
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);
 
677
 
 
678
    if ( tag->genre < sizeof(genre_table)/sizeof(*genre_table) ) {
 
679
        strncpy(genre, genre_table[tag->genre], 30);
 
680
    } else {
 
681
        strncpy(genre,"Unknown",30);
 
682
    }
 
683
        
 
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);
 
687
}
 
688
 
 
689
#if 0
 
690
/* removed the strndup for better portability */
 
691
/*
 
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.
 
695
 */
 
696
char *strndup (const char *src, int num)
 
697
{
 
698
    char *dst;
 
699
 
 
700
    if (!(dst = (char *) malloc(num+1)))
 
701
        return (NULL);
 
702
    dst[num] = '\0';
 
703
    return (strncpy(dst, src, num));
 
704
}
 
705
#endif
 
706
 
 
707
/*
 
708
 *   Split "path" into directory and filename components.
 
709
 *
 
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.
 
713
 *
 
714
 *   Return value is 1 if a directory was specified AND it
 
715
 *   is different from the previous one (if any).
 
716
 */
 
717
 
 
718
int split_dir_file (const char *path, char **dname, char **fname)
 
719
{
 
720
    static char *lastdir = NULL;
 
721
    char *slashpos;
 
722
 
 
723
    if ((slashpos = strrchr(path, '/'))) {
 
724
        *fname = slashpos + 1;
 
725
        *dname = strdup(path); /* , 1 + slashpos - path); */
 
726
        if(!(*dname)) {
 
727
            perror("memory");
 
728
            exit(1);
 
729
        }
 
730
        (*dname)[1 + slashpos - path] = 0;
 
731
        if (lastdir && !strcmp(lastdir, *dname)) {
 
732
            /***   same as previous directory   ***/
 
733
            free (*dname);
 
734
            *dname = lastdir;
 
735
            return 0;
 
736
        }
 
737
        else {
 
738
            /***   different directory   ***/
 
739
            if (lastdir)
 
740
                free (lastdir);
 
741
            lastdir = *dname;
 
742
            return 1;
 
743
        }
 
744
    }
 
745
    else {
 
746
        /***   no directory specified   ***/
 
747
        if (lastdir) {
 
748
            free (lastdir);
 
749
            lastdir = NULL;
 
750
        };
 
751
        *dname = NULL;
 
752
        *fname = (char *)path;
 
753
        return 0;
 
754
    }
 
755
}
 
756
 
 
757
void set_pointer(int ssize,long backstep)
 
758
{
 
759
    bsi.wordpointer = bsbuf + ssize - backstep;
 
760
    if (backstep)
 
761
        memcpy(bsi.wordpointer,bsbufold+bsbufold_end-backstep,backstep);
 
762
    bsi.bitindex = 0; 
 
763
}
 
764
 
 
765
/********************************/
 
766
 
 
767
double compute_bpf(struct frame *fr)
 
768
{
 
769
    double bpf;
 
770
 
 
771
    if(!fr->bitrate_index) {
 
772
        return fr->freeformatsize + 4;
 
773
    }
 
774
 
 
775
    switch(fr->lay) {
 
776
    case 1:
 
777
        bpf = tabsel_123[fr->lsf][0][fr->bitrate_index];
 
778
        bpf *= 12000.0 * 4.0;
 
779
        bpf /= freqs[fr->sampling_frequency] <<(fr->lsf);
 
780
        break;
 
781
    case 2:
 
782
    case 3:
 
783
        bpf = tabsel_123[fr->lsf][fr->lay-1][fr->bitrate_index];
 
784
        bpf *= 144000;
 
785
        bpf /= freqs[fr->sampling_frequency] << (fr->lsf);
 
786
        break;
 
787
    default:
 
788
        bpf = 1.0;
 
789
    }
 
790
 
 
791
    return bpf;
 
792
}
 
793
 
 
794
double compute_tpf(struct frame *fr)
 
795
{
 
796
    static int bs[4] = { 0,384,1152,1152 };
 
797
    double tpf;
 
798
 
 
799
    tpf = (double) bs[fr->lay];
 
800
    tpf /= freqs[fr->sampling_frequency] << (fr->lsf);
 
801
    return tpf;
 
802
}
 
803
 
 
804
/*
 
805
 * Returns number of frames queued up in output buffer, i.e. 
 
806
 * offset between currently played and currently decoded frame.
 
807
 */
 
808
 
 
809
#ifndef NOXFERMEM
 
810
long compute_buffer_offset(struct frame *fr)
 
811
{
 
812
    long bufsize;
 
813
        
 
814
    /*
 
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.
 
818
     */
 
819
        
 
820
    if(!param.usebuffer || !(bufsize=xfermem_get_usedspace(buffermem))
 
821
       || !buffermem->buf[0] || !buffermem->buf[1])
 
822
        return 0;
 
823
 
 
824
    bufsize = (long)((double) bufsize / buffermem->buf[0] / 
 
825
                     buffermem->buf[1] / compute_tpf(fr));
 
826
        
 
827
    if((buffermem->buf[2] & AUDIO_FORMAT_MASK) == AUDIO_FORMAT_16)
 
828
        return bufsize/2;
 
829
    else
 
830
        return bufsize;
 
831
}
 
832
#endif
 
833
 
 
834
void print_stat(struct reader *rds,struct frame *fr,int no,long buffsize,struct audio_info_struct *ai)
 
835
{
 
836
    double bpf,tpf,tim1,tim2;
 
837
    double dt = 0.0;
 
838
    int sno,rno;
 
839
    char outbuf[256];
 
840
 
 
841
    if(!rds || !fr) 
 
842
        return;
 
843
 
 
844
    outbuf[0] = 0;
 
845
 
 
846
#ifndef GENERIC
 
847
    {
 
848
        struct timeval t;
 
849
        fd_set serr;
 
850
        int n,errfd = fileno(stderr);
 
851
 
 
852
        t.tv_sec=t.tv_usec=0;
 
853
 
 
854
        FD_ZERO(&serr);
 
855
        FD_SET(errfd,&serr);
 
856
        n = select(errfd+1,NULL,&serr,NULL,&t);
 
857
        if(n <= 0)
 
858
            return;
 
859
    }
 
860
#endif
 
861
 
 
862
    bpf = compute_bpf(fr);
 
863
    tpf = compute_tpf(fr);
 
864
 
 
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)
 
868
            dt *= 0.5;
 
869
    }
 
870
 
 
871
    rno = 0;
 
872
    sno = no;
 
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);
 
877
    }
 
878
 
 
879
    sprintf(outbuf+strlen(outbuf),"\rFrame# %5d [%5d], ",sno,rno);
 
880
 
 
881
    tim1 = sno*tpf-dt;
 
882
    tim2 = rno*tpf+dt;
 
883
#if 0
 
884
    tim1 = tim1 < 0 ? 0.0 : tim1;
 
885
#endif
 
886
    tim2 = tim2 < 0 ? 0.0 : tim2;
 
887
 
 
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);
 
895
 
 
896
    if(param.usebuffer)
 
897
        sprintf(outbuf+strlen(outbuf),"[%8ld] ",(long)buffsize);
 
898
    write(fileno(stderr),outbuf,strlen(outbuf));
 
899
#if 0
 
900
    fflush(out); /* hmm not really nec. */
 
901
#endif
 
902
}
 
903
 
 
904
int get_songlen(struct reader *rds,struct frame *fr,int no)
 
905
{
 
906
    double tpf;
 
907
        
 
908
    if(!fr)
 
909
        return 0;
 
910
        
 
911
    if(no < 0) {
 
912
        if(!rds || rds->filelen < 0)
 
913
            return 0;
 
914
        no = (double) rds->filelen / compute_bpf(fr);
 
915
    }
 
916
 
 
917
    tpf = compute_tpf(fr);
 
918
    return no*tpf;
 
919
}
 
920
 
 
921