vorbisfile.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498
  1. /********************************************************************
  2. * *
  3. * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
  4. * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
  5. * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
  6. * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
  7. * *
  8. * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2001 *
  9. * by the XIPHOPHORUS Company http://www.xiph.org/ *
  10. * *
  11. ********************************************************************
  12. function: stdio-based convenience library for opening/seeking/decoding
  13. last mod: $Id: vorbisfile.c,v 1.46 2001/05/27 08:16:00 xiphmont Exp $
  14. ********************************************************************/
  15. #include <stdlib.h>
  16. #include <stdio.h>
  17. #include <errno.h>
  18. #include <string.h>
  19. #include <math.h>
  20. #include "vorbis/codec.h"
  21. #include "vorbis/vorbisfile.h"
  22. #include "os.h"
  23. #include "codec_internal.h"
  24. #include "misc.h"
  25. /* A 'chained bitstream' is a Vorbis bitstream that contains more than
  26. one logical bitstream arranged end to end (the only form of Ogg
  27. multiplexing allowed in a Vorbis bitstream; grouping [parallel
  28. multiplexing] is not allowed in Vorbis) */
  29. /* A Vorbis file can be played beginning to end (streamed) without
  30. worrying ahead of time about chaining (see decoder_example.c). If
  31. we have the whole file, however, and want random access
  32. (seeking/scrubbing) or desire to know the total length/time of a
  33. file, we need to account for the possibility of chaining. */
  34. /* We can handle things a number of ways; we can determine the entire
  35. bitstream structure right off the bat, or find pieces on demand.
  36. This example determines and caches structure for the entire
  37. bitstream, but builds a virtual decoder on the fly when moving
  38. between links in the chain. */
  39. /* There are also different ways to implement seeking. Enough
  40. information exists in an Ogg bitstream to seek to
  41. sample-granularity positions in the output. Or, one can seek by
  42. picking some portion of the stream roughly in the desired area if
  43. we only want course navigation through the stream. */
  44. /*************************************************************************
  45. * Many, many internal helpers. The intention is not to be confusing;
  46. * rampant duplication and monolithic function implementation would be
  47. * harder to understand anyway. The high level functions are last. Begin
  48. * grokking near the end of the file */
  49. /* read a little more data from the file/pipe into the ogg_sync framer */
  50. #define CHUNKSIZE 8500 /* a shade over 8k; anyone using pages well
  51. over 8k gets what they deserve */
  52. static long _get_data(OggVorbis_File *vf){
  53. errno=0;
  54. if(vf->datasource){
  55. char *buffer=ogg_sync_buffer(&vf->oy,CHUNKSIZE);
  56. long bytes=(vf->callbacks.read_func)(buffer,1,CHUNKSIZE,vf->datasource);
  57. if(bytes>0)ogg_sync_wrote(&vf->oy,bytes);
  58. if(bytes==0 && errno)return(-1);
  59. return(bytes);
  60. }else
  61. return(0);
  62. }
  63. /* save a tiny smidge of verbosity to make the code more readable */
  64. static void _seek_helper(OggVorbis_File *vf,long offset){
  65. if(vf->datasource){
  66. (vf->callbacks.seek_func)(vf->datasource, offset, SEEK_SET);
  67. vf->offset=offset;
  68. ogg_sync_reset(&vf->oy);
  69. }else{
  70. /* shouldn't happen unless someone writes a broken callback */
  71. return;
  72. }
  73. }
  74. /* The read/seek functions track absolute position within the stream */
  75. /* from the head of the stream, get the next page. boundary specifies
  76. if the function is allowed to fetch more data from the stream (and
  77. how much) or only use internally buffered data.
  78. boundary: -1) unbounded search
  79. 0) read no additional data; use cached only
  80. n) search for a new page beginning for n bytes
  81. return: <0) did not find a page (OV_FALSE, OV_EOF, OV_EREAD)
  82. n) found a page at absolute offset n */
  83. static long _get_next_page(OggVorbis_File *vf,ogg_page *og,int boundary){
  84. if(boundary>0)boundary+=vf->offset;
  85. while(1){
  86. long more;
  87. if(boundary>0 && vf->offset>=boundary)return(OV_FALSE);
  88. more=ogg_sync_pageseek(&vf->oy,og);
  89. if(more<0){
  90. /* skipped n bytes */
  91. vf->offset-=more;
  92. }else{
  93. if(more==0){
  94. /* send more paramedics */
  95. if(!boundary)return(OV_FALSE);
  96. {
  97. long ret=_get_data(vf);
  98. if(ret==0)return(OV_EOF);
  99. if(ret<0)return(OV_EREAD);
  100. }
  101. }else{
  102. /* got a page. Return the offset at the page beginning,
  103. advance the internal offset past the page end */
  104. long ret=vf->offset;
  105. vf->offset+=more;
  106. return(ret);
  107. }
  108. }
  109. }
  110. }
  111. /* find the latest page beginning before the current stream cursor
  112. position. Much dirtier than the above as Ogg doesn't have any
  113. backward search linkage. no 'readp' as it will certainly have to
  114. read. */
  115. /* returns offset or OV_EREAD, OV_FAULT */
  116. static long _get_prev_page(OggVorbis_File *vf,ogg_page *og){
  117. long begin=vf->offset;
  118. long ret;
  119. int offset=-1;
  120. while(offset==-1){
  121. begin-=CHUNKSIZE;
  122. _seek_helper(vf,begin);
  123. while(vf->offset<begin+CHUNKSIZE){
  124. ret=_get_next_page(vf,og,begin+CHUNKSIZE-vf->offset);
  125. if(ret==OV_EREAD)return(OV_EREAD);
  126. if(ret<0){
  127. break;
  128. }else{
  129. offset=ret;
  130. }
  131. }
  132. }
  133. /* we have the offset. Actually snork and hold the page now */
  134. _seek_helper(vf,offset);
  135. ret=_get_next_page(vf,og,CHUNKSIZE);
  136. if(ret<0)
  137. /* this shouldn't be possible */
  138. return(OV_EFAULT);
  139. return(offset);
  140. }
  141. /* finds each bitstream link one at a time using a bisection search
  142. (has to begin by knowing the offset of the lb's initial page).
  143. Recurses for each link so it can alloc the link storage after
  144. finding them all, then unroll and fill the cache at the same time */
  145. static int _bisect_forward_serialno(OggVorbis_File *vf,
  146. long begin,
  147. long searched,
  148. long end,
  149. long currentno,
  150. long m){
  151. long endsearched=end;
  152. long next=end;
  153. ogg_page og;
  154. long ret;
  155. /* the below guards against garbage seperating the last and
  156. first pages of two links. */
  157. while(searched<endsearched){
  158. long bisect;
  159. if(endsearched-searched<CHUNKSIZE){
  160. bisect=searched;
  161. }else{
  162. bisect=(searched+endsearched)/2;
  163. }
  164. _seek_helper(vf,bisect);
  165. ret=_get_next_page(vf,&og,-1);
  166. if(ret==OV_EREAD)return(OV_EREAD);
  167. if(ret<0 || ogg_page_serialno(&og)!=currentno){
  168. endsearched=bisect;
  169. if(ret>=0)next=ret;
  170. }else{
  171. searched=ret+og.header_len+og.body_len;
  172. }
  173. }
  174. _seek_helper(vf,next);
  175. ret=_get_next_page(vf,&og,-1);
  176. if(ret==OV_EREAD)return(OV_EREAD);
  177. if(searched>=end || ret<0){
  178. vf->links=m+1;
  179. vf->offsets=_ogg_malloc((m+2)*sizeof(ogg_int64_t));
  180. vf->offsets[m+1]=searched;
  181. }else{
  182. ret=_bisect_forward_serialno(vf,next,vf->offset,
  183. end,ogg_page_serialno(&og),m+1);
  184. if(ret==OV_EREAD)return(OV_EREAD);
  185. }
  186. vf->offsets[m]=begin;
  187. return(0);
  188. }
  189. /* uses the local ogg_stream storage in vf; this is important for
  190. non-streaming input sources */
  191. static int _fetch_headers(OggVorbis_File *vf,vorbis_info *vi,vorbis_comment *vc,
  192. long *serialno,ogg_page *og_ptr){
  193. ogg_page og;
  194. ogg_packet op;
  195. int i,ret=0;
  196. if(!og_ptr){
  197. ret=_get_next_page(vf,&og,CHUNKSIZE);
  198. if(ret==OV_EREAD)return(OV_EREAD);
  199. if(ret<0)return OV_ENOTVORBIS;
  200. og_ptr=&og;
  201. }
  202. if(serialno)*serialno=ogg_page_serialno(og_ptr);
  203. ogg_stream_init(&vf->os,ogg_page_serialno(og_ptr));
  204. vf->ready_state=STREAMSET;
  205. /* extract the initial header from the first page and verify that the
  206. Ogg bitstream is in fact Vorbis data */
  207. vorbis_info_init(vi);
  208. vorbis_comment_init(vc);
  209. i=0;
  210. while(i<3){
  211. ogg_stream_pagein(&vf->os,og_ptr);
  212. while(i<3){
  213. int result=ogg_stream_packetout(&vf->os,&op);
  214. if(result==0)break;
  215. if(result==-1){
  216. ret=OV_EBADHEADER;
  217. goto bail_header;
  218. }
  219. if((ret=vorbis_synthesis_headerin(vi,vc,&op))){
  220. goto bail_header;
  221. }
  222. i++;
  223. }
  224. if(i<3)
  225. if(_get_next_page(vf,og_ptr,1)<0){
  226. ret=OV_EBADHEADER;
  227. goto bail_header;
  228. }
  229. }
  230. return 0;
  231. bail_header:
  232. vorbis_info_clear(vi);
  233. vorbis_comment_clear(vc);
  234. ogg_stream_clear(&vf->os);
  235. return ret;
  236. }
  237. /* last step of the OggVorbis_File initialization; get all the
  238. vorbis_info structs and PCM positions. Only called by the seekable
  239. initialization (local stream storage is hacked slightly; pay
  240. attention to how that's done) */
  241. /* this is void and does not propogate errors up because we want to be
  242. able to open and use damaged bitstreams as well as we can. Just
  243. watch out for missing information for links in the OggVorbis_File
  244. struct */
  245. static void _prefetch_all_headers(OggVorbis_File *vf, long dataoffset){
  246. ogg_page og;
  247. int i,ret;
  248. vf->vi=_ogg_realloc(vf->vi,vf->links*sizeof(vorbis_info));
  249. vf->vc=_ogg_realloc(vf->vc,vf->links*sizeof(vorbis_info));
  250. vf->dataoffsets=_ogg_malloc(vf->links*sizeof(ogg_int64_t));
  251. vf->pcmlengths=_ogg_malloc(vf->links*sizeof(ogg_int64_t));
  252. vf->serialnos=_ogg_malloc(vf->links*sizeof(long));
  253. for(i=0;i<vf->links;i++){
  254. if(i==0){
  255. /* we already grabbed the initial header earlier. Just set the offset */
  256. vf->dataoffsets[i]=dataoffset;
  257. }else{
  258. /* seek to the location of the initial header */
  259. _seek_helper(vf,vf->offsets[i]);
  260. if(_fetch_headers(vf,vf->vi+i,vf->vc+i,NULL,NULL)<0){
  261. vf->dataoffsets[i]=-1;
  262. }else{
  263. vf->dataoffsets[i]=vf->offset;
  264. ogg_stream_clear(&vf->os);
  265. }
  266. }
  267. /* get the serial number and PCM length of this link. To do this,
  268. get the last page of the stream */
  269. {
  270. long end=vf->offsets[i+1];
  271. _seek_helper(vf,end);
  272. while(1){
  273. ret=_get_prev_page(vf,&og);
  274. if(ret<0){
  275. /* this should not be possible */
  276. vorbis_info_clear(vf->vi+i);
  277. vorbis_comment_clear(vf->vc+i);
  278. break;
  279. }
  280. if(ogg_page_granulepos(&og)!=-1){
  281. vf->serialnos[i]=ogg_page_serialno(&og);
  282. vf->pcmlengths[i]=ogg_page_granulepos(&og);
  283. break;
  284. }
  285. vf->offset=ret;
  286. }
  287. }
  288. }
  289. }
  290. static void _make_decode_ready(OggVorbis_File *vf){
  291. if(vf->ready_state!=STREAMSET)return;
  292. if(vf->seekable){
  293. vorbis_synthesis_init(&vf->vd,vf->vi+vf->current_link);
  294. }else{
  295. vorbis_synthesis_init(&vf->vd,vf->vi);
  296. }
  297. vorbis_block_init(&vf->vd,&vf->vb);
  298. vf->ready_state=INITSET;
  299. return;
  300. }
  301. static int _open_seekable2(OggVorbis_File *vf){
  302. long serialno=vf->current_serialno,end;
  303. long dataoffset=vf->offset;
  304. ogg_page og;
  305. /* we're partially open and have a first link header state in
  306. storage in vf */
  307. /* we can seek, so set out learning all about this file */
  308. (vf->callbacks.seek_func)(vf->datasource,0,SEEK_END);
  309. vf->offset=vf->end=(vf->callbacks.tell_func)(vf->datasource);
  310. /* We get the offset for the last page of the physical bitstream.
  311. Most OggVorbis files will contain a single logical bitstream */
  312. end=_get_prev_page(vf,&og);
  313. if(end<0){
  314. ov_clear(vf);
  315. return(end);
  316. }
  317. /* more than one logical bitstream? */
  318. if(ogg_page_serialno(&og)!=serialno){
  319. /* Chained bitstream. Bisect-search each logical bitstream
  320. section. Do so based on serial number only */
  321. if(_bisect_forward_serialno(vf,0,0,end+1,serialno,0)<0){
  322. ov_clear(vf);
  323. return(OV_EREAD);
  324. }
  325. }else{
  326. /* Only one logical bitstream */
  327. if(_bisect_forward_serialno(vf,0,end,end+1,serialno,0)){
  328. ov_clear(vf);
  329. return(OV_EREAD);
  330. }
  331. }
  332. /* the initial header memory is referenced by vf after; don't free it */
  333. _prefetch_all_headers(vf,dataoffset);
  334. return(ov_raw_seek(vf,0));
  335. }
  336. /* clear out the current logical bitstream decoder */
  337. static void _decode_clear(OggVorbis_File *vf){
  338. ogg_stream_clear(&vf->os);
  339. vorbis_dsp_clear(&vf->vd);
  340. vorbis_block_clear(&vf->vb);
  341. vf->ready_state=OPENED;
  342. vf->bittrack=0.f;
  343. vf->samptrack=0.f;
  344. }
  345. /* fetch and process a packet. Handles the case where we're at a
  346. bitstream boundary and dumps the decoding machine. If the decoding
  347. machine is unloaded, it loads it. It also keeps pcm_offset up to
  348. date (seek and read both use this. seek uses a special hack with
  349. readp).
  350. return: <0) error, OV_HOLE (lost packet) or OV_EOF
  351. 0) need more data (only if readp==0)
  352. 1) got a packet
  353. */
  354. static int _process_packet(OggVorbis_File *vf,int readp){
  355. ogg_page og;
  356. /* handle one packet. Try to fetch it from current stream state */
  357. /* extract packets from page */
  358. while(1){
  359. /* process a packet if we can. If the machine isn't loaded,
  360. neither is a page */
  361. if(vf->ready_state==INITSET){
  362. ogg_packet op;
  363. int result=ogg_stream_packetout(&vf->os,&op);
  364. ogg_int64_t granulepos;
  365. if(result==-1)return(OV_HOLE); /* hole in the data. */
  366. if(result>0){
  367. /* got a packet. process it */
  368. granulepos=op.granulepos;
  369. if(!vorbis_synthesis(&vf->vb,&op)){ /* lazy check for lazy
  370. header handling. The
  371. header packets aren't
  372. audio, so if/when we
  373. submit them,
  374. vorbis_synthesis will
  375. reject them */
  376. /* suck in the synthesis data and track bitrate */
  377. {
  378. int oldsamples=vorbis_synthesis_pcmout(&vf->vd,NULL);
  379. vorbis_synthesis_blockin(&vf->vd,&vf->vb);
  380. vf->samptrack+=vorbis_synthesis_pcmout(&vf->vd,NULL)-oldsamples;
  381. vf->bittrack+=op.bytes*8;
  382. }
  383. /* update the pcm offset. */
  384. if(granulepos!=-1 && !op.e_o_s){
  385. int link=(vf->seekable?vf->current_link:0);
  386. int i,samples;
  387. /* this packet has a pcm_offset on it (the last packet
  388. completed on a page carries the offset) After processing
  389. (above), we know the pcm position of the *last* sample
  390. ready to be returned. Find the offset of the *first*
  391. As an aside, this trick is inaccurate if we begin
  392. reading anew right at the last page; the end-of-stream
  393. granulepos declares the last frame in the stream, and the
  394. last packet of the last page may be a partial frame.
  395. So, we need a previous granulepos from an in-sequence page
  396. to have a reference point. Thus the !op.e_o_s clause
  397. above */
  398. samples=vorbis_synthesis_pcmout(&vf->vd,NULL);
  399. granulepos-=samples;
  400. for(i=0;i<link;i++)
  401. granulepos+=vf->pcmlengths[i];
  402. vf->pcm_offset=granulepos;
  403. }
  404. return(1);
  405. }
  406. }
  407. }
  408. if(vf->ready_state>=STREAMSET){
  409. if(!readp)return(0);
  410. if(_get_next_page(vf,&og,-1)<0)return(OV_EOF); /* eof.
  411. leave unitialized */
  412. /* bitrate tracking; add the header's bytes here, the body bytes
  413. are done by packet above */
  414. vf->bittrack+=og.header_len*8;
  415. /* has our decoding just traversed a bitstream boundary? */
  416. if(vf->ready_state==INITSET){
  417. if(vf->current_serialno!=ogg_page_serialno(&og)){
  418. _decode_clear(vf);
  419. if(!vf->seekable){
  420. vorbis_info_clear(vf->vi);
  421. vorbis_comment_clear(vf->vc);
  422. }
  423. }
  424. }
  425. }
  426. /* Do we need to load a new machine before submitting the page? */
  427. /* This is different in the seekable and non-seekable cases.
  428. In the seekable case, we already have all the header
  429. information loaded and cached; we just initialize the machine
  430. with it and continue on our merry way.
  431. In the non-seekable (streaming) case, we'll only be at a
  432. boundary if we just left the previous logical bitstream and
  433. we're now nominally at the header of the next bitstream
  434. */
  435. if(vf->ready_state!=INITSET){
  436. int link;
  437. if(vf->ready_state<STREAMSET){
  438. if(vf->seekable){
  439. vf->current_serialno=ogg_page_serialno(&og);
  440. /* match the serialno to bitstream section. We use this rather than
  441. offset positions to avoid problems near logical bitstream
  442. boundaries */
  443. for(link=0;link<vf->links;link++)
  444. if(vf->serialnos[link]==vf->current_serialno)break;
  445. if(link==vf->links)return(OV_EBADLINK); /* sign of a bogus
  446. stream. error out,
  447. leave machine
  448. uninitialized */
  449. vf->current_link=link;
  450. ogg_stream_init(&vf->os,vf->current_serialno);
  451. ogg_stream_reset(&vf->os);
  452. vf->ready_state=STREAMSET;
  453. }else{
  454. /* we're streaming */
  455. /* fetch the three header packets, build the info struct */
  456. _fetch_headers(vf,vf->vi,vf->vc,&vf->current_serialno,&og);
  457. vf->current_link++;
  458. link=0;
  459. }
  460. }
  461. _make_decode_ready(vf);
  462. }
  463. ogg_stream_pagein(&vf->os,&og);
  464. }
  465. }
  466. static int _fseek64_wrap(FILE *f,ogg_int64_t off,int whence){
  467. if(f==NULL)return(-1);
  468. return fseek(f,(int)off,whence);
  469. }
  470. static int _ov_open1(void *f,OggVorbis_File *vf,char *initial,
  471. long ibytes, ov_callbacks callbacks){
  472. long offset=(f?callbacks.seek_func(f,0,SEEK_CUR):-1);
  473. int ret;
  474. memset(vf,0,sizeof(OggVorbis_File));
  475. vf->datasource=f;
  476. vf->callbacks = callbacks;
  477. /* init the framing state */
  478. ogg_sync_init(&vf->oy);
  479. /* perhaps some data was previously read into a buffer for testing
  480. against other stream types. Allow initialization from this
  481. previously read data (as we may be reading from a non-seekable
  482. stream) */
  483. if(initial){
  484. char *buffer=ogg_sync_buffer(&vf->oy,ibytes);
  485. memcpy(buffer,initial,ibytes);
  486. ogg_sync_wrote(&vf->oy,ibytes);
  487. }
  488. /* can we seek? Stevens suggests the seek test was portable */
  489. if(offset!=-1)vf->seekable=1;
  490. /* No seeking yet; Set up a 'single' (current) logical bitstream
  491. entry for partial open */
  492. vf->links=1;
  493. vf->vi=_ogg_calloc(vf->links,sizeof(vorbis_info));
  494. vf->vc=_ogg_calloc(vf->links,sizeof(vorbis_info));
  495. /* Try to fetch the headers, maintaining all the storage */
  496. if((ret=_fetch_headers(vf,vf->vi,vf->vc,&vf->current_serialno,NULL))<0){
  497. vf->datasource=NULL;
  498. ov_clear(vf);
  499. }else
  500. vf->ready_state=PARTOPEN;
  501. return(ret);
  502. }
  503. static int _ov_open2(OggVorbis_File *vf){
  504. vf->ready_state=OPENED;
  505. if(vf->seekable){
  506. int ret=_open_seekable2(vf);
  507. if(ret){
  508. vf->datasource=NULL;
  509. ov_clear(vf);
  510. }
  511. return(ret);
  512. }
  513. return 0;
  514. }
  515. /* clear out the OggVorbis_File struct */
  516. int ov_clear(OggVorbis_File *vf){
  517. if(vf){
  518. vorbis_block_clear(&vf->vb);
  519. vorbis_dsp_clear(&vf->vd);
  520. ogg_stream_clear(&vf->os);
  521. if(vf->vi && vf->links){
  522. int i;
  523. for(i=0;i<vf->links;i++){
  524. vorbis_info_clear(vf->vi+i);
  525. vorbis_comment_clear(vf->vc+i);
  526. }
  527. _ogg_free(vf->vi);
  528. _ogg_free(vf->vc);
  529. }
  530. if(vf->dataoffsets)_ogg_free(vf->dataoffsets);
  531. if(vf->pcmlengths)_ogg_free(vf->pcmlengths);
  532. if(vf->serialnos)_ogg_free(vf->serialnos);
  533. if(vf->offsets)_ogg_free(vf->offsets);
  534. ogg_sync_clear(&vf->oy);
  535. if(vf->datasource)(vf->callbacks.close_func)(vf->datasource);
  536. memset(vf,0,sizeof(OggVorbis_File));
  537. }
  538. #ifdef DEBUG_LEAKS
  539. _VDBG_dump();
  540. #endif
  541. return(0);
  542. }
  543. /* inspects the OggVorbis file and finds/documents all the logical
  544. bitstreams contained in it. Tries to be tolerant of logical
  545. bitstream sections that are truncated/woogie.
  546. return: -1) error
  547. 0) OK
  548. */
  549. int ov_open_callbacks(void *f,OggVorbis_File *vf,char *initial,long ibytes,
  550. ov_callbacks callbacks){
  551. int ret=_ov_open1(f,vf,initial,ibytes,callbacks);
  552. if(ret)return ret;
  553. return _ov_open2(vf);
  554. }
  555. int ov_open(FILE *f,OggVorbis_File *vf,char *initial,long ibytes){
  556. ov_callbacks callbacks = {
  557. (size_t (*)(void *, size_t, size_t, void *)) fread,
  558. (int (*)(void *, ogg_int64_t, int)) _fseek64_wrap,
  559. (int (*)(void *)) fclose,
  560. (long (*)(void *)) ftell
  561. };
  562. return ov_open_callbacks((void *)f, vf, initial, ibytes, callbacks);
  563. }
  564. /* Only partially open the vorbis file; test for Vorbisness, and load
  565. the headers for the first chain. Do not seek (although test for
  566. seekability). Use ov_test_open to finish opening the file, else
  567. ov_clear to close/free it. Same return codes as open. */
  568. int ov_test_callbacks(void *f,OggVorbis_File *vf,char *initial,long ibytes,
  569. ov_callbacks callbacks)
  570. {
  571. return _ov_open1(f,vf,initial,ibytes,callbacks);
  572. }
  573. int ov_test(FILE *f,OggVorbis_File *vf,char *initial,long ibytes){
  574. ov_callbacks callbacks = {
  575. (size_t (*)(void *, size_t, size_t, void *)) fread,
  576. (int (*)(void *, ogg_int64_t, int)) _fseek64_wrap,
  577. (int (*)(void *)) fclose,
  578. (long (*)(void *)) ftell
  579. };
  580. return ov_test_callbacks((void *)f, vf, initial, ibytes, callbacks);
  581. }
  582. int ov_test_open(OggVorbis_File *vf){
  583. if(vf->ready_state!=PARTOPEN)return(OV_EINVAL);
  584. return _ov_open2(vf);
  585. }
  586. /* How many logical bitstreams in this physical bitstream? */
  587. long ov_streams(OggVorbis_File *vf){
  588. return vf->links;
  589. }
  590. /* Is the FILE * associated with vf seekable? */
  591. long ov_seekable(OggVorbis_File *vf){
  592. return vf->seekable;
  593. }
  594. /* returns the bitrate for a given logical bitstream or the entire
  595. physical bitstream. If the file is open for random access, it will
  596. find the *actual* average bitrate. If the file is streaming, it
  597. returns the nominal bitrate (if set) else the average of the
  598. upper/lower bounds (if set) else -1 (unset).
  599. If you want the actual bitrate field settings, get them from the
  600. vorbis_info structs */
  601. long ov_bitrate(OggVorbis_File *vf,int i){
  602. if(vf->ready_state<OPENED)return(OV_EINVAL);
  603. if(i>=vf->links)return(OV_EINVAL);
  604. if(!vf->seekable && i!=0)return(ov_bitrate(vf,0));
  605. if(i<0){
  606. ogg_int64_t bits=0;
  607. int i;
  608. for(i=0;i<vf->links;i++)
  609. bits+=(vf->offsets[i+1]-vf->dataoffsets[i])*8;
  610. return(rint(bits/ov_time_total(vf,-1)));
  611. }else{
  612. if(vf->seekable){
  613. /* return the actual bitrate */
  614. return(rint((vf->offsets[i+1]-vf->dataoffsets[i])*8/ov_time_total(vf,i)));
  615. }else{
  616. /* return nominal if set */
  617. if(vf->vi[i].bitrate_nominal>0){
  618. return vf->vi[i].bitrate_nominal;
  619. }else{
  620. if(vf->vi[i].bitrate_upper>0){
  621. if(vf->vi[i].bitrate_lower>0){
  622. return (vf->vi[i].bitrate_upper+vf->vi[i].bitrate_lower)/2;
  623. }else{
  624. return vf->vi[i].bitrate_upper;
  625. }
  626. }
  627. return(OV_FALSE);
  628. }
  629. }
  630. }
  631. }
  632. /* returns the actual bitrate since last call. returns -1 if no
  633. additional data to offer since last call (or at beginning of stream),
  634. EINVAL if stream is only partially open
  635. */
  636. long ov_bitrate_instant(OggVorbis_File *vf){
  637. int link=(vf->seekable?vf->current_link:0);
  638. long ret;
  639. if(vf->ready_state<OPENED)return(OV_EINVAL);
  640. if(vf->samptrack==0)return(OV_FALSE);
  641. ret=vf->bittrack/vf->samptrack*vf->vi[link].rate+.5;
  642. vf->bittrack=0.f;
  643. vf->samptrack=0.f;
  644. return(ret);
  645. }
  646. /* Guess */
  647. long ov_serialnumber(OggVorbis_File *vf,int i){
  648. if(i>=vf->links)return(ov_serialnumber(vf,vf->links-1));
  649. if(!vf->seekable && i>=0)return(ov_serialnumber(vf,-1));
  650. if(i<0){
  651. return(vf->current_serialno);
  652. }else{
  653. return(vf->serialnos[i]);
  654. }
  655. }
  656. /* returns: total raw (compressed) length of content if i==-1
  657. raw (compressed) length of that logical bitstream for i==0 to n
  658. OV_EINVAL if the stream is not seekable (we can't know the length)
  659. or if stream is only partially open
  660. */
  661. ogg_int64_t ov_raw_total(OggVorbis_File *vf,int i){
  662. if(vf->ready_state<OPENED)return(OV_EINVAL);
  663. if(!vf->seekable || i>=vf->links)return(OV_EINVAL);
  664. if(i<0){
  665. long acc=0;
  666. int i;
  667. for(i=0;i<vf->links;i++)
  668. acc+=ov_raw_total(vf,i);
  669. return(acc);
  670. }else{
  671. return(vf->offsets[i+1]-vf->offsets[i]);
  672. }
  673. }
  674. /* returns: total PCM length (samples) of content if i==-1 PCM length
  675. (samples) of that logical bitstream for i==0 to n
  676. OV_EINVAL if the stream is not seekable (we can't know the
  677. length) or only partially open
  678. */
  679. ogg_int64_t ov_pcm_total(OggVorbis_File *vf,int i){
  680. if(vf->ready_state<OPENED)return(OV_EINVAL);
  681. if(!vf->seekable || i>=vf->links)return(OV_EINVAL);
  682. if(i<0){
  683. ogg_int64_t acc=0;
  684. int i;
  685. for(i=0;i<vf->links;i++)
  686. acc+=ov_pcm_total(vf,i);
  687. return(acc);
  688. }else{
  689. return(vf->pcmlengths[i]);
  690. }
  691. }
  692. /* returns: total seconds of content if i==-1
  693. seconds in that logical bitstream for i==0 to n
  694. OV_EINVAL if the stream is not seekable (we can't know the
  695. length) or only partially open
  696. */
  697. double ov_time_total(OggVorbis_File *vf,int i){
  698. if(vf->ready_state<OPENED)return(OV_EINVAL);
  699. if(!vf->seekable || i>=vf->links)return(OV_EINVAL);
  700. if(i<0){
  701. double acc=0;
  702. int i;
  703. for(i=0;i<vf->links;i++)
  704. acc+=ov_time_total(vf,i);
  705. return(acc);
  706. }else{
  707. return((float)(vf->pcmlengths[i])/vf->vi[i].rate);
  708. }
  709. }
  710. /* seek to an offset relative to the *compressed* data. This also
  711. scans packets to update the PCM cursor. It will cross a logical
  712. bitstream boundary, but only if it can't get any packets out of the
  713. tail of the bitstream we seek to (so no surprises).
  714. returns zero on success, nonzero on failure */
  715. int ov_raw_seek(OggVorbis_File *vf,long pos){
  716. ogg_stream_state work_os;
  717. if(vf->ready_state<OPENED)return(OV_EINVAL);
  718. if(!vf->seekable)
  719. return(OV_ENOSEEK); /* don't dump machine if we can't seek */
  720. if(pos<0 || pos>vf->offsets[vf->links])return(OV_EINVAL);
  721. /* clear out decoding machine state */
  722. vf->pcm_offset=-1;
  723. _decode_clear(vf);
  724. _seek_helper(vf,pos);
  725. /* we need to make sure the pcm_offset is set, but we don't want to
  726. advance the raw cursor past good packets just to get to the first
  727. with a granulepos. That's not equivalent behavior to beginning
  728. decoding as immediately after the seek position as possible.
  729. So, a hack. We use two stream states; a local scratch state and
  730. a the shared vf->os stream state. We use the local state to
  731. scan, and the shared state as a buffer for later decode.
  732. Unfortuantely, on the last page we still advance to last packet
  733. because the granulepos on the last page is not necessarily on a
  734. packet boundary, and we need to make sure the granpos is
  735. correct.
  736. */
  737. {
  738. ogg_page og;
  739. ogg_packet op;
  740. int lastblock=0;
  741. int accblock=0;
  742. int thisblock;
  743. int eosflag;
  744. memset(&work_os,0,sizeof(work_os));/* so that it's safe to clear
  745. it later even if we don't
  746. init it */
  747. while(1){
  748. if(vf->ready_state==STREAMSET){
  749. /* snarf/scan a packet if we can */
  750. int result=ogg_stream_packetout(&work_os,&op);
  751. if(result>0){
  752. if(vf->vi[vf->current_link].codec_setup)
  753. thisblock=vorbis_packet_blocksize(vf->vi+vf->current_link,&op);
  754. if(eosflag)
  755. ogg_stream_packetout(&vf->os,NULL);
  756. else
  757. if(lastblock)accblock+=(lastblock+thisblock)>>2;
  758. if(op.granulepos!=-1){
  759. int i,link=vf->current_link;
  760. ogg_int64_t granulepos=op.granulepos;
  761. for(i=0;i<link;i++)
  762. granulepos+=vf->pcmlengths[i];
  763. vf->pcm_offset=granulepos-accblock;
  764. break;
  765. }
  766. lastblock=thisblock;
  767. continue;
  768. }
  769. }
  770. if(!lastblock){
  771. if(_get_next_page(vf,&og,-1)<0){
  772. vf->pcm_offset=ov_pcm_total(vf,-1);
  773. break;
  774. }
  775. }else{
  776. /* huh? Bogus stream with packets but no granulepos */
  777. vf->pcm_offset=-1;
  778. break;
  779. }
  780. /* has our decoding just traversed a bitstream boundary? */
  781. if(vf->ready_state==STREAMSET)
  782. if(vf->current_serialno!=ogg_page_serialno(&og)){
  783. _decode_clear(vf); /* clear out stream state */
  784. ogg_stream_clear(&work_os);
  785. }
  786. if(vf->ready_state<STREAMSET){
  787. int link;
  788. vf->current_serialno=ogg_page_serialno(&og);
  789. for(link=0;link<vf->links;link++)
  790. if(vf->serialnos[link]==vf->current_serialno)break;
  791. if(link==vf->links)goto seek_error; /* sign of a bogus stream.
  792. error out, leave
  793. machine uninitialized */
  794. vf->current_link=link;
  795. ogg_stream_init(&vf->os,vf->current_serialno);
  796. ogg_stream_reset(&vf->os);
  797. ogg_stream_init(&work_os,vf->current_serialno);
  798. ogg_stream_reset(&work_os);
  799. vf->ready_state=STREAMSET;
  800. }
  801. ogg_stream_pagein(&vf->os,&og);
  802. ogg_stream_pagein(&work_os,&og);
  803. eosflag=ogg_page_eos(&og);
  804. }
  805. }
  806. ogg_stream_clear(&work_os);
  807. return(0);
  808. seek_error:
  809. /* dump the machine so we're in a known state */
  810. vf->pcm_offset=-1;
  811. ogg_stream_clear(&work_os);
  812. _decode_clear(vf);
  813. return OV_EBADLINK;
  814. }
  815. /* Page granularity seek (faster than sample granularity because we
  816. don't do the last bit of decode to find a specific sample).
  817. Seek to the last [granule marked] page preceeding the specified pos
  818. location, such that decoding past the returned point will quickly
  819. arrive at the requested position. */
  820. int ov_pcm_seek_page(OggVorbis_File *vf,ogg_int64_t pos){
  821. int link=-1;
  822. long ret;
  823. ogg_int64_t total=ov_pcm_total(vf,-1);
  824. if(vf->ready_state<OPENED)return(OV_EINVAL);
  825. if(!vf->seekable)return(OV_ENOSEEK);
  826. if(pos<0 || pos>total)return(OV_EINVAL);
  827. /* which bitstream section does this pcm offset occur in? */
  828. for(link=vf->links-1;link>=0;link--){
  829. total-=vf->pcmlengths[link];
  830. if(pos>=total)break;
  831. }
  832. /* search within the logical bitstream for the page with the highest
  833. pcm_pos preceeding (or equal to) pos. There is a danger here;
  834. missing pages or incorrect frame number information in the
  835. bitstream could make our task impossible. Account for that (it
  836. would be an error condition) */
  837. /* new search algorithm by HB (Nicholas Vinen) */
  838. {
  839. ogg_int64_t target=pos-total;
  840. long end=vf->offsets[link+1];
  841. long begin=vf->offsets[link];
  842. ogg_int64_t endtime = vf->pcmlengths[link];
  843. ogg_int64_t begintime = 0;
  844. long best=begin;
  845. ogg_page og;
  846. while(begin<end){
  847. long bisect;
  848. if(end-begin<CHUNKSIZE){
  849. bisect=begin;
  850. }else{
  851. /* take a (pretty decent) guess. */
  852. bisect=begin +
  853. (target-begintime)*(end-begin)/(endtime-begintime) - CHUNKSIZE;
  854. if(bisect<=begin)
  855. bisect=begin+1;
  856. }
  857. _seek_helper(vf,bisect);
  858. while(begin<end){
  859. ret=_get_next_page(vf,&og,end-bisect);
  860. if(ret==OV_EREAD) goto seek_error;
  861. if(ret<0){
  862. if(bisect<=begin+1)
  863. end=begin; /* found it */
  864. else{
  865. if(bisect==0)goto seek_error;
  866. bisect-=CHUNKSIZE;
  867. if(bisect<=begin)bisect=begin+1;
  868. _seek_helper(vf,bisect);
  869. }
  870. }else{
  871. ogg_int64_t granulepos=ogg_page_granulepos(&og);
  872. if(granulepos<target){
  873. best=ret; /* raw offset of packet with granulepos */
  874. begin=vf->offset; /* raw offset of next page */
  875. begintime=granulepos;
  876. if(target-begin>44100)break;
  877. bisect=begin; /* *not* begin + 1 */
  878. }else{
  879. if(bisect<=begin+1)
  880. end=begin; /* found it */
  881. else{
  882. if(end==vf->offset){ /* we're pretty close - we'd be stuck in */
  883. end=ret;
  884. bisect-=CHUNKSIZE; /* an endless loop otherwise. */
  885. if(bisect<=begin)bisect=begin+1;
  886. _seek_helper(vf,bisect);
  887. }else{
  888. end=ret;
  889. endtime=granulepos;
  890. break;
  891. }
  892. }
  893. }
  894. }
  895. }
  896. }
  897. /* found our page. seek to it, update pcm offset. Easier case than
  898. raw_seek, don't keep packets preceeding granulepos. */
  899. {
  900. ogg_page og;
  901. ogg_packet op;
  902. /* clear out decoding machine state */
  903. _decode_clear(vf);
  904. /* seek */
  905. _seek_helper(vf,best);
  906. if(_get_next_page(vf,&og,-1)<0)return(OV_EOF); /* shouldn't happen */
  907. vf->current_serialno=ogg_page_serialno(&og);
  908. vf->current_link=link;
  909. ogg_stream_init(&vf->os,vf->current_serialno);
  910. ogg_stream_reset(&vf->os);
  911. vf->ready_state=STREAMSET;
  912. ogg_stream_pagein(&vf->os,&og);
  913. /* pull out all but last packet; the one with granulepos */
  914. while(1){
  915. ret=ogg_stream_packetpeek(&vf->os,&op);
  916. if(ret==0){
  917. /* !!! the packet finishing this page originated on a
  918. preceeding page. Keep fetching previous pages until we
  919. get one with a granulepos or without the 'continued' flag
  920. set. Then just use raw_seek for simplicity. */
  921. while(1){
  922. ret=_get_prev_page(vf,&og);
  923. if(ret<0)goto seek_error;
  924. if(ogg_page_granulepos(&og)>-1 ||
  925. !ogg_page_continued(&og)){
  926. return ov_raw_seek(vf,ret);
  927. }
  928. vf->offset=ret;
  929. }
  930. }
  931. if(ret<0)goto seek_error;
  932. if(op.granulepos!=-1){
  933. vf->pcm_offset=op.granulepos+total;
  934. break;
  935. }else
  936. ret=ogg_stream_packetout(&vf->os,NULL);
  937. }
  938. }
  939. }
  940. /* verify result */
  941. if(vf->pcm_offset>pos || pos>ov_pcm_total(vf,-1)){
  942. ret=OV_EFAULT;
  943. goto seek_error;
  944. }
  945. return(0);
  946. seek_error:
  947. /* dump machine so we're in a known state */
  948. vf->pcm_offset=-1;
  949. _decode_clear(vf);
  950. return ret;
  951. }
  952. /* seek to a sample offset relative to the decompressed pcm stream
  953. returns zero on success, nonzero on failure */
  954. int ov_pcm_seek(OggVorbis_File *vf,ogg_int64_t pos){
  955. int thisblock,lastblock=0;
  956. int ret=ov_pcm_seek_page(vf,pos);
  957. codec_setup_info *ci=vf->vi->codec_setup;
  958. if(ret<0)return(ret);
  959. /* discard leading packets we don't need for the lapping of the
  960. position we want; don't decode them */
  961. while(1){
  962. ogg_packet op;
  963. ogg_page og;
  964. int ret=ogg_stream_packetpeek(&vf->os,&op);
  965. if(ret>0){
  966. thisblock=vorbis_packet_blocksize(vf->vi+vf->current_link,&op);
  967. if(lastblock)vf->pcm_offset+=(lastblock+thisblock)>>2;
  968. if(vf->pcm_offset+((thisblock+ci->blocksizes[1])>>2)>=pos)break;
  969. ogg_stream_packetout(&vf->os,NULL);
  970. /* end of logical stream case is hard, especially with exact
  971. length positioning. */
  972. if(op.granulepos>-1){
  973. int i;
  974. /* always believe the stream markers */
  975. vf->pcm_offset=op.granulepos;
  976. for(i=0;i<vf->current_link;i++)
  977. vf->pcm_offset+=vf->pcmlengths[i];
  978. }
  979. lastblock=thisblock;
  980. }else{
  981. if(ret<0 && ret!=OV_HOLE)break;
  982. /* suck in a new page */
  983. if(_get_next_page(vf,&og,-1)<0)break;
  984. if(vf->current_serialno!=ogg_page_serialno(&og))_decode_clear(vf);
  985. if(vf->ready_state<STREAMSET){
  986. int link;
  987. vf->current_serialno=ogg_page_serialno(&og);
  988. for(link=0;link<vf->links;link++)
  989. if(vf->serialnos[link]==vf->current_serialno)break;
  990. if(link==vf->links)return(OV_EBADLINK);
  991. vf->current_link=link;
  992. ogg_stream_init(&vf->os,vf->current_serialno);
  993. ogg_stream_reset(&vf->os);
  994. vf->ready_state=STREAMSET;
  995. lastblock=0;
  996. }
  997. ogg_stream_pagein(&vf->os,&og);
  998. }
  999. }
  1000. /* discard samples until we reach the desired position. Crossing a
  1001. logical bitstream boundary with abandon is OK. */
  1002. _make_decode_ready(vf);
  1003. while(vf->pcm_offset<pos){
  1004. float **pcm;
  1005. long target=pos-vf->pcm_offset;
  1006. long samples=vorbis_synthesis_pcmout(&vf->vd,&pcm);
  1007. if(samples>target)samples=target;
  1008. vorbis_synthesis_read(&vf->vd,samples);
  1009. vf->pcm_offset+=samples;
  1010. if(samples<target)
  1011. if(_process_packet(vf,1)<=0)
  1012. vf->pcm_offset=ov_pcm_total(vf,-1); /* eof */
  1013. }
  1014. return 0;
  1015. }
  1016. /* seek to a playback time relative to the decompressed pcm stream
  1017. returns zero on success, nonzero on failure */
  1018. int ov_time_seek(OggVorbis_File *vf,double seconds){
  1019. /* translate time to PCM position and call ov_pcm_seek */
  1020. int link=-1;
  1021. ogg_int64_t pcm_total=ov_pcm_total(vf,-1);
  1022. double time_total=ov_time_total(vf,-1);
  1023. if(vf->ready_state<OPENED)return(OV_EINVAL);
  1024. if(!vf->seekable)return(OV_ENOSEEK);
  1025. if(seconds<0 || seconds>time_total)return(OV_EINVAL);
  1026. /* which bitstream section does this time offset occur in? */
  1027. for(link=vf->links-1;link>=0;link--){
  1028. pcm_total-=vf->pcmlengths[link];
  1029. time_total-=ov_time_total(vf,link);
  1030. if(seconds>=time_total)break;
  1031. }
  1032. /* enough information to convert time offset to pcm offset */
  1033. {
  1034. ogg_int64_t target=pcm_total+(seconds-time_total)*vf->vi[link].rate;
  1035. return(ov_pcm_seek(vf,target));
  1036. }
  1037. }
  1038. /* page-granularity version of ov_time_seek
  1039. returns zero on success, nonzero on failure */
  1040. int ov_time_seek_page(OggVorbis_File *vf,double seconds){
  1041. /* translate time to PCM position and call ov_pcm_seek */
  1042. int link=-1;
  1043. ogg_int64_t pcm_total=ov_pcm_total(vf,-1);
  1044. double time_total=ov_time_total(vf,-1);
  1045. if(vf->ready_state<OPENED)return(OV_EINVAL);
  1046. if(!vf->seekable)return(OV_ENOSEEK);
  1047. if(seconds<0 || seconds>time_total)return(OV_EINVAL);
  1048. /* which bitstream section does this time offset occur in? */
  1049. for(link=vf->links-1;link>=0;link--){
  1050. pcm_total-=vf->pcmlengths[link];
  1051. time_total-=ov_time_total(vf,link);
  1052. if(seconds>=time_total)break;
  1053. }
  1054. /* enough information to convert time offset to pcm offset */
  1055. {
  1056. ogg_int64_t target=pcm_total+(seconds-time_total)*vf->vi[link].rate;
  1057. return(ov_pcm_seek_page(vf,target));
  1058. }
  1059. }
  1060. /* tell the current stream offset cursor. Note that seek followed by
  1061. tell will likely not give the set offset due to caching */
  1062. ogg_int64_t ov_raw_tell(OggVorbis_File *vf){
  1063. if(vf->ready_state<OPENED)return(OV_EINVAL);
  1064. return(vf->offset);
  1065. }
  1066. /* return PCM offset (sample) of next PCM sample to be read */
  1067. ogg_int64_t ov_pcm_tell(OggVorbis_File *vf){
  1068. if(vf->ready_state<OPENED)return(OV_EINVAL);
  1069. return(vf->pcm_offset);
  1070. }
  1071. /* return time offset (seconds) of next PCM sample to be read */
  1072. double ov_time_tell(OggVorbis_File *vf){
  1073. /* translate time to PCM position and call ov_pcm_seek */
  1074. int link=-1;
  1075. ogg_int64_t pcm_total=0;
  1076. double time_total=0.f;
  1077. if(vf->ready_state<OPENED)return(OV_EINVAL);
  1078. if(vf->seekable){
  1079. pcm_total=ov_pcm_total(vf,-1);
  1080. time_total=ov_time_total(vf,-1);
  1081. /* which bitstream section does this time offset occur in? */
  1082. for(link=vf->links-1;link>=0;link--){
  1083. pcm_total-=vf->pcmlengths[link];
  1084. time_total-=ov_time_total(vf,link);
  1085. if(vf->pcm_offset>=pcm_total)break;
  1086. }
  1087. }
  1088. return((double)time_total+(double)(vf->pcm_offset-pcm_total)/vf->vi[link].rate);
  1089. }
  1090. /* link: -1) return the vorbis_info struct for the bitstream section
  1091. currently being decoded
  1092. 0-n) to request information for a specific bitstream section
  1093. In the case of a non-seekable bitstream, any call returns the
  1094. current bitstream. NULL in the case that the machine is not
  1095. initialized */
  1096. vorbis_info *ov_info(OggVorbis_File *vf,int link){
  1097. if(vf->seekable){
  1098. if(link<0)
  1099. if(vf->ready_state>=STREAMSET)
  1100. return vf->vi+vf->current_link;
  1101. else
  1102. return vf->vi;
  1103. else
  1104. if(link>=vf->links)
  1105. return NULL;
  1106. else
  1107. return vf->vi+link;
  1108. }else{
  1109. return vf->vi;
  1110. }
  1111. }
  1112. /* grr, strong typing, grr, no templates/inheritence, grr */
  1113. vorbis_comment *ov_comment(OggVorbis_File *vf,int link){
  1114. if(vf->seekable){
  1115. if(link<0)
  1116. if(vf->ready_state>=STREAMSET)
  1117. return vf->vc+vf->current_link;
  1118. else
  1119. return vf->vc;
  1120. else
  1121. if(link>=vf->links)
  1122. return NULL;
  1123. else
  1124. return vf->vc+link;
  1125. }else{
  1126. return vf->vc;
  1127. }
  1128. }
  1129. int host_is_big_endian() {
  1130. ogg_int32_t pattern = 0xfeedface; /* deadbeef */
  1131. unsigned char *bytewise = (unsigned char *)&pattern;
  1132. if (bytewise[0] == 0xfe) return 1;
  1133. return 0;
  1134. }
  1135. /* up to this point, everything could more or less hide the multiple
  1136. logical bitstream nature of chaining from the toplevel application
  1137. if the toplevel application didn't particularly care. However, at
  1138. the point that we actually read audio back, the multiple-section
  1139. nature must surface: Multiple bitstream sections do not necessarily
  1140. have to have the same number of channels or sampling rate.
  1141. ov_read returns the sequential logical bitstream number currently
  1142. being decoded along with the PCM data in order that the toplevel
  1143. application can take action on channel/sample rate changes. This
  1144. number will be incremented even for streamed (non-seekable) streams
  1145. (for seekable streams, it represents the actual logical bitstream
  1146. index within the physical bitstream. Note that the accessor
  1147. functions above are aware of this dichotomy).
  1148. input values: buffer) a buffer to hold packed PCM data for return
  1149. length) the byte length requested to be placed into buffer
  1150. bigendianp) should the data be packed LSB first (0) or
  1151. MSB first (1)
  1152. word) word size for output. currently 1 (byte) or
  1153. 2 (16 bit short)
  1154. return values: <0) error/hole in data (OV_HOLE), partial open (OV_EINVAL)
  1155. 0) EOF
  1156. n) number of bytes of PCM actually returned. The
  1157. below works on a packet-by-packet basis, so the
  1158. return length is not related to the 'length' passed
  1159. in, just guaranteed to fit.
  1160. *section) set to the logical bitstream number */
  1161. long ov_read(OggVorbis_File *vf,char *buffer,int length,
  1162. int bigendianp,int word,int sgned,int *bitstream){
  1163. int i,j;
  1164. int host_endian = host_is_big_endian();
  1165. if(vf->ready_state<OPENED)return(OV_EINVAL);
  1166. if(vf->ready_state==OPENED)return(OV_EOF); /* stream is always
  1167. initialized after
  1168. other calls (after
  1169. open)... unless there
  1170. was no page at the end
  1171. to initialize state
  1172. with. */
  1173. while(1){
  1174. if(vf->ready_state>=STREAMSET){
  1175. float **pcm;
  1176. long samples=vorbis_synthesis_pcmout(&vf->vd,&pcm);
  1177. if(samples){
  1178. /* yay! proceed to pack data into the byte buffer */
  1179. long channels=ov_info(vf,-1)->channels;
  1180. long bytespersample=word * channels;
  1181. vorbis_fpu_control fpu;
  1182. if(samples>length/bytespersample)samples=length/bytespersample;
  1183. /* a tight loop to pack each size */
  1184. {
  1185. int val;
  1186. if(word==1){
  1187. int off=(sgned?0:128);
  1188. vorbis_fpu_setround(&fpu);
  1189. for(j=0;j<samples;j++)
  1190. for(i=0;i<channels;i++){
  1191. val=vorbis_ftoi(pcm[i][j]*128.f);
  1192. if(val>127)val=127;
  1193. else if(val<-128)val=-128;
  1194. *buffer++=val+off;
  1195. }
  1196. vorbis_fpu_restore(fpu);
  1197. }else{
  1198. int off=(sgned?0:32768);
  1199. if(host_endian==bigendianp){
  1200. if(sgned){
  1201. vorbis_fpu_setround(&fpu);
  1202. for(i=0;i<channels;i++) { /* It's faster in this order */
  1203. float *src=pcm[i];
  1204. short *dest=((short *)buffer)+i;
  1205. for(j=0;j<samples;j++) {
  1206. val=vorbis_ftoi(src[j]*32768.f);
  1207. if(val>32767)val=32767;
  1208. else if(val<-32768)val=-32768;
  1209. *dest=val;
  1210. dest+=channels;
  1211. }
  1212. }
  1213. vorbis_fpu_restore(fpu);
  1214. }else{
  1215. vorbis_fpu_setround(&fpu);
  1216. for(i=0;i<channels;i++) {
  1217. float *src=pcm[i];
  1218. short *dest=((short *)buffer)+i;
  1219. for(j=0;j<samples;j++) {
  1220. val=vorbis_ftoi(src[j]*32768.f);
  1221. if(val>32767)val=32767;
  1222. else if(val<-32768)val=-32768;
  1223. *dest=val+off;
  1224. dest+=channels;
  1225. }
  1226. }
  1227. vorbis_fpu_restore(fpu);
  1228. }
  1229. }else if(bigendianp){
  1230. vorbis_fpu_setround(&fpu);
  1231. for(j=0;j<samples;j++)
  1232. for(i=0;i<channels;i++){
  1233. val=vorbis_ftoi(pcm[i][j]*32768.f);
  1234. if(val>32767)val=32767;
  1235. else if(val<-32768)val=-32768;
  1236. val+=off;
  1237. *buffer++=(val>>8);
  1238. *buffer++=(val&0xff);
  1239. }
  1240. vorbis_fpu_restore(fpu);
  1241. }else{
  1242. int val;
  1243. vorbis_fpu_setround(&fpu);
  1244. for(j=0;j<samples;j++)
  1245. for(i=0;i<channels;i++){
  1246. val=vorbis_ftoi(pcm[i][j]*32768.f);
  1247. if(val>32767)val=32767;
  1248. else if(val<-32768)val=-32768;
  1249. val+=off;
  1250. *buffer++=(val&0xff);
  1251. *buffer++=(val>>8);
  1252. }
  1253. vorbis_fpu_restore(fpu);
  1254. }
  1255. }
  1256. }
  1257. vorbis_synthesis_read(&vf->vd,samples);
  1258. vf->pcm_offset+=samples;
  1259. if(bitstream)*bitstream=vf->current_link;
  1260. return(samples*bytespersample);
  1261. }
  1262. }
  1263. /* suck in another packet */
  1264. switch(_process_packet(vf,1)){
  1265. case 0:case OV_EOF:
  1266. return(0);
  1267. case OV_HOLE:
  1268. return(OV_HOLE);
  1269. case OV_EBADLINK:
  1270. return(OV_EBADLINK);
  1271. }
  1272. }
  1273. }