readers.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. #include <stdlib.h>
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <fcntl.h>
  5. #include "mpg123.h"
  6. #include "buffer.h"
  7. #include "common.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. static int get_fileinfo(struct reader *,char *buf);
  15. /*******************************************************************
  16. * stream based operation
  17. */
  18. static int fullread(int fd,unsigned char *buf,int count)
  19. {
  20. int ret,cnt=0;
  21. while(cnt < count) {
  22. ret = read(fd,buf+cnt,count-cnt);
  23. if(ret < 0)
  24. return ret;
  25. if(ret == 0)
  26. break;
  27. cnt += ret;
  28. }
  29. return cnt;
  30. }
  31. static int default_init(struct reader *rds)
  32. {
  33. char buf[128];
  34. rds->filepos = 0;
  35. rds->filelen = get_fileinfo(rds,buf);
  36. if(rds->filelen > 0) {
  37. if(!strncmp(buf,"TAG",3)) {
  38. rds->flags |= READER_ID3TAG;
  39. memcpy(rds->id3buf,buf,128);
  40. }
  41. }
  42. return 0;
  43. }
  44. void stream_close(struct reader *rds)
  45. {
  46. if (rds->flags & READER_FD_OPENED)
  47. close(rds->filept);
  48. }
  49. /****************************************
  50. * HACK,HACK,HACK: step back <num> frames
  51. * can only work if the 'stream' isn't a real stream but a file
  52. */
  53. static int stream_back_bytes(struct reader *rds,int bytes)
  54. {
  55. if(lseek(rds->filept,-bytes,SEEK_CUR) < 0)
  56. return -1;
  57. if(param.usebuffer)
  58. buffer_resync();
  59. return 0;
  60. }
  61. static int stream_back_frame(struct reader *rds,struct frame *fr,int num)
  62. {
  63. long bytes;
  64. unsigned char buf[4];
  65. unsigned long newhead;
  66. if(!firsthead)
  67. return 0;
  68. bytes = (fr->framesize+8)*(num+2);
  69. /* Skipping back/forth requires a bit more work in buffered mode.
  70. * See mapped_back_frame().
  71. */
  72. if(param.usebuffer)
  73. bytes += (long)(xfermem_get_usedspace(buffermem) /
  74. (buffermem->buf[0] * buffermem->buf[1]
  75. * (buffermem->buf[2] & AUDIO_FORMAT_MASK ?
  76. 16.0 : 8.0 ))
  77. * (tabsel_123[fr->lsf][fr->lay-1][fr->bitrate_index] << 10));
  78. /*
  79. bytes += (long)(compute_buffer_offset(fr)*compute_bpf(fr));
  80. */
  81. if(lseek(rds->filept,-bytes,SEEK_CUR) < 0)
  82. return -1;
  83. if(fullread(rds->filept,buf,4) != 4)
  84. return -1;
  85. newhead = (buf[0]<<24) + (buf[1]<<16) + (buf[2]<<8) + buf[3];
  86. while( (newhead & HDRCMPMASK) != (firsthead & HDRCMPMASK) ) {
  87. if(fullread(rds->filept,buf,1) != 1)
  88. return -1;
  89. newhead <<= 8;
  90. newhead |= buf[0];
  91. newhead &= 0xffffffff;
  92. }
  93. if( lseek(rds->filept,-4,SEEK_CUR) < 0)
  94. return -1;
  95. read_frame(fr);
  96. read_frame(fr);
  97. if(fr->lay == 3) {
  98. set_pointer(512);
  99. }
  100. if(param.usebuffer)
  101. buffer_resync();
  102. return 0;
  103. }
  104. static int stream_head_read(struct reader *rds,unsigned long *newhead)
  105. {
  106. unsigned char hbuf[4];
  107. if(fullread(rds->filept,hbuf,4) != 4)
  108. return FALSE;
  109. *newhead = ((unsigned long) hbuf[0] << 24) |
  110. ((unsigned long) hbuf[1] << 16) |
  111. ((unsigned long) hbuf[2] << 8) |
  112. (unsigned long) hbuf[3];
  113. return TRUE;
  114. }
  115. static int stream_head_shift(struct reader *rds,unsigned long *head)
  116. {
  117. unsigned char hbuf;
  118. if(fullread(rds->filept,&hbuf,1) != 1)
  119. return 0;
  120. *head <<= 8;
  121. *head |= hbuf;
  122. *head &= 0xffffffff;
  123. return 1;
  124. }
  125. static int stream_skip_bytes(struct reader *rds,int len)
  126. {
  127. if (!param.usebuffer)
  128. return lseek(rds->filept,len,SEEK_CUR);
  129. else {
  130. int ret = lseek(rds->filept,len,SEEK_CUR);
  131. buffer_resync();
  132. return ret;
  133. }
  134. }
  135. static int stream_read_frame_body(struct reader *rds,unsigned char *buf,
  136. int size)
  137. {
  138. long l;
  139. if( (l=fullread(rds->filept,buf,size)) != size)
  140. {
  141. if(l <= 0)
  142. return 0;
  143. memset(buf+l,0,size-l);
  144. }
  145. return 1;
  146. }
  147. static long stream_tell(struct reader *rds)
  148. {
  149. return lseek(rds->filept,0,SEEK_CUR);
  150. }
  151. static void stream_rewind(struct reader *rds)
  152. {
  153. lseek(rds->filept,0,SEEK_SET);
  154. if(param.usebuffer)
  155. buffer_resync();
  156. }
  157. /*
  158. * returns length of a file (if filept points to a file)
  159. * reads the last 128 bytes information into buffer
  160. */
  161. static int get_fileinfo(struct reader *rds,char *buf)
  162. {
  163. int len;
  164. if((len=lseek(rds->filept,0,SEEK_END)) < 0) {
  165. return -1;
  166. }
  167. if(lseek(rds->filept,-128,SEEK_END) < 0)
  168. return -1;
  169. if(fullread(rds->filept,(unsigned char *)buf,128) != 128) {
  170. return -1;
  171. }
  172. if(!strncmp(buf,"TAG",3)) {
  173. len -= 128;
  174. }
  175. if(lseek(rds->filept,0,SEEK_SET) < 0)
  176. return -1;
  177. if(len <= 0)
  178. return -1;
  179. return len;
  180. }
  181. #ifdef READ_MMAP
  182. /*********************************************************+
  183. * memory mapped operation
  184. *
  185. */
  186. static unsigned char *mapbuf;
  187. static unsigned char *mappnt;
  188. static unsigned char *mapend;
  189. static int mapped_init(struct reader *rds)
  190. {
  191. long len;
  192. char buf[128];
  193. len = get_fileinfo(rds,buf);
  194. if(len < 0)
  195. return -1;
  196. if(!strncmp(buf,"TAG",3)) {
  197. rds->flags |= READER_ID3TAG;
  198. memcpy(rds->id3buf,buf,128);
  199. }
  200. mappnt = mapbuf = (unsigned char *)
  201. mmap(NULL, len, PROT_READ, MAP_SHARED , rds->filept, 0);
  202. if(!mapbuf || mapbuf == MAP_FAILED)
  203. return -1;
  204. mapend = mapbuf + len;
  205. if(param.verbose > 1)
  206. fprintf(stderr,"Using memory mapped IO for this stream.\n");
  207. rds->filelen = len;
  208. return 0;
  209. }
  210. static void mapped_rewind(struct reader *rds)
  211. {
  212. mappnt = mapbuf;
  213. if (param.usebuffer)
  214. buffer_resync();
  215. }
  216. static void mapped_close(struct reader *rds)
  217. {
  218. munmap((void *)mapbuf,mapend-mapbuf);
  219. if (rds->flags & READER_FD_OPENED)
  220. close(rds->filept);
  221. }
  222. static int mapped_head_read(struct reader *rds,unsigned long *newhead)
  223. {
  224. unsigned long nh;
  225. if(mappnt + 4 > mapend)
  226. return FALSE;
  227. nh = (*mappnt++) << 24;
  228. nh |= (*mappnt++) << 16;
  229. nh |= (*mappnt++) << 8;
  230. nh |= (*mappnt++) ;
  231. *newhead = nh;
  232. return TRUE;
  233. }
  234. static int mapped_head_shift(struct reader *rds,unsigned long *head)
  235. {
  236. if(mappnt + 1 > mapend)
  237. return FALSE;
  238. *head <<= 8;
  239. *head |= *mappnt++;
  240. *head &= 0xffffffff;
  241. return TRUE;
  242. }
  243. static int mapped_skip_bytes(struct reader *rds,int len)
  244. {
  245. if(mappnt + len > mapend)
  246. return FALSE;
  247. mappnt += len;
  248. if (param.usebuffer)
  249. buffer_resync();
  250. return TRUE;
  251. }
  252. static int mapped_read_frame_body(struct reader *rds,unsigned char *buf,
  253. int size)
  254. {
  255. if(size <= 0) {
  256. fprintf(stderr,"Ouch. Read_frame called with size <= 0\n");
  257. return FALSE;
  258. }
  259. if(mappnt + size > mapend)
  260. return FALSE;
  261. memcpy(buf,mappnt,size);
  262. mappnt += size;
  263. return TRUE;
  264. }
  265. static int mapped_back_bytes(struct reader *rds,int bytes)
  266. {
  267. if( (mappnt - bytes) < mapbuf || (mappnt - bytes + 4) > mapend)
  268. return -1;
  269. mappnt -= bytes;
  270. if(param.usebuffer)
  271. buffer_resync();
  272. return 0;
  273. }
  274. static int mapped_back_frame(struct reader *rds,struct frame *fr,int num)
  275. {
  276. long bytes;
  277. unsigned long newhead;
  278. if(!firsthead)
  279. return 0;
  280. bytes = (fr->framesize+8)*(num+2);
  281. /* Buffered mode is a bit trickier. From the size of the buffered
  282. * output audio stream we have to make a guess at the number of frames
  283. * this corresponds to.
  284. */
  285. if(param.usebuffer)
  286. bytes += (long)(xfermem_get_usedspace(buffermem) /
  287. (buffermem->buf[0] * buffermem->buf[1]
  288. * (buffermem->buf[2] & AUDIO_FORMAT_MASK ?
  289. 16.0 : 8.0 ))
  290. * (tabsel_123[fr->lsf][fr->lay-1][fr->bitrate_index] << 10));
  291. /*
  292. bytes += (long)(compute_buffer_offset(fr)*compute_bpf(fr));
  293. */
  294. if( (mappnt - bytes) < mapbuf || (mappnt - bytes + 4) > mapend)
  295. return -1;
  296. mappnt -= bytes;
  297. newhead = (mappnt[0]<<24) + (mappnt[1]<<16) + (mappnt[2]<<8) + mappnt[3];
  298. mappnt += 4;
  299. while( (newhead & HDRCMPMASK) != (firsthead & HDRCMPMASK) ) {
  300. if(mappnt + 1 > mapend)
  301. return -1;
  302. newhead <<= 8;
  303. newhead |= *mappnt++;
  304. newhead &= 0xffffffff;
  305. }
  306. mappnt -= 4;
  307. read_frame(fr);
  308. read_frame(fr);
  309. if(fr->lay == 3)
  310. set_pointer(512);
  311. if(param.usebuffer)
  312. buffer_resync();
  313. return 0;
  314. }
  315. static long mapped_tell(struct reader *rds)
  316. {
  317. return mappnt - mapbuf;
  318. }
  319. #endif
  320. /*****************************************************************
  321. * read frame helper
  322. */
  323. struct reader *rd;
  324. struct reader readers[] = {
  325. #ifdef READ_SYSTEM
  326. { system_init,
  327. NULL, /* filled in by system_init() */
  328. NULL,
  329. NULL,
  330. NULL,
  331. NULL,
  332. NULL,
  333. NULL,
  334. NULL } ,
  335. #endif
  336. #ifdef READ_MMAP
  337. { mapped_init,
  338. mapped_close,
  339. mapped_head_read,
  340. mapped_head_shift,
  341. mapped_skip_bytes,
  342. mapped_read_frame_body,
  343. mapped_back_bytes,
  344. mapped_back_frame,
  345. mapped_tell,
  346. mapped_rewind } ,
  347. #endif
  348. { default_init,
  349. stream_close,
  350. stream_head_read,
  351. stream_head_shift,
  352. stream_skip_bytes,
  353. stream_read_frame_body,
  354. stream_back_bytes,
  355. stream_back_frame,
  356. stream_tell,
  357. stream_rewind } ,
  358. { NULL, }
  359. };
  360. /* open the device to read the bit stream from it */
  361. void open_stream(char *bs_filenam,int fd)
  362. {
  363. int i;
  364. int filept_opened = 1;
  365. int filept;
  366. if (!bs_filenam) {
  367. if(fd < 0) {
  368. filept = 0;
  369. filept_opened = 0;
  370. }
  371. else
  372. filept = fd;
  373. }
  374. else if (!strncmp(bs_filenam, "http://", 7))
  375. filept = http_open(bs_filenam);
  376. #ifndef O_BINARY
  377. #define O_BINARY (0)
  378. #endif
  379. else if ( (filept = open(bs_filenam, O_RDONLY|O_BINARY)) < 0) {
  380. perror (bs_filenam);
  381. exit(1);
  382. }
  383. rd = NULL;
  384. for(i=0;;i++) {
  385. readers[i].filelen = -1;
  386. readers[i].filept = filept;
  387. readers[i].flags = 0;
  388. if(filept_opened)
  389. readers[i].flags |= READER_FD_OPENED;
  390. if(!readers[i].init) {
  391. fprintf(stderr,"Fatal error!\n");
  392. exit(1);
  393. }
  394. if(readers[i].init(readers+i) >= 0) {
  395. rd = &readers[i];
  396. break;
  397. }
  398. }
  399. if(rd && rd->flags & READER_ID3TAG) {
  400. print_id3_tag(rd->id3buf);
  401. }
  402. }