common.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  1. #include <ctype.h>
  2. #include <stdlib.h>
  3. #include <signal.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <sys/time.h>
  7. #include <fcntl.h>
  8. #ifdef READ_MMAP
  9. #include <sys/mman.h>
  10. #ifndef MAP_FAILED
  11. #define MAP_FAILED ( (void *) -1 )
  12. #endif
  13. #endif
  14. #include "mpg123.h"
  15. #include "genre.h"
  16. #include "common.h"
  17. int tabsel_123[2][3][16] = {
  18. { {0,32,64,96,128,160,192,224,256,288,320,352,384,416,448,},
  19. {0,32,48,56, 64, 80, 96,112,128,160,192,224,256,320,384,},
  20. {0,32,40,48, 56, 64, 80, 96,112,128,160,192,224,256,320,} },
  21. { {0,32,48,56,64,80,96,112,128,144,160,176,192,224,256,},
  22. {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,},
  23. {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,} }
  24. };
  25. long freqs[9] = { 44100, 48000, 32000, 22050, 24000, 16000 , 11025 , 12000 , 8000 };
  26. struct bitstream_info bsi;
  27. static int fsizeold=0,ssize;
  28. static unsigned char bsspace[2][MAXFRAMESIZE+512]; /* MAXFRAMESIZE */
  29. static unsigned char *bsbuf=bsspace[1],*bsbufold;
  30. static int bsnum=0;
  31. static unsigned long oldhead = 0;
  32. unsigned long firsthead=0;
  33. unsigned char *pcm_sample;
  34. int pcm_point = 0;
  35. int audiobufsize = AUDIOBUFSIZE;
  36. #ifdef VARMODESUPPORT
  37. /*
  38. * This is a dirty hack! It might burn your PC and kill your cat!
  39. * When in "varmode", specially formatted layer-3 mpeg files are
  40. * expected as input -- it will NOT work with standard mpeg files.
  41. * The reason for this:
  42. * Varmode mpeg files enable my own GUI player to perform fast
  43. * forward and backward functions, and to jump to an arbitrary
  44. * timestamp position within the file. This would be possible
  45. * with standard mpeg files, too, but it would be a lot harder to
  46. * implement.
  47. * A filter for converting standard mpeg to varmode mpeg is
  48. * available on request, but it's really not useful on its own.
  49. *
  50. * Oliver Fromme <oliver.fromme@heim3.tu-clausthal.de>
  51. * Mon Mar 24 00:04:24 MET 1997
  52. */
  53. int varmode = FALSE;
  54. int playlimit;
  55. #endif
  56. static int decode_header(struct frame *fr,unsigned long newhead);
  57. void audio_flush(int outmode, struct audio_info_struct *ai)
  58. {
  59. if (pcm_point) {
  60. switch (outmode) {
  61. case DECODE_FILE:
  62. write (OutputDescriptor, pcm_sample, pcm_point);
  63. break;
  64. case DECODE_AUDIO:
  65. audio_play_samples (ai, pcm_sample, pcm_point);
  66. break;
  67. case DECODE_BUFFER:
  68. write (buffer_fd[1], pcm_sample, pcm_point);
  69. break;
  70. case DECODE_WAV:
  71. case DECODE_CDR:
  72. case DECODE_AU:
  73. wav_write(pcm_sample, pcm_point);
  74. break;
  75. }
  76. pcm_point = 0;
  77. }
  78. }
  79. #if !defined(WIN32) && !defined(GENERIC)
  80. void (*catchsignal(int signum, void(*handler)()))()
  81. {
  82. struct sigaction new_sa;
  83. struct sigaction old_sa;
  84. #ifdef DONT_CATCH_SIGNALS
  85. printf ("Not catching any signals.\n");
  86. return ((void (*)()) -1);
  87. #endif
  88. new_sa.sa_handler = handler;
  89. sigemptyset(&new_sa.sa_mask);
  90. new_sa.sa_flags = 0;
  91. if (sigaction(signum, &new_sa, &old_sa) == -1)
  92. return ((void (*)()) -1);
  93. return (old_sa.sa_handler);
  94. }
  95. #endif
  96. void read_frame_init (void)
  97. {
  98. oldhead = 0;
  99. firsthead = 0;
  100. }
  101. int head_check(unsigned long head)
  102. {
  103. if( (head & 0xffe00000) != 0xffe00000)
  104. return FALSE;
  105. if(!((head>>17)&3))
  106. return FALSE;
  107. if( ((head>>12)&0xf) == 0xf)
  108. return FALSE;
  109. if( ((head>>10)&0x3) == 0x3 )
  110. return FALSE;
  111. if ((head & 0xffff0000) == 0xfffe0000)
  112. return FALSE;
  113. return TRUE;
  114. }
  115. /*****************************************************************
  116. * read next frame
  117. */
  118. int read_frame(struct frame *fr)
  119. {
  120. unsigned long newhead;
  121. static unsigned char ssave[34];
  122. fsizeold=fr->framesize; /* for Layer3 */
  123. if (param.halfspeed) {
  124. static int halfphase = 0;
  125. if (halfphase--) {
  126. bsi.bitindex = 0;
  127. bsi.wordpointer = (unsigned char *) bsbuf;
  128. if (fr->lay == 3)
  129. memcpy (bsbuf, ssave, ssize);
  130. return 1;
  131. }
  132. else
  133. halfphase = param.halfspeed - 1;
  134. }
  135. read_again:
  136. if(!rd->head_read(rd,&newhead))
  137. return FALSE;
  138. if(1 || oldhead != newhead || !oldhead)
  139. {
  140. init_resync:
  141. fr->header_change = 2;
  142. if(oldhead) {
  143. if((oldhead & 0xc00) == (newhead & 0xc00)) {
  144. if( (oldhead & 0xc0) == 0 && (newhead & 0xc0) == 0)
  145. fr->header_change = 1;
  146. else if( (oldhead & 0xc0) > 0 && (newhead & 0xc0) > 0)
  147. fr->header_change = 1;
  148. }
  149. }
  150. #ifdef SKIP_JUNK
  151. if(!firsthead && !head_check(newhead) ) {
  152. int i;
  153. fprintf(stderr,"Junk at the beginning %08lx\n",newhead);
  154. /* I even saw RIFF headers at the beginning of MPEG streams ;( */
  155. if(newhead == ('R'<<24)+('I'<<16)+('F'<<8)+'F') {
  156. if(!rd->head_read(rd,&newhead))
  157. return 0;
  158. while(newhead != ('d'<<24)+('a'<<16)+('t'<<8)+'a') {
  159. if(!rd->head_shift(rd,&newhead))
  160. return 0;
  161. }
  162. if(!rd->head_read(rd,&newhead))
  163. return 0;
  164. fprintf(stderr,"Skipped RIFF header!\n");
  165. goto read_again;
  166. }
  167. {
  168. /* step in byte steps through next 64K */
  169. for(i=0;i<65536;i++) {
  170. if(!rd->head_shift(rd,&newhead))
  171. return 0;
  172. if(head_check(newhead))
  173. break;
  174. #if 0
  175. fprintf(stderr,"%08lx ",newhead);
  176. #endif
  177. }
  178. if(i == 65536) {
  179. fprintf(stderr,"Giving up searching valid MPEG header\n");
  180. return 0;
  181. }
  182. }
  183. /*
  184. * should we additionaly check, whether a new frame starts at
  185. * the next expected position? (some kind of read ahead)
  186. * We could implement this easily, at least for files.
  187. */
  188. }
  189. #endif
  190. if( (newhead & 0xffe00000) != 0xffe00000) {
  191. if (!param.quiet)
  192. fprintf(stderr,"Illegal Audio-MPEG-Header 0x%08lx at offset 0x%lx.\n",
  193. newhead,rd->tell(rd)-4);
  194. /* and those ugly ID3 tags */
  195. if((newhead & 0xffffff00) == ('T'<<24)+('A'<<16)+('G'<<8)) {
  196. rd->skip_bytes(rd,124);
  197. fprintf(stderr,"Skipped ID3 Tag!\n");
  198. goto read_again;
  199. }
  200. if (param.tryresync) {
  201. int try = 0;
  202. /* Read more bytes until we find something that looks
  203. reasonably like a valid header. This is not a
  204. perfect strategy, but it should get us back on the
  205. track within a short time (and hopefully without
  206. too much distortion in the audio output). */
  207. do {
  208. try++;
  209. if(!rd->head_shift(rd,&newhead))
  210. return 0;
  211. if (!oldhead)
  212. goto init_resync; /* "considered harmful", eh? */
  213. } while ((newhead & HDRCMPMASK) != (oldhead & HDRCMPMASK)
  214. && (newhead & HDRCMPMASK) != (firsthead & HDRCMPMASK));
  215. if (!param.quiet)
  216. fprintf (stderr, "Skipped %d bytes in input.\n", try);
  217. }
  218. else
  219. return (0);
  220. }
  221. if (!firsthead) {
  222. if(!decode_header(fr,newhead))
  223. goto read_again;
  224. firsthead = newhead;
  225. }
  226. else
  227. if(!decode_header(fr,newhead))
  228. return 0;
  229. }
  230. else
  231. fr->header_change = 0;
  232. /* flip/init buffer for Layer 3 */
  233. bsbufold = bsbuf;
  234. bsbuf = bsspace[bsnum]+512;
  235. bsnum = (bsnum + 1) & 1;
  236. /* read main data into memory */
  237. if(!rd->read_frame_body(rd,bsbuf,fr->framesize))
  238. return 0;
  239. bsi.bitindex = 0;
  240. bsi.wordpointer = (unsigned char *) bsbuf;
  241. if (param.halfspeed && fr->lay == 3)
  242. memcpy (ssave, bsbuf, ssize);
  243. return 1;
  244. }
  245. /****************************************
  246. * HACK,HACK,HACK: step back <num> frames
  247. * can only work if the 'stream' isn't a real stream but a file
  248. */
  249. int back_frame(struct reader *rds,struct frame *fr,int num)
  250. {
  251. long bytes;
  252. unsigned long newhead;
  253. if(!firsthead)
  254. return 0;
  255. bytes = (fr->framesize+8)*(num+2);
  256. if(rds->back_bytes(rds,bytes) < 0)
  257. return -1;
  258. if(!rds->head_read(rds,&newhead))
  259. return -1;
  260. while( (newhead & HDRCMPMASK) != (firsthead & HDRCMPMASK) ) {
  261. if(!rds->head_shift(rds,&newhead))
  262. return -1;
  263. }
  264. if(rds->back_bytes(rds,4) <0)
  265. return -1;
  266. read_frame(fr);
  267. read_frame(fr);
  268. if(fr->lay == 3) {
  269. set_pointer(512);
  270. }
  271. return 0;
  272. }
  273. /*
  274. * decode a header and write the information
  275. * into the frame structure
  276. */
  277. static int decode_header(struct frame *fr,unsigned long newhead)
  278. {
  279. if(!head_check(newhead))
  280. return 0;
  281. if( newhead & (1<<20) ) {
  282. fr->lsf = (newhead & (1<<19)) ? 0x0 : 0x1;
  283. fr->mpeg25 = 0;
  284. }
  285. else {
  286. fr->lsf = 1;
  287. fr->mpeg25 = 1;
  288. }
  289. if (!param.tryresync || !oldhead) {
  290. /* If "tryresync" is true, assume that certain
  291. parameters do not change within the stream! */
  292. fr->lay = 4-((newhead>>17)&3);
  293. if( ((newhead>>10)&0x3) == 0x3) {
  294. fprintf(stderr,"Stream error\n");
  295. exit(1);
  296. }
  297. if(fr->mpeg25) {
  298. fr->sampling_frequency = 6 + ((newhead>>10)&0x3);
  299. }
  300. else
  301. fr->sampling_frequency = ((newhead>>10)&0x3) + (fr->lsf*3);
  302. fr->error_protection = ((newhead>>16)&0x1)^0x1;
  303. }
  304. fr->bitrate_index = ((newhead>>12)&0xf);
  305. fr->padding = ((newhead>>9)&0x1);
  306. fr->extension = ((newhead>>8)&0x1);
  307. fr->mode = ((newhead>>6)&0x3);
  308. fr->mode_ext = ((newhead>>4)&0x3);
  309. fr->copyright = ((newhead>>3)&0x1);
  310. fr->original = ((newhead>>2)&0x1);
  311. fr->emphasis = newhead & 0x3;
  312. fr->stereo = (fr->mode == MPG_MD_MONO) ? 1 : 2;
  313. oldhead = newhead;
  314. if(!fr->bitrate_index) {
  315. fprintf(stderr,"Free format not supported: (head %08lx)\n",newhead);
  316. return (0);
  317. }
  318. switch(fr->lay) {
  319. case 1:
  320. fr->do_layer = do_layer1;
  321. #ifdef VARMODESUPPORT
  322. if (varmode) {
  323. fprintf(stderr,"Sorry, layer-1 not supported in varmode.\n");
  324. return (0);
  325. }
  326. #endif
  327. fr->framesize = (long) tabsel_123[fr->lsf][0][fr->bitrate_index] * 12000;
  328. fr->framesize /= freqs[fr->sampling_frequency];
  329. fr->framesize = ((fr->framesize+fr->padding)<<2)-4;
  330. break;
  331. case 2:
  332. fr->do_layer = do_layer2;
  333. #ifdef VARMODESUPPORT
  334. if (varmode) {
  335. fprintf(stderr,"Sorry, layer-2 not supported in varmode.\n");
  336. return (0);
  337. }
  338. #endif
  339. fr->framesize = (long) tabsel_123[fr->lsf][1][fr->bitrate_index] * 144000;
  340. fr->framesize /= freqs[fr->sampling_frequency];
  341. fr->framesize += fr->padding - 4;
  342. break;
  343. case 3:
  344. fr->do_layer = do_layer3;
  345. if(fr->lsf)
  346. ssize = (fr->stereo == 1) ? 9 : 17;
  347. else
  348. ssize = (fr->stereo == 1) ? 17 : 32;
  349. if(fr->error_protection)
  350. ssize += 2;
  351. fr->framesize = (long) tabsel_123[fr->lsf][2][fr->bitrate_index] * 144000;
  352. fr->framesize /= freqs[fr->sampling_frequency]<<(fr->lsf);
  353. fr->framesize = fr->framesize + fr->padding - 4;
  354. break;
  355. default:
  356. fprintf(stderr,"Sorry, unknown layer type.\n");
  357. return (0);
  358. }
  359. return 1;
  360. }
  361. #ifdef MPG123_REMOTE
  362. void print_rheader(struct frame *fr)
  363. {
  364. static char *modes[4] = { "Stereo", "Joint-Stereo", "Dual-Channel", "Single-Channel" };
  365. static char *layers[4] = { "Unknown" , "I", "II", "III" };
  366. static char *mpeg_type[2] = { "1.0" , "2.0" };
  367. /* version, layer, freq, mode, channels, bitrate, BPF */
  368. fprintf(stderr,"@I %s %s %ld %s %d %d %d\n",
  369. mpeg_type[fr->lsf],layers[fr->lay],freqs[fr->sampling_frequency],
  370. modes[fr->mode],fr->stereo,
  371. tabsel_123[fr->lsf][fr->lay-1][fr->bitrate_index],
  372. fr->framesize+4);
  373. }
  374. #endif
  375. void print_header(struct frame *fr)
  376. {
  377. static char *modes[4] = { "Stereo", "Joint-Stereo", "Dual-Channel", "Single-Channel" };
  378. static char *layers[4] = { "Unknown" , "I", "II", "III" };
  379. fprintf(stderr,"MPEG %s, Layer: %s, Freq: %ld, mode: %s, modext: %d, BPF : %d\n",
  380. fr->mpeg25 ? "2.5" : (fr->lsf ? "2.0" : "1.0"),
  381. layers[fr->lay],freqs[fr->sampling_frequency],
  382. modes[fr->mode],fr->mode_ext,fr->framesize+4);
  383. fprintf(stderr,"Channels: %d, copyright: %s, original: %s, CRC: %s, emphasis: %d.\n",
  384. fr->stereo,fr->copyright?"Yes":"No",
  385. fr->original?"Yes":"No",fr->error_protection?"Yes":"No",
  386. fr->emphasis);
  387. fprintf(stderr,"Bitrate: %d Kbits/s, Extension value: %d\n",
  388. tabsel_123[fr->lsf][fr->lay-1][fr->bitrate_index],fr->extension);
  389. }
  390. void print_header_compact(struct frame *fr)
  391. {
  392. static char *modes[4] = { "stereo", "joint-stereo", "dual-channel", "mono" };
  393. static char *layers[4] = { "Unknown" , "I", "II", "III" };
  394. fprintf(stderr,"MPEG %s layer %s, %d kbit/s, %ld Hz %s\n",
  395. fr->mpeg25 ? "2.5" : (fr->lsf ? "2.0" : "1.0"),
  396. layers[fr->lay],
  397. tabsel_123[fr->lsf][fr->lay-1][fr->bitrate_index],
  398. freqs[fr->sampling_frequency], modes[fr->mode]);
  399. }
  400. void print_id3_tag(unsigned char *buf)
  401. {
  402. struct id3tag {
  403. char tag[3];
  404. char title[30];
  405. char artist[30];
  406. char album[30];
  407. char year[4];
  408. char comment[30];
  409. unsigned char genre;
  410. };
  411. struct id3tag *tag = (struct id3tag *) buf;
  412. char title[31]={0,};
  413. char artist[31]={0,};
  414. char album[31]={0,};
  415. char year[5]={0,};
  416. char comment[31]={0,};
  417. char genre[31]={0,};
  418. if(param.quiet)
  419. return;
  420. strncpy(title,tag->title,30);
  421. strncpy(artist,tag->artist,30);
  422. strncpy(album,tag->album,30);
  423. strncpy(year,tag->year,4);
  424. strncpy(comment,tag->comment,30);
  425. if ( tag->genre <= sizeof(genre_table)/sizeof(*genre_table) ) {
  426. strncpy(genre, genre_table[tag->genre], 30);
  427. } else {
  428. strncpy(genre,"Unknown",30);
  429. }
  430. fprintf(stderr,"Title : %-30s Artist: %s\n",title,artist);
  431. fprintf(stderr,"Album : %-30s Year : %4s\n",album,year);
  432. fprintf(stderr,"Comment: %-30s Genre : %s\n",comment,genre);
  433. }
  434. #if 0
  435. /* removed the strndup for better portability */
  436. /*
  437. * Allocate space for a new string containing the first
  438. * "num" characters of "src". The resulting string is
  439. * always zero-terminated. Returns NULL if malloc fails.
  440. */
  441. char *strndup (const char *src, int num)
  442. {
  443. char *dst;
  444. if (!(dst = (char *) malloc(num+1)))
  445. return (NULL);
  446. dst[num] = '\0';
  447. return (strncpy(dst, src, num));
  448. }
  449. #endif
  450. /*
  451. * Split "path" into directory and filename components.
  452. *
  453. * Return value is 0 if no directory was specified (i.e.
  454. * "path" does not contain a '/'), OR if the directory
  455. * is the same as on the previous call to this function.
  456. *
  457. * Return value is 1 if a directory was specified AND it
  458. * is different from the previous one (if any).
  459. */
  460. int split_dir_file (const char *path, char **dname, char **fname)
  461. {
  462. static char *lastdir = NULL;
  463. char *slashpos;
  464. if ((slashpos = strrchr(path, '/'))) {
  465. *fname = slashpos + 1;
  466. *dname = strdup(path); /* , 1 + slashpos - path); */
  467. if(!(*dname)) {
  468. perror("memory");
  469. exit(1);
  470. }
  471. (*dname)[1 + slashpos - path] = 0;
  472. if (lastdir && !strcmp(lastdir, *dname)) {
  473. /*** same as previous directory ***/
  474. free (*dname);
  475. *dname = lastdir;
  476. return 0;
  477. }
  478. else {
  479. /*** different directory ***/
  480. if (lastdir)
  481. free (lastdir);
  482. lastdir = *dname;
  483. return 1;
  484. }
  485. }
  486. else {
  487. /*** no directory specified ***/
  488. if (lastdir) {
  489. free (lastdir);
  490. lastdir = NULL;
  491. };
  492. *dname = NULL;
  493. *fname = (char *)path;
  494. return 0;
  495. }
  496. }
  497. void set_pointer(long backstep)
  498. {
  499. bsi.wordpointer = bsbuf + ssize - backstep;
  500. if (backstep)
  501. memcpy(bsi.wordpointer,bsbufold+fsizeold-backstep,backstep);
  502. bsi.bitindex = 0;
  503. }
  504. /********************************/
  505. double compute_bpf(struct frame *fr)
  506. {
  507. double bpf;
  508. switch(fr->lay) {
  509. case 1:
  510. bpf = tabsel_123[fr->lsf][0][fr->bitrate_index];
  511. bpf *= 12000.0 * 4.0;
  512. bpf /= freqs[fr->sampling_frequency] <<(fr->lsf);
  513. break;
  514. case 2:
  515. case 3:
  516. bpf = tabsel_123[fr->lsf][fr->lay-1][fr->bitrate_index]; bpf *= 144000;
  517. bpf /= freqs[fr->sampling_frequency] << (fr->lsf);
  518. break;
  519. default:
  520. bpf = 1.0;
  521. }
  522. return bpf;
  523. }
  524. double compute_tpf(struct frame *fr)
  525. {
  526. static int bs[4] = { 0,384,1152,1152 };
  527. double tpf;
  528. tpf = (double) bs[fr->lay];
  529. tpf /= freqs[fr->sampling_frequency] << (fr->lsf);
  530. return tpf;
  531. }
  532. /*
  533. * Returns number of frames queued up in output buffer, i.e.
  534. * offset between currently played and currently decoded frame.
  535. */
  536. long compute_buffer_offset(struct frame *fr)
  537. {
  538. long bufsize;
  539. /*
  540. * buffermem->buf[0] holds output sampling rate,
  541. * buffermem->buf[1] holds number of channels,
  542. * buffermem->buf[2] holds audio format of output.
  543. */
  544. if(!param.usebuffer || !(bufsize=xfermem_get_usedspace(buffermem))
  545. || !buffermem->buf[0] || !buffermem->buf[1])
  546. return 0;
  547. bufsize = (long)((double) bufsize / buffermem->buf[0] /
  548. buffermem->buf[1] / compute_tpf(fr));
  549. if((buffermem->buf[2] & AUDIO_FORMAT_MASK) == AUDIO_FORMAT_16)
  550. return bufsize/2;
  551. else
  552. return bufsize;
  553. }
  554. void print_stat(struct frame *fr,int no,long buffsize,struct audio_info_struct *ai)
  555. {
  556. double bpf,tpf,tim1,tim2;
  557. double dt = 0.0;
  558. int sno,rno;
  559. char outbuf[256];
  560. if(!rd || !fr)
  561. return;
  562. outbuf[0] = 0;
  563. #ifndef GENERIC
  564. {
  565. struct timeval t;
  566. fd_set serr;
  567. int n,errfd = fileno(stderr);
  568. t.tv_sec=t.tv_usec=0;
  569. FD_ZERO(&serr);
  570. FD_SET(errfd,&serr);
  571. n = select(errfd+1,NULL,&serr,NULL,&t);
  572. if(n <= 0)
  573. return;
  574. }
  575. #endif
  576. bpf = compute_bpf(fr);
  577. tpf = compute_tpf(fr);
  578. if(buffsize > 0 && ai && ai->rate > 0 && ai->channels > 0) {
  579. dt = (double) buffsize / ai->rate / ai->channels;
  580. if( (ai->format & AUDIO_FORMAT_MASK) == AUDIO_FORMAT_16)
  581. dt *= 0.5;
  582. }
  583. rno = 0;
  584. sno = no;
  585. if(rd->filelen >= 0) {
  586. long t = rd->tell(rd);
  587. rno = (int)((double)(rd->filelen-t)/bpf);
  588. sno = (int)((double)t/bpf);
  589. }
  590. sprintf(outbuf+strlen(outbuf),"\rFrame# %5d [%5d], ",sno,rno);
  591. tim1 = sno*tpf-dt;
  592. tim2 = rno*tpf+dt;
  593. #if 0
  594. tim1 = tim1 < 0 ? 0.0 : tim1;
  595. #endif
  596. tim2 = tim2 < 0 ? 0.0 : tim2;
  597. sprintf(outbuf+strlen(outbuf),"Time: %02u:%02u.%02u [%02u:%02u.%02u], ",
  598. (unsigned int)tim1/60,
  599. (unsigned int)tim1%60,
  600. (unsigned int)(tim1*100)%100,
  601. (unsigned int)tim2/60,
  602. (unsigned int)tim2%60,
  603. (unsigned int)(tim2*100)%100);
  604. if(param.usebuffer)
  605. sprintf(outbuf+strlen(outbuf),"[%8ld] ",(long)buffsize);
  606. write(fileno(stderr),outbuf,strlen(outbuf));
  607. #if 0
  608. fflush(out); /* hmm not really nec. */
  609. #endif
  610. }
  611. int get_songlen(struct frame *fr,int no)
  612. {
  613. double tpf;
  614. if(!fr)
  615. return 0;
  616. if(no < 0) {
  617. if(!rd || rd->filelen < 0)
  618. return 0;
  619. no = (double) rd->filelen / compute_bpf(fr);
  620. }
  621. tpf = compute_tpf(fr);
  622. return no*tpf;
  623. }