Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.         parse: spawned from common; clustering around stream/frame parsing
  3.  
  4.         copyright ?-2009 by the mpg123 project - free software under the terms of the LGPL 2.1
  5.         see COPYING and AUTHORS files in distribution or http://mpg123.org
  6.         initially written by Michael Hipp & Thomas Orgis
  7. */
  8.  
  9. #include "mpg123lib_intern.h"
  10.  
  11. #include <sys/stat.h>
  12. #include <fcntl.h>
  13.  
  14. #include "getbits.h"
  15.  
  16.  
  17. /* a limit for number of frames in a track; beyond that unsigned long may not be enough to hold byte addresses */
  18. #ifdef HAVE_LIMITS_H
  19. #include <limits.h>
  20. #endif
  21. #ifndef ULONG_MAX
  22. /* hm, is this portable across preprocessors? */
  23. #define ULONG_MAX ((unsigned long)-1)
  24. #endif
  25. #define TRACK_MAX_FRAMES ULONG_MAX/4/1152
  26.  
  27. #include "debug.h"
  28.  
  29. #define bsbufid(fr) (fr)->bsbuf==(fr)->bsspace[0] ? 0 : ((fr)->bsbuf==fr->bsspace[1] ? 1 : ( (fr)->bsbuf==(fr)->bsspace[0]+512 ? 2 : ((fr)->bsbuf==fr->bsspace[1]+512 ? 3 : -1) ) )
  30.  
  31. /*
  32.         AAAAAAAA AAABBCCD EEEEFFGH IIJJKLMM
  33.         A: sync
  34.         B: mpeg version
  35.         C: layer
  36.         D: CRC
  37.         E: bitrate
  38.         F:sampling rate
  39.         G: padding
  40.         H: private
  41.         I: channel mode
  42.         J: mode ext
  43.         K: copyright
  44.         L: original
  45.         M: emphasis
  46.  
  47.         old compare mask 0xfffffd00:
  48.         11111111 11111111 11111101 00000000
  49.  
  50.         means: everything must match excluding padding and channel mode, ext mode, ...
  51.         But a vbr stream's headers will differ in bitrate!
  52.         We are already strict in allowing only frames of same type in stream, we should at least watch out for VBR while being strict.
  53.  
  54.         So a better mask is:
  55.         11111111 11111111 00001101 00000000
  56.  
  57.         Even more, I'll allow varying crc bit.
  58.         11111111 11111110 00001101 00000000
  59.  
  60.         (still unsure about this private bit)
  61. */
  62. #define HDRCMPMASK 0xfffe0d00
  63. #define HDRSAMPMASK 0xc00 /* 1100 00000000, FF bits (sample rate) */
  64.  
  65. /* bitrates for [mpeg1/2][layer] */
  66. static const int tabsel_123[2][3][16] =
  67. {
  68.         {
  69.                 {0,32,64,96,128,160,192,224,256,288,320,352,384,416,448,},
  70.                 {0,32,48,56, 64, 80, 96,112,128,160,192,224,256,320,384,},
  71.                 {0,32,40,48, 56, 64, 80, 96,112,128,160,192,224,256,320,}
  72.         },
  73.         {
  74.                 {0,32,48,56,64,80,96,112,128,144,160,176,192,224,256,},
  75.                 {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,},
  76.                 {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,}
  77.         }
  78. };
  79.  
  80. const long freqs[9] = { 44100, 48000, 32000, 22050, 24000, 16000 , 11025 , 12000 , 8000 };
  81.  
  82. static int decode_header(mpg123_handle *fr,unsigned long newhead);
  83.  
  84. int read_frame_init(mpg123_handle* fr)
  85. {
  86.         if(frame_reset(fr) != 0) return -1;
  87.         return 0;
  88. }
  89.  
  90. /* These two are to be replaced by one function that gives all the frame parameters (for outsiders).*/
  91.  
  92. int frame_bitrate(mpg123_handle *fr)
  93. {
  94.         return tabsel_123[fr->lsf][fr->lay-1][fr->bitrate_index];
  95. }
  96.  
  97. long frame_freq(mpg123_handle *fr)
  98. {
  99.         return freqs[fr->sampling_frequency];
  100. }
  101.  
  102. #define free_format_header(head) ( ((head & 0xffe00000) == 0xffe00000) && ((head>>17)&3) && (((head>>12)&0xf) == 0x0) && (((head>>10)&0x3) != 0x3 ))
  103.  
  104. /* compiler is smart enought to inline this one or should I really do it as macro...? */
  105. int head_check(unsigned long head)
  106. {
  107.         if
  108.         (
  109.                 /* first 11 bits are set to 1 for frame sync */
  110.                 ((head & 0xffe00000) != 0xffe00000)
  111.                 ||
  112.                 /* layer: 01,10,11 is 1,2,3; 00 is reserved */
  113.                 (!((head>>17)&3))
  114.                 ||
  115.                 /* 1111 means bad bitrate */
  116.                 (((head>>12)&0xf) == 0xf)
  117.                 ||
  118.                 /* sampling freq: 11 is reserved */
  119.                 (((head>>10)&0x3) == 0x3 )
  120.                 /* here used to be a mpeg 2.5 check... re-enabled 2.5 decoding due to lack of evidence that it is really not good */
  121.         )
  122.         {
  123.                 return FALSE;
  124.         }
  125.         /* if no check failed, the header is valid (hopefully)*/
  126.         else
  127.         {
  128.                 return TRUE;
  129.         }
  130. }
  131.  
  132. static int check_lame_tag(mpg123_handle *fr)
  133. {
  134.         /*
  135.                 going to look for Xing or Info at some position after the header
  136.                                                    MPEG 1  MPEG 2/2.5 (LSF)
  137.                 Stereo, Joint Stereo, Dual Channel  32      17
  138.                 Mono                                17       9
  139.  
  140.                 Also, how to avoid false positives? I guess I should interpret more of the header to rule that out(?).
  141.                 I hope that ensuring all zeros until tag start is enough.
  142.         */
  143.         int lame_offset = (fr->stereo == 2) ? (fr->lsf ? 17 : 32 ) : (fr->lsf ? 9 : 17);
  144.         /* At least skip the decoder delay. */
  145. #ifdef GAPLESS
  146.         if(fr->begin_s == 0) frame_gapless_init(fr, GAPLESS_DELAY, 0);
  147. #endif
  148.  
  149.         if(fr->framesize >= 120+lame_offset) /* traditional Xing header is 120 bytes */
  150.         {
  151.                 int i;
  152.                 int lame_type = 0;
  153.                 debug("do we have lame tag?");
  154.                 /* only search for tag when all zero before it (apart from checksum) */
  155.                 for(i=2; i < lame_offset; ++i) if(fr->bsbuf[i] != 0) break;
  156.                 if(i == lame_offset)
  157.                 {
  158.                         debug("possibly...");
  159.                         if
  160.                         (
  161.                                            (fr->bsbuf[lame_offset] == 'I')
  162.                                 && (fr->bsbuf[lame_offset+1] == 'n')
  163.                                 && (fr->bsbuf[lame_offset+2] == 'f')
  164.                                 && (fr->bsbuf[lame_offset+3] == 'o')
  165.                         )
  166.                         {
  167.                                 lame_type = 1; /* We still have to see what there is */
  168.                         }
  169.                         else if
  170.                         (
  171.                                            (fr->bsbuf[lame_offset] == 'X')
  172.                                 && (fr->bsbuf[lame_offset+1] == 'i')
  173.                                 && (fr->bsbuf[lame_offset+2] == 'n')
  174.                                 && (fr->bsbuf[lame_offset+3] == 'g')
  175.                         )
  176.                         {
  177.                                 lame_type = 2;
  178.                                 fr->vbr = MPG123_VBR; /* Xing header means always VBR */
  179.                         }
  180.                         if(lame_type)
  181.                         {
  182.                                 unsigned long xing_flags;
  183.  
  184.                                 /* we have one of these headers... */
  185.                                 if(VERBOSE2) fprintf(stderr, "Note: Xing/Lame/Info header detected\n");
  186.                                 /* now interpret the Xing part, I have 120 bytes total for sure */
  187.                                 /* there are 4 bytes for flags, but only the last byte contains known ones */
  188.                                 lame_offset += 4; /* now first byte after Xing/Name */
  189.                                 /* 4 bytes dword for flags */
  190.                                 #define make_long(a, o) ((((unsigned long) a[o]) << 24) | (((unsigned long) a[o+1]) << 16) | (((unsigned long) a[o+2]) << 8) | ((unsigned long) a[o+3]))
  191.                                 /* 16 bit */
  192.                                 #define make_short(a,o) ((((unsigned short) a[o]) << 8) | ((unsigned short) a[o+1]))
  193.                                 xing_flags = make_long(fr->bsbuf, lame_offset);
  194.                                 lame_offset += 4;
  195.                                 debug1("Xing: flags 0x%08lx", xing_flags);
  196.                                 if(xing_flags & 1) /* frames */
  197.                                 {
  198.                                         /*
  199.                                                 In theory, one should use that value for skipping...
  200.                                                 When I know the exact number of samples I could simply count in flush_output,
  201.                                                 but that's problematic with seeking and such.
  202.                                                 I still miss the real solution for detecting the end.
  203.                                         */
  204.                                         fr->track_frames = (off_t) make_long(fr->bsbuf, lame_offset);
  205.                                         if(fr->track_frames > TRACK_MAX_FRAMES) fr->track_frames = 0; /* endless stream? */
  206.                                         #ifdef GAPLESS
  207.                                         /* if no further info there, remove/add at least the decoder delay */
  208.                                         if(fr->p.flags & MPG123_GAPLESS)
  209.                                         {
  210.                                                 off_t length = fr->track_frames * spf(fr);
  211.                                                 if(length > 1)
  212.                                                 frame_gapless_init(fr, GAPLESS_DELAY, length+GAPLESS_DELAY);
  213.                                         }
  214.                                         #endif
  215.                                         if(VERBOSE3) fprintf(stderr, "Note: Xing: %lu frames\n", (long unsigned)fr->track_frames);
  216.                                         lame_offset += 4;
  217.                                 }
  218.                                 if(xing_flags & 0x2) /* bytes */
  219.                                 {
  220.                                         unsigned long xing_bytes = make_long(fr->bsbuf, lame_offset);                                   /* We assume that this is the _total_ size of the file, including Xing frame ... and ID3 frames...
  221.                                            It's not that clearly documented... */
  222.                                         if(fr->rdat.filelen < 1)
  223.                                         fr->rdat.filelen = (off_t) xing_bytes; /* One could start caring for overflow here. */
  224.                                         else
  225.                                         {
  226.                                                 if((off_t) xing_bytes != fr->rdat.filelen && NOQUIET)
  227.                                                 {
  228.                                                         double diff = 1.0/fr->rdat.filelen * (fr->rdat.filelen - (off_t)xing_bytes);
  229.                                                         if(diff < 0.) diff = -diff;
  230.  
  231.                                                         if(VERBOSE3)
  232.                                                         fprintf(stderr, "Note: Xing stream size %lu differs by %f%% from determined/given file size!\n", xing_bytes, diff);
  233.  
  234.                                                         if(diff > 1.)
  235.                                                         fprintf(stderr, "Warning: Xing stream size off by more than 1%%, fuzzy seeking may be even more fuzzy than by design!\n");
  236.                                                 }
  237.                                         }
  238.  
  239.                                         if(VERBOSE3)
  240.                                         fprintf(stderr, "Note: Xing: %lu bytes\n", (long unsigned)xing_bytes);
  241.  
  242.                                         lame_offset += 4;
  243.                                 }
  244.                                 if(xing_flags & 0x4) /* TOC */
  245.                                 {
  246.                                         frame_fill_toc(fr, fr->bsbuf+lame_offset);
  247.                                         lame_offset += 100; /* just skip */
  248.                                 }
  249.                                 if(xing_flags & 0x8) /* VBR quality */
  250.                                 {
  251.                                         if(VERBOSE3)
  252.                                         {
  253.                                                 unsigned long xing_quality = make_long(fr->bsbuf, lame_offset);
  254.                                                 fprintf(stderr, "Note: Xing: quality = %lu\n", (long unsigned)xing_quality);
  255.                                         }
  256.                                         lame_offset += 4;
  257.                                 }
  258.                                 /* I guess that either 0 or LAME extra data follows */
  259.                                 /* there may this crc16 be floating around... (?) */
  260.                                 if(fr->bsbuf[lame_offset] != 0)
  261.                                 {
  262.                                         unsigned char lame_vbr;
  263.                                         float replay_gain[2] = {0,0};
  264.                                         float peak = 0;
  265.                                         float gain_offset = 0; /* going to be +6 for old lame that used 83dB */
  266.                                         char nb[10];
  267.                                         memcpy(nb, fr->bsbuf+lame_offset, 9);
  268.                                         nb[9] = 0;
  269.                                         if(VERBOSE3) fprintf(stderr, "Note: Info: Encoder: %s\n", nb);
  270.                                         if(!strncmp("LAME", nb, 4))
  271.                                         {
  272.                                                 gain_offset = 6;
  273.                                                 debug("TODO: finish lame detetcion...");
  274.                                         }
  275.                                         lame_offset += 9;
  276.                                         /* the 4 big bits are tag revision, the small bits vbr method */
  277.                                         lame_vbr = fr->bsbuf[lame_offset] & 15;
  278.                                         if(VERBOSE3)
  279.                                         {
  280.                                                 fprintf(stderr, "Note: Info: rev %u\n", fr->bsbuf[lame_offset] >> 4);
  281.                                                 fprintf(stderr, "Note: Info: vbr mode %u\n", lame_vbr);
  282.                                         }
  283.                                         lame_offset += 1;
  284.                                         switch(lame_vbr)
  285.                                         {
  286.                                                 /* from rev1 proposal... not sure if all good in practice */
  287.                                                 case 1:
  288.                                                 case 8: fr->vbr = MPG123_CBR; break;
  289.                                                 case 2:
  290.                                                 case 9: fr->vbr = MPG123_ABR; break;
  291.                                                 default: fr->vbr = MPG123_VBR; /* 00==unknown is taken as VBR */
  292.                                         }
  293.                                         /* skipping: lowpass filter value */
  294.                                         lame_offset += 1;
  295.                                         /* replaygain */
  296.                                         /* 32bit float: peak amplitude -- why did I parse it as int before??*/
  297.                                         /* Ah, yes, lame seems to store it as int since some day in 2003; I've only seen zeros anyway until now, bah! */
  298.                                         if
  299.                                         (
  300.                                                          (fr->bsbuf[lame_offset] != 0)
  301.                                                 || (fr->bsbuf[lame_offset+1] != 0)
  302.                                                 || (fr->bsbuf[lame_offset+2] != 0)
  303.                                                 || (fr->bsbuf[lame_offset+3] != 0)
  304.                                         )
  305.                                         {
  306.                                                 debug("Wow! Is there _really_ a non-zero peak value? Now is it stored as float or int - how should I know?");
  307.                                                 /* byte*peak_bytes = (byte*) &peak;
  308.                                                 ... endianess ... just copy bytes to avoid floating point operation on unaligned memory?
  309.                                                 peak_bytes[0] = ...
  310.                                                 peak = *(float*) (fr->bsbuf+lame_offset); */
  311.                                         }
  312.                                         if(VERBOSE3) fprintf(stderr, "Note: Info: peak = %f (I won't use this)\n", peak);
  313.                                         peak = 0; /* until better times arrived */
  314.                                         lame_offset += 4;
  315.                                         /*
  316.                                                 ReplayGain values - lame only writes radio mode gain...
  317.                                                 16bit gain, 3 bits name, 3 bits originator, sign (1=-, 0=+), dB value*10 in 9 bits (fixed point)
  318.                                                 ignore the setting if name or originator == 000!
  319.                                                 radio 0 0 1 0 1 1 1 0 0 1 1 1 1 1 0 1
  320.                                                 audiophile 0 1 0 0 1 0 0 0 0 0 0 1 0 1 0 0
  321.                                         */
  322.  
  323.                                         for(i =0; i < 2; ++i)
  324.                                         {
  325.                                                 unsigned char origin = (fr->bsbuf[lame_offset] >> 2) & 0x7; /* the 3 bits after that... */
  326.                                                 if(origin != 0)
  327.                                                 {
  328.                                                         unsigned char gt = fr->bsbuf[lame_offset] >> 5; /* only first 3 bits */
  329.                                                         if(gt == 1) gt = 0; /* radio */
  330.                                                         else if(gt == 2) gt = 1; /* audiophile */
  331.                                                         else continue;
  332.                                                         /* get the 9 bits into a number, divide by 10, multiply sign... happy bit banging */
  333.                                                         replay_gain[0] = (float) ((fr->bsbuf[lame_offset] & 0x2) ? -0.1 : 0.1) * (make_short(fr->bsbuf, lame_offset) & 0x1f);
  334.                                                 }
  335.                                                 lame_offset += 2;
  336.                                         }
  337.                                         if(VERBOSE3)
  338.                                         {
  339.                                                 fprintf(stderr, "Note: Info: Radio Gain = %03.1fdB\n", replay_gain[0]);
  340.                                                 fprintf(stderr, "Note: Info: Audiophile Gain = %03.1fdB\n", replay_gain[1]);
  341.                                         }
  342.                                         for(i=0; i < 2; ++i)
  343.                                         {
  344.                                                 if(fr->rva.level[i] <= 0)
  345.                                                 {
  346.                                                         fr->rva.peak[i] = 0; /* at some time the parsed peak should be used */
  347.                                                         fr->rva.gain[i] = replay_gain[i];
  348.                                                         fr->rva.level[i] = 0;
  349.                                                 }
  350.                                         }
  351.                                         lame_offset += 1; /* skipping encoding flags byte */
  352.                                         if(fr->vbr == MPG123_ABR)
  353.                                         {
  354.                                                 fr->abr_rate = fr->bsbuf[lame_offset];
  355.                                                 if(VERBOSE3) fprintf(stderr, "Note: Info: ABR rate = %u\n", fr->abr_rate);
  356.                                         }
  357.                                         lame_offset += 1;
  358.                                         /* encoder delay and padding, two 12 bit values... lame does write them from int ...*/
  359.                                         if(VERBOSE3)
  360.                                         fprintf(stderr, "Note: Encoder delay = %i; padding = %i\n",
  361.                                                 ((((int) fr->bsbuf[lame_offset]) << 4) | (((int) fr->bsbuf[lame_offset+1]) >> 4)),
  362.                                                 (((((int) fr->bsbuf[lame_offset+1]) << 8) | (((int) fr->bsbuf[lame_offset+2]))) & 0xfff) );
  363.                                         #ifdef GAPLESS
  364.                                         if(fr->p.flags & MPG123_GAPLESS)
  365.                                         {
  366.                                                 off_t length = fr->track_frames * spf(fr);
  367.                                                 off_t skipbegin = GAPLESS_DELAY + ((((int) fr->bsbuf[lame_offset]) << 4) | (((int) fr->bsbuf[lame_offset+1]) >> 4));
  368.                                                 off_t skipend = -GAPLESS_DELAY + (((((int) fr->bsbuf[lame_offset+1]) << 8) | (((int) fr->bsbuf[lame_offset+2]))) & 0xfff);
  369.                                                 debug3("preparing gapless mode for layer3: length %lu, skipbegin %lu, skipend %lu",
  370.                                                                 (long unsigned)length, (long unsigned)skipbegin, (long unsigned)skipend);
  371.                                                 if(length > 1)
  372.                                                 frame_gapless_init(fr, skipbegin, (skipend < length) ? length-skipend : length);
  373.                                         }
  374.                                         #endif
  375.                                 }
  376.                                 /* switch buffer back ... */
  377.                                 fr->bsbuf = fr->bsspace[fr->bsnum]+512;
  378.                                 fr->bsnum = (fr->bsnum + 1) & 1;
  379.                                 return 1; /* got it! */
  380.                         }
  381.                 }
  382.         }
  383.         return 0; /* no lame tag */
  384. }
  385.  
  386. /* Just tell if the header is some mono. */
  387. static int header_mono(unsigned long newhead)
  388. {
  389.         return ((newhead>>6)&0x3) == MPG_MD_MONO ? TRUE : FALSE;
  390. }
  391.  
  392. /*
  393.         That's a big one: read the next frame. 1 is success, <= 0 is some error
  394.         Special error READER_MORE means: Please feed more data and try again.
  395. */
  396. int read_frame(mpg123_handle *fr)
  397. {
  398.         /* TODO: rework this thing */
  399.         unsigned long newhead;
  400.         off_t framepos;
  401.         int ret;
  402.         /* stuff that needs resetting if complete frame reading fails */
  403.         int oldsize  = fr->framesize;
  404.         int oldphase = fr->halfphase;
  405.         fr->fsizeold=fr->framesize;       /* for Layer3 */
  406.  
  407.         /* Speed-down hack: Play it again, Sam (the frame, I mean). */
  408.         if (fr->p.halfspeed)
  409.         {
  410.                 if(fr->halfphase) /* repeat last frame */
  411.                 {
  412.                         debug("repeat!");
  413.                         fr->to_decode = fr->to_ignore = TRUE;
  414.                         --fr->halfphase;
  415.                         fr->bitindex = 0;
  416.                         fr->wordpointer = (unsigned char *) fr->bsbuf;
  417.                         if(fr->lay == 3) memcpy (fr->bsbuf, fr->ssave, fr->ssize);
  418.                         if(fr->error_protection) fr->crc = getbits(fr, 16); /* skip crc */
  419.                         return 1;
  420.                 }
  421.                 else
  422.                 {
  423.                         fr->halfphase = fr->p.halfspeed - 1;
  424.                 }
  425.         }
  426.  
  427. read_again:
  428.         debug2("trying to get frame %li at 0x%lx", (long)fr->num+1, (unsigned long)fr->rd->tell(fr));
  429.         if((ret = fr->rd->head_read(fr,&newhead)) <= 0){ debug("need more?"); goto read_frame_bad;}
  430.  
  431. init_resync:
  432.  
  433.         fr->header_change = 2; /* output format change is possible... */
  434.         if(fr->oldhead)        /* check a following header for change */
  435.         {
  436.                 if(fr->oldhead == newhead) fr->header_change = 0;
  437.                 else
  438.                 /* If they have the same sample rate. Note that only is _not_ the case for the first header, as we enforce sample rate match for following frames.
  439.                          So, during one stream, only change of stereoness is possible and indicated by header_change == 2. */
  440.                 if((fr->oldhead & HDRSAMPMASK) == (newhead & HDRSAMPMASK))
  441.                 {
  442.                         /* Now if both channel modes are mono or both stereo, it's no big deal. */
  443.                         if( header_mono(fr->oldhead) == header_mono(newhead))
  444.                         fr->header_change = 1;
  445.                 }
  446.         }
  447.  
  448. #ifdef SKIP_JUNK
  449.         /* watch out for junk/tags on beginning of stream by invalid header */
  450.         if(!fr->firsthead && !head_check(newhead)) {
  451.                 int i;
  452.  
  453.                 /* check for id3v2; first three bytes (of 4) are "ID3" */
  454.                 if((newhead & (unsigned long) 0xffffff00) == (unsigned long) 0x49443300)
  455.                 {
  456.                         int id3ret = 0;
  457.                         id3ret = parse_new_id3(fr, newhead);
  458.                         if     (id3ret < 0){ debug("need more?"); ret = id3ret; goto read_frame_bad; }
  459. #ifndef NO_ID3V2
  460.                         else if(id3ret > 0){ debug("got ID3v2"); fr->metaflags  |= MPG123_NEW_ID3|MPG123_ID3; }
  461.                         else debug("no useful ID3v2");
  462. #endif
  463.  
  464.                         fr->oldhead = 0;
  465.                         goto read_again; /* Also in case of invalid ID3 tag (ret==0), try to get on track again. */
  466.                 }
  467.                 else if(VERBOSE2 && fr->silent_resync == 0) fprintf(stderr,"Note: Junk at the beginning (0x%08lx)\n",newhead);
  468.  
  469.                 /* I even saw RIFF headers at the beginning of MPEG streams ;( */
  470.                 if(newhead == ('R'<<24)+('I'<<16)+('F'<<8)+'F') {
  471.                         if(VERBOSE2 && fr->silent_resync == 0) fprintf(stderr, "Note: Looks like a RIFF header.\n");
  472.  
  473.                         if((ret=fr->rd->head_read(fr,&newhead))<=0){ debug("need more?"); goto read_frame_bad; }
  474.  
  475.                         while(newhead != ('d'<<24)+('a'<<16)+('t'<<8)+'a')
  476.                         {
  477.                                 if((ret=fr->rd->head_shift(fr,&newhead))<=0){ debug("need more?"); goto read_frame_bad; }
  478.                         }
  479.                         if((ret=fr->rd->head_read(fr,&newhead))<=0){ debug("need more?"); goto read_frame_bad; }
  480.  
  481.                         if(VERBOSE2 && fr->silent_resync == 0) fprintf(stderr,"Note: Skipped RIFF header!\n");
  482.  
  483.                         fr->oldhead = 0;
  484.                         goto read_again;
  485.                 }
  486.                 /* unhandled junk... just continue search for a header */
  487.                 /* step in byte steps through next 64K */
  488.                 debug("searching for header...");
  489.                 for(i=0;i<65536;i++) {
  490.                         if((ret=fr->rd->head_shift(fr,&newhead))<=0){ debug("need more?"); goto read_frame_bad; }
  491.                         /* if(head_check(newhead)) */
  492.                         if(head_check(newhead) && decode_header(fr, newhead))
  493.                                 break;
  494.                 }
  495.                 if(i == 65536)
  496.                 {
  497.                         if(NOQUIET) error("Giving up searching valid MPEG header after 64K of junk.");
  498.                         return 0;
  499.                 }
  500.                 else debug("hopefully found one...");
  501.                 /*
  502.                  * should we additionaly check, whether a new frame starts at
  503.                  * the next expected position? (some kind of read ahead)
  504.                  * We could implement this easily, at least for files.
  505.                  */
  506.         }
  507. #endif
  508.  
  509.         /* first attempt of read ahead check to find the real first header; cannot believe what junk is out there! */
  510.         /* for now, a spurious first free format header screws up here; need free format support for detecting false free format headers... */
  511.         if(!fr->firsthead && fr->rdat.flags & (READER_SEEKABLE|READER_BUFFERED) && head_check(newhead) && decode_header(fr, newhead))
  512.         {
  513.                 unsigned long nexthead = 0;
  514.                 int hd = 0;
  515.                 off_t start = fr->rd->tell(fr);
  516.                 debug2("doing ahead check with BPF %d at %li", fr->framesize+4, (long)start);
  517.                 /* step framesize bytes forward and read next possible header*/
  518.                 if((ret=fr->rd->skip_bytes(fr, fr->framesize))<0)
  519.                 {
  520.                         if(ret==READER_ERROR && NOQUIET) error("cannot seek!");
  521.                         goto read_frame_bad;
  522.                 }
  523.                 hd = fr->rd->head_read(fr,&nexthead);
  524.                 if(hd==MPG123_NEED_MORE){ debug("need more?"); ret = hd; goto read_frame_bad; }
  525.                 if((ret=fr->rd->back_bytes(fr, fr->rd->tell(fr)-start))<0)
  526.                 {
  527.                         if(ret==READER_ERROR && NOQUIET) error("cannot seek!");
  528.                         else debug("need more?");
  529.                         goto read_frame_bad;
  530.                 }
  531.                 debug1("After fetching next header, at %li", (long)fr->rd->tell(fr));
  532.                 if(!hd)
  533.                 {
  534.                         if(NOQUIET) warning("cannot read next header, a one-frame stream? Duh...");
  535.                 }
  536.                 else
  537.                 {
  538.                         debug2("does next header 0x%08lx match first 0x%08lx?", nexthead, newhead);
  539.                         /* not allowing free format yet */
  540.                         if(!head_check(nexthead) || (nexthead & HDRCMPMASK) != (newhead & HDRCMPMASK))
  541.                         {
  542.                                 debug("No, the header was not valid, start from beginning...");
  543.                                 fr->oldhead = 0; /* start over */
  544.                                 /* try next byte for valid header */
  545.                                 if((ret=fr->rd->back_bytes(fr, 3))<0)
  546.                                 {
  547.                                         if(NOQUIET) error("cannot seek!");
  548.                                         else debug("need more?");
  549.                                         goto read_frame_bad;
  550.                                 }
  551.                                 goto read_again;
  552.                         }
  553.                 }
  554.         }
  555.  
  556.         /* why has this head check been avoided here before? */
  557.         if(!head_check(newhead))
  558.         {
  559.                 /* and those ugly ID3 tags */
  560.                 if((newhead & 0xffffff00) == ('T'<<24)+('A'<<16)+('G'<<8))
  561.                 {
  562.                         fr->id3buf[0] = (unsigned char) ((newhead >> 24) & 0xff);
  563.                         fr->id3buf[1] = (unsigned char) ((newhead >> 16) & 0xff);
  564.                         fr->id3buf[2] = (unsigned char) ((newhead >> 8)  & 0xff);
  565.                         fr->id3buf[3] = (unsigned char) ( newhead        & 0xff);
  566.                         if((ret=fr->rd->fullread(fr,fr->id3buf+4,124)) < 0){ debug("need more?"); goto read_frame_bad; }
  567.                         fr->metaflags  |= MPG123_NEW_ID3|MPG123_ID3;
  568.                         fr->rdat.flags |= READER_ID3TAG; /* that marks id3v1 */
  569.                         if (VERBOSE2) fprintf(stderr,"Note: Skipped ID3 Tag!\n");
  570.                         goto read_again;
  571.                 }
  572.                 /* duplicated code from above! */
  573.                 /* check for id3v2; first three bytes (of 4) are "ID3" */
  574.                 if((newhead & (unsigned long) 0xffffff00) == (unsigned long) 0x49443300)
  575.                 {
  576.                         int id3length = 0;
  577.                         id3length = parse_new_id3(fr, newhead);
  578.                         if(id3length < 0){ debug("need more?"); ret = id3length; goto read_frame_bad; }
  579.  
  580.                         fr->metaflags  |= MPG123_NEW_ID3|MPG123_ID3;
  581.                         goto read_again;
  582.                 }
  583.                 else if(NOQUIET && fr->silent_resync == 0)
  584.                 {
  585.                         fprintf(stderr,"Note: Illegal Audio-MPEG-Header 0x%08lx at offset 0x%lx.\n",
  586.                                 newhead, (long unsigned int)fr->rd->tell(fr)-4);
  587.                 }
  588.  
  589.                 if(NOQUIET && (newhead & 0xffffff00) == ('b'<<24)+('m'<<16)+('p'<<8)) fprintf(stderr,"Note: Could be a BMP album art.\n");
  590.                 /* Do resync if not forbidden by flag.
  591.                 I used to have a check for not-icy-meta here, but concluded that the desync issues came from a reader bug, not the stream. */
  592.                 if( !(fr->p.flags & MPG123_NO_RESYNC) )
  593.                 {
  594.                         long try = 0;
  595.                         long limit = fr->p.resync_limit;
  596.                        
  597.                         /* If a resync is needed the bitreservoir of previous frames is no longer valid */
  598.                         fr->bitreservoir = 0;
  599.  
  600.                         /* TODO: make this more robust, I'd like to cat two mp3 fragments together (in a dirty way) and still have mpg123 beign able to decode all it somehow. */
  601.                         if(NOQUIET && fr->silent_resync == 0) fprintf(stderr, "Note: Trying to resync...\n");
  602.                         /* Read more bytes until we find something that looks
  603.                          reasonably like a valid header.  This is not a
  604.                          perfect strategy, but it should get us back on the
  605.                          track within a short time (and hopefully without
  606.                          too much distortion in the audio output).  */
  607.                         do
  608.                         {
  609.                                 ++try;
  610.                                 if(limit >= 0 && try >= limit) break;                          
  611.  
  612.                                 if((ret=fr->rd->head_shift(fr,&newhead)) <= 0)
  613.                                 {
  614.                                         debug("need more?");
  615.                                         if(NOQUIET) fprintf (stderr, "Note: Hit end of (available) data during resync.\n");
  616.  
  617.                                         goto read_frame_bad;
  618.                                 }
  619.                                 if(VERBOSE3) debug3("resync try %li at 0x%lx, got newhead 0x%08lx", try, (unsigned long)fr->rd->tell(fr),  newhead);
  620.  
  621.                                 if(!fr->oldhead)
  622.                                 {
  623.                                         debug("going to init_resync...");
  624.                                         goto init_resync;       /* "considered harmful", eh? */
  625.                                 }
  626.                                 /* we should perhaps collect a list of valid headers that occured in file... there can be more */
  627.                                 /* Michael's new resync routine seems to work better with the one frame readahead (and some input buffering?) */
  628.                         } while
  629.                         (
  630.                                 !head_check(newhead) /* Simply check for any valid header... we have the readahead to get it straight now(?) */
  631.                                 /*   (newhead & HDRCMPMASK) != (fr->oldhead & HDRCMPMASK)
  632.                                 && (newhead & HDRCMPMASK) != (fr->firsthead & HDRCMPMASK)*/
  633.                         );
  634.                         /* too many false positives
  635.                         }while (!(head_check(newhead) && decode_header(fr, newhead))); */
  636.                         if(NOQUIET && fr->silent_resync == 0) fprintf (stderr, "Note: Skipped %li bytes in input.\n", try);
  637.  
  638.                         if(limit >= 0 && try >= limit)
  639.                         {
  640.                                 if(NOQUIET)
  641.                                 error1("Giving up resync after %li bytes - your stream is not nice... (maybe increasing resync limit could help).", try);
  642.  
  643.                                 fr->err = MPG123_RESYNC_FAIL;
  644.                                 return READER_ERROR;
  645.                         }
  646.                         else
  647.                         {
  648.                                 debug1("Found valid header 0x%lx... unsetting firsthead to reinit stream.", newhead);
  649.                                 fr->firsthead = 0;
  650.                                 goto init_resync;
  651.                         }
  652.                 }
  653.                 else
  654.                 {
  655.                         if(NOQUIET) error("not attempting to resync...");
  656.  
  657.                         fr->err = MPG123_OUT_OF_SYNC;
  658.                         return READER_ERROR;
  659.                 }
  660.         }
  661.  
  662.         if (!fr->firsthead)
  663.         {
  664.                 if(!decode_header(fr,newhead))
  665.                 {
  666.                         if(NOQUIET) error("decode header failed before first valid one, going to read again");
  667.  
  668.                         goto read_again;
  669.                 }
  670.         }
  671.         else
  672.         if(!decode_header(fr,newhead))
  673.         {
  674.                 if(NOQUIET) error("decode header failed - goto resync");
  675.                 /* return 0; */
  676.                 goto init_resync;
  677.         }
  678.  
  679.         /* if filepos is invalid, so is framepos */
  680.         framepos = fr->rd->tell(fr) - 4;
  681.         /* flip/init buffer for Layer 3 */
  682.         {
  683.                 unsigned char *newbuf = fr->bsspace[fr->bsnum]+512;
  684.                 /* read main data into memory */
  685.                 if((ret=fr->rd->read_frame_body(fr,newbuf,fr->framesize))<0)
  686.                 {
  687.                         /* if failed: flip back */
  688.                         debug("need more?");
  689.                         goto read_frame_bad;
  690.                 }
  691.                 fr->bsbufold = fr->bsbuf;
  692.                 fr->bsbuf = newbuf;
  693.         }
  694.         fr->bsnum = (fr->bsnum + 1) & 1;
  695.  
  696.         if(!fr->firsthead)
  697.         {
  698.                 fr->firsthead = newhead; /* _now_ it's time to store it... the first real header */
  699.                 /* This is the first header of our current stream segment.
  700.                    It is only the actual first header of the whole stream when fr->num is still below zero!
  701.                    Think of resyncs where firsthead has been reset for format flexibility. */
  702.                 if(fr->num < 0)
  703.                 {
  704.                         fr->audio_start = framepos;
  705.                         /* Only check for LAME  tag at beginning of whole stream
  706.                            ... when there indeed is one in between, it's the user's problem. */
  707.                         if(fr->lay == 3 && check_lame_tag(fr) == 1)
  708.                         { /* ...in practice, Xing/LAME tags are layer 3 only. */
  709.                                 if(fr->rd->forget != NULL) fr->rd->forget(fr);
  710.  
  711.                                 fr->oldhead = 0;
  712.                                 goto read_again;
  713.                         }
  714.                         /* now adjust volume */
  715.                         do_rva(fr);
  716.                 }
  717.  
  718.                 debug2("fr->firsthead: %08lx, audio_start: %li", fr->firsthead, (long int)fr->audio_start);
  719.         }
  720.  
  721.   fr->bitindex = 0;
  722.   fr->wordpointer = (unsigned char *) fr->bsbuf;
  723.         /* Question: How bad does the floating point value get with repeated recomputation?
  724.            Also, considering that we can play the file or parts of many times. */
  725.         if(++fr->mean_frames != 0)
  726.         {
  727.                 fr->mean_framesize = ((fr->mean_frames-1)*fr->mean_framesize+compute_bpf(fr)) / fr->mean_frames ;
  728.         }
  729.         ++fr->num; /* 0 for first frame! */
  730.         debug4("Frame %li %08lx %i, next filepos=0x%lx",
  731.         (long)fr->num, newhead, fr->framesize, (long unsigned)fr->rd->tell(fr));
  732.         /* save for repetition */
  733.         if(fr->p.halfspeed && fr->lay == 3)
  734.         {
  735.                 debug("halfspeed - reusing old bsbuf ");
  736.                 memcpy (fr->ssave, fr->bsbuf, fr->ssize);
  737.         }
  738.  
  739.         /* index the position */
  740. #ifdef FRAME_INDEX
  741.         /* Keep track of true frame positions in our frame index.
  742.            but only do so when we are sure that the frame number is accurate... */
  743.         if(fr->accurate && FI_NEXT(fr->index, fr->num))
  744.         fi_add(&fr->index, framepos);
  745. #endif
  746.  
  747.         if(fr->silent_resync > 0) --fr->silent_resync;
  748.  
  749.         if(fr->rd->forget != NULL) fr->rd->forget(fr);
  750.  
  751.         fr->to_decode = fr->to_ignore = TRUE;
  752.         if(fr->error_protection) fr->crc = getbits(fr, 16); /* skip crc */
  753.  
  754.         return 1;
  755. read_frame_bad:
  756.         fr->silent_resync = 0;
  757.         if(fr->err == MPG123_OK) fr->err = MPG123_ERR_READER;
  758.         fr->framesize = oldsize;
  759.         fr->halfphase = oldphase;
  760.         return ret;
  761. }
  762.  
  763.  
  764. /*
  765.  * read ahead and find the next MPEG header, to guess framesize
  766.  * return value: guessed framesize
  767.  */
  768. static long guess_freeformat_framesize(mpg123_handle *fr)
  769. {
  770.         long i;
  771.         int ret;
  772.         unsigned long head;
  773.         if(!(fr->rdat.flags & (READER_SEEKABLE|READER_BUFFERED)))
  774.         {
  775.                 if(NOQUIET) error("Cannot look for freeformat frame size with non-seekable and non-buffered stream!");
  776.                 return -1;
  777.         }
  778.         /* FIXME: We need proper handling/passing of MPG123_NEED_MORE! */
  779.         if((ret=fr->rd->head_read(fr,&head))<=0)
  780.         return -1;
  781.  
  782.         /* We are already 4 bytes into it */
  783.         for(i=4;i<65536;i++) {
  784.                 if((ret=fr->rd->head_shift(fr,&head))<=0)
  785.                 {
  786.                         return -1;
  787.                 }
  788.                 if(head_check(head))
  789.                 {
  790.                         int sampling_frequency,mpeg25,lsf;
  791.                        
  792.                         if(head & (1<<20))
  793.                         {
  794.                                 lsf = (head & (1<<19)) ? 0x0 : 0x1;
  795.                                 mpeg25 = 0;
  796.                         }
  797.                         else
  798.                         {
  799.                                 lsf = 1;
  800.                                 mpeg25 = 1;
  801.                         }
  802.                        
  803.                         if(mpeg25)
  804.                                 sampling_frequency = 6 + ((head>>10)&0x3);
  805.                         else
  806.                                 sampling_frequency = ((head>>10)&0x3) + (lsf*3);
  807.                        
  808.                         if((lsf==fr->lsf) && (mpeg25==fr->mpeg25) && (sampling_frequency == fr->sampling_frequency))
  809.                         {
  810.                                 fr->rd->back_bytes(fr,i+1);
  811.                                 return i-3;
  812.                         }
  813.                 }
  814.         }
  815.         fr->rd->back_bytes(fr,i);
  816.         return -1;
  817. }
  818.  
  819.  
  820. /*
  821.  * decode a header and write the information
  822.  * into the frame structure
  823.  */
  824. static int decode_header(mpg123_handle *fr,unsigned long newhead)
  825. {
  826.         if(!head_check(newhead))
  827.         {
  828.                 if(NOQUIET) error("tried to decode obviously invalid header");
  829.  
  830.                 return 0;
  831.         }
  832.         if( newhead & (1<<20) )
  833.         {
  834.                 fr->lsf = (newhead & (1<<19)) ? 0x0 : 0x1;
  835.                 fr->mpeg25 = 0;
  836.         }
  837.         else
  838.         {
  839.                 fr->lsf = 1;
  840.                 fr->mpeg25 = 1;
  841.         }
  842.  
  843.         if(   (fr->p.flags & MPG123_NO_RESYNC) || !fr->oldhead
  844.            || (((fr->oldhead>>19)&0x3) ^ ((newhead>>19)&0x3))  )
  845.         {
  846.                 /* If "tryresync" is false, assume that certain
  847.                 parameters do not change within the stream!
  848.                 Force an update if lsf or mpeg25 settings
  849.                 have changed. */
  850.                 fr->lay = 4-((newhead>>17)&3);
  851.                 if( ((newhead>>10)&0x3) == 0x3)
  852.                 {
  853.                         if(NOQUIET) error("Stream error");
  854.  
  855.                         return 0; /* exit() here really is too much, isn't it? */
  856.                 }
  857.                 if(fr->mpeg25)
  858.                 fr->sampling_frequency = 6 + ((newhead>>10)&0x3);
  859.                 else
  860.                 fr->sampling_frequency = ((newhead>>10)&0x3) + (fr->lsf*3);
  861.         }
  862.  
  863.         #ifdef DEBUG
  864.         if((((newhead>>16)&0x1)^0x1) != fr->error_protection) debug("changed crc bit!");
  865.         #endif
  866.         fr->error_protection = ((newhead>>16)&0x1)^0x1; /* seen a file where this varies (old lame tag without crc, track with crc) */
  867.         fr->bitrate_index = ((newhead>>12)&0xf);
  868.         fr->padding   = ((newhead>>9)&0x1);
  869.         fr->extension = ((newhead>>8)&0x1);
  870.         fr->mode      = ((newhead>>6)&0x3);
  871.         fr->mode_ext  = ((newhead>>4)&0x3);
  872.         fr->copyright = ((newhead>>3)&0x1);
  873.         fr->original  = ((newhead>>2)&0x1);
  874.         fr->emphasis  = newhead & 0x3;
  875.         fr->freeformat = free_format_header(newhead);
  876.  
  877.         fr->stereo    = (fr->mode == MPG_MD_MONO) ? 1 : 2;
  878.  
  879.         fr->oldhead = newhead;
  880.        
  881.         /* we can't use tabsel_123 for freeformat, so trying to guess framesize... */
  882.         /* FIXME: We need proper handling/passing of MPG123_NEED_MORE! */
  883.         if(fr->freeformat)
  884.         {
  885.                 /* when we first encounter the frame with freeformat, guess framesize */
  886.                 if(fr->freeformat_framesize < 0)
  887.                 {
  888.                         fr->framesize = guess_freeformat_framesize(fr);
  889.                         if(fr->framesize > 0)
  890.                         {
  891.                                 fr->freeformat_framesize = fr->framesize - fr->padding;
  892.                         }
  893.                         else
  894.                         {
  895.                                 error("encountered free format header, but failed to guess framesize");
  896.                                 return 0;
  897.                         }
  898.                 }
  899.                 /* freeformat should be CBR, so the same framesize can be used at the 2nd reading or later */
  900.                 else
  901.                 {
  902.                         fr->framesize = fr->freeformat_framesize + fr->padding;
  903.                 }
  904.         }
  905.  
  906.         switch(fr->lay)
  907.         {
  908. #ifndef NO_LAYER1
  909.                 case 1:
  910.                         fr->do_layer = do_layer1;
  911.                         if(!fr->freeformat)
  912.                         {
  913.                                 fr->framesize  = (long) tabsel_123[fr->lsf][0][fr->bitrate_index] * 12000;
  914.                                 fr->framesize /= freqs[fr->sampling_frequency];
  915.                                 fr->framesize  = ((fr->framesize+fr->padding)<<2)-4;
  916.                         }
  917.                 break;
  918. #endif
  919. #ifndef NO_LAYER2
  920.                 case 2:
  921.                         fr->do_layer = do_layer2;
  922.                         if(!fr->freeformat)
  923.                         {
  924.                                 debug2("bitrate index: %i (%i)", fr->bitrate_index, tabsel_123[fr->lsf][1][fr->bitrate_index] );
  925.                                 fr->framesize = (long) tabsel_123[fr->lsf][1][fr->bitrate_index] * 144000;
  926.                                 fr->framesize /= freqs[fr->sampling_frequency];
  927.                                 fr->framesize += fr->padding - 4;
  928.                         }
  929.                 break;
  930. #endif
  931. #ifndef NO_LAYER3
  932.                 case 3:
  933.                         fr->do_layer = do_layer3;
  934.                         if(fr->lsf)
  935.                         fr->ssize = (fr->stereo == 1) ? 9 : 17;
  936.                         else
  937.                         fr->ssize = (fr->stereo == 1) ? 17 : 32;
  938.  
  939.                         if(fr->error_protection)
  940.                         fr->ssize += 2;
  941.  
  942.                         if(!fr->freeformat)
  943.                         {
  944.                                 fr->framesize  = (long) tabsel_123[fr->lsf][2][fr->bitrate_index] * 144000;
  945.                                 fr->framesize /= freqs[fr->sampling_frequency]<<(fr->lsf);
  946.                                 fr->framesize = fr->framesize + fr->padding - 4;
  947.                         }
  948.                 break;
  949. #endif
  950.                 default:
  951.                         if(NOQUIET) error1("Layer type %i not supported in this build!", fr->lay);
  952.  
  953.                         return 0;
  954.         }
  955.         if (fr->framesize > MAXFRAMESIZE)
  956.         {
  957.                 if(NOQUIET) error1("Frame size too big: %d", fr->framesize+4-fr->padding);
  958.  
  959.                 return (0);
  960.         }
  961.         return 1;
  962. }
  963.  
  964. void set_pointer(mpg123_handle *fr, long backstep)
  965. {
  966.         fr->wordpointer = fr->bsbuf + fr->ssize - backstep;
  967.         if (backstep)
  968.         memcpy(fr->wordpointer,fr->bsbufold+fr->fsizeold-backstep,backstep);
  969.  
  970.         fr->bitindex = 0;
  971. }
  972.  
  973. /********************************/
  974.  
  975. double compute_bpf(mpg123_handle *fr)
  976. {
  977.         double bpf;
  978.  
  979.         switch(fr->lay)
  980.         {
  981.                 case 1:
  982.                         bpf = tabsel_123[fr->lsf][0][fr->bitrate_index];
  983.                         bpf *= 12000.0 * 4.0;
  984.                         bpf /= freqs[fr->sampling_frequency] <<(fr->lsf);
  985.                 break;
  986.                 case 2:
  987.                 case 3:
  988.                         bpf = tabsel_123[fr->lsf][fr->lay-1][fr->bitrate_index];
  989.                         bpf *= 144000;
  990.                         bpf /= freqs[fr->sampling_frequency] << (fr->lsf);
  991.                 break;
  992.                 default:
  993.                         bpf = 1.0;
  994.         }
  995.  
  996.         return bpf;
  997. }
  998.  
  999. double attribute_align_arg mpg123_tpf(mpg123_handle *fr)
  1000. {
  1001.         static int bs[4] = { 0,384,1152,1152 };
  1002.         double tpf;
  1003.         if(fr == NULL) return -1;
  1004.  
  1005.         tpf = (double) bs[fr->lay];
  1006.         tpf /= freqs[fr->sampling_frequency] << (fr->lsf);
  1007.         return tpf;
  1008. }
  1009.  
  1010. int attribute_align_arg mpg123_position(mpg123_handle *fr, off_t no, off_t buffsize,
  1011.         off_t  *current_frame,   off_t  *frames_left,
  1012.         double *current_seconds, double *seconds_left)
  1013. {
  1014.         double tpf;
  1015.         double dt = 0.0;
  1016.         off_t cur, left;
  1017.         double curs, lefts;
  1018.  
  1019.         if(!fr || !fr->rd) /* Isn't this too paranoid? */
  1020.         {
  1021.                 debug("reader troubles!");
  1022.                 return MPG123_ERR;
  1023.         }
  1024.  
  1025.         no += fr->num; /* no starts out as offset */
  1026.         cur = no;
  1027.         tpf = mpg123_tpf(fr);
  1028.         if(buffsize > 0 && fr->af.rate > 0 && fr->af.channels > 0)
  1029.         {
  1030.                 dt = (double) buffsize / fr->af.rate / fr->af.channels;
  1031.                 if(fr->af.encoding & MPG123_ENC_16) dt *= 0.5;
  1032.         }
  1033.  
  1034.         left = 0;
  1035.  
  1036.         if((fr->track_frames != 0) && (fr->track_frames >= fr->num)) left = no < fr->track_frames ? fr->track_frames - no : 0;
  1037.         else
  1038.         if(fr->rdat.filelen >= 0)
  1039.         {
  1040.                 double bpf;
  1041.                 off_t t = fr->rd->tell(fr);
  1042.                 bpf = fr->mean_framesize ? fr->mean_framesize : compute_bpf(fr);
  1043.                 left = (off_t)((double)(fr->rdat.filelen-t)/bpf);
  1044.                 /* no can be different for prophetic purposes, file pointer is always associated with fr->num! */
  1045.                 if(fr->num != no)
  1046.                 {
  1047.                         if(fr->num > no) left += fr->num - no;
  1048.                         else
  1049.                         {
  1050.                                 if(left >= (no - fr->num)) left -= no - fr->num;
  1051.                                 else left = 0; /* uh, oh! */
  1052.                         }
  1053.                 }
  1054.                 /* I totally don't understand why we should re-estimate the given correct(?) value */
  1055.                 /* fr->num = (unsigned long)((double)t/bpf); */
  1056.         }
  1057.  
  1058.         /* beginning with 0 or 1?*/
  1059.         curs = (double) no*tpf-dt;
  1060.         lefts = (double)left*tpf+dt;
  1061. #if 0
  1062.         curs = curs < 0 ? 0.0 : curs;
  1063. #endif
  1064.         if(left < 0 || lefts < 0)
  1065.         { /* That is the case for non-seekable streams. */
  1066.                 left  = 0;
  1067.                 lefts = 0.0;
  1068.         }
  1069.         if(current_frame != NULL) *current_frame = cur;
  1070.         if(frames_left   != NULL) *frames_left   = left;
  1071.         if(current_seconds != NULL) *current_seconds = curs;
  1072.         if(seconds_left    != NULL) *seconds_left   = lefts;
  1073.         return MPG123_OK;
  1074. }
  1075.  
  1076. int get_songlen(mpg123_handle *fr,int no)
  1077. {
  1078.         double tpf;
  1079.        
  1080.         if(!fr)
  1081.                 return 0;
  1082.        
  1083.         if(no < 0) {
  1084.                 if(!fr->rd || fr->rdat.filelen < 0)
  1085.                         return 0;
  1086.                 no = (int) ((double) fr->rdat.filelen / compute_bpf(fr));
  1087.         }
  1088.  
  1089.         tpf = mpg123_tpf(fr);
  1090.         return (int) (no*tpf);
  1091. }
  1092.