vorbisfile.c 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295
  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-2007 *
  9. * by the Xiph.Org Foundation http://www.xiph.org/ *
  10. * *
  11. ********************************************************************
  12. function: stdio-based convenience library for opening/seeking/decoding
  13. last mod: $Id$
  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 "misc.h"
  24. /* A 'chained bitstream' is a Vorbis bitstream that contains more than
  25. one logical bitstream arranged end to end (the only form of Ogg
  26. multiplexing allowed in a Vorbis bitstream; grouping [parallel
  27. multiplexing] is not allowed in Vorbis) */
  28. /* A Vorbis file can be played beginning to end (streamed) without
  29. worrying ahead of time about chaining (see decoder_example.c). If
  30. we have the whole file, however, and want random access
  31. (seeking/scrubbing) or desire to know the total length/time of a
  32. file, we need to account for the possibility of chaining. */
  33. /* We can handle things a number of ways; we can determine the entire
  34. bitstream structure right off the bat, or find pieces on demand.
  35. This example determines and caches structure for the entire
  36. bitstream, but builds a virtual decoder on the fly when moving
  37. between links in the chain. */
  38. /* There are also different ways to implement seeking. Enough
  39. information exists in an Ogg bitstream to seek to
  40. sample-granularity positions in the output. Or, one can seek by
  41. picking some portion of the stream roughly in the desired area if
  42. we only want coarse navigation through the stream. */
  43. /*************************************************************************
  44. * Many, many internal helpers. The intention is not to be confusing;
  45. * rampant duplication and monolithic function implementation would be
  46. * harder to understand anyway. The high level functions are last. Begin
  47. * grokking near the end of the file */
  48. /* read a little more data from the file/pipe into the ogg_sync framer
  49. */
  50. #define CHUNKSIZE 65536
  51. static long _get_data(OggVorbis_File *vf){
  52. errno=0;
  53. if(!(vf->callbacks.read_func))return(-1);
  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 int _seek_helper(OggVorbis_File *vf,ogg_int64_t offset){
  65. if(vf->datasource){
  66. if(!(vf->callbacks.seek_func)||
  67. (vf->callbacks.seek_func)(vf->datasource, offset, SEEK_SET) == -1)
  68. return OV_EREAD;
  69. vf->offset=offset;
  70. ogg_sync_reset(&vf->oy);
  71. }else{
  72. /* shouldn't happen unless someone writes a broken callback */
  73. return OV_EFAULT;
  74. }
  75. return 0;
  76. }
  77. /* The read/seek functions track absolute position within the stream */
  78. /* from the head of the stream, get the next page. boundary specifies
  79. if the function is allowed to fetch more data from the stream (and
  80. how much) or only use internally buffered data.
  81. boundary: -1) unbounded search
  82. 0) read no additional data; use cached only
  83. n) search for a new page beginning for n bytes
  84. return: <0) did not find a page (OV_FALSE, OV_EOF, OV_EREAD)
  85. n) found a page at absolute offset n */
  86. static ogg_int64_t _get_next_page(OggVorbis_File *vf,ogg_page *og,
  87. ogg_int64_t boundary){
  88. if(boundary>0)boundary+=vf->offset;
  89. while(1){
  90. long more;
  91. if(boundary>0 && vf->offset>=boundary)return(OV_FALSE);
  92. more=ogg_sync_pageseek(&vf->oy,og);
  93. if(more<0){
  94. /* skipped n bytes */
  95. vf->offset-=more;
  96. }else{
  97. if(more==0){
  98. /* send more paramedics */
  99. if(!boundary)return(OV_FALSE);
  100. {
  101. long ret=_get_data(vf);
  102. if(ret==0)return(OV_EOF);
  103. if(ret<0)return(OV_EREAD);
  104. }
  105. }else{
  106. /* got a page. Return the offset at the page beginning,
  107. advance the internal offset past the page end */
  108. ogg_int64_t ret=vf->offset;
  109. vf->offset+=more;
  110. return(ret);
  111. }
  112. }
  113. }
  114. }
  115. /* find the latest page beginning before the current stream cursor
  116. position. Much dirtier than the above as Ogg doesn't have any
  117. backward search linkage. no 'readp' as it will certainly have to
  118. read. */
  119. /* returns offset or OV_EREAD, OV_FAULT */
  120. static ogg_int64_t _get_prev_page(OggVorbis_File *vf,ogg_page *og){
  121. ogg_int64_t begin=vf->offset;
  122. ogg_int64_t end=begin;
  123. ogg_int64_t ret;
  124. ogg_int64_t offset=-1;
  125. while(offset==-1){
  126. begin-=CHUNKSIZE;
  127. if(begin<0)
  128. begin=0;
  129. ret=_seek_helper(vf,begin);
  130. if(ret)return(ret);
  131. while(vf->offset<end){
  132. memset(og,0,sizeof(*og));
  133. ret=_get_next_page(vf,og,end-vf->offset);
  134. if(ret==OV_EREAD)return(OV_EREAD);
  135. if(ret<0){
  136. break;
  137. }else{
  138. offset=ret;
  139. }
  140. }
  141. }
  142. /* In a fully compliant, non-multiplexed stream, we'll still be
  143. holding the last page. In multiplexed (or noncompliant streams),
  144. we will probably have to re-read the last page we saw */
  145. if(og->header_len==0){
  146. ret=_seek_helper(vf,offset);
  147. if(ret)return(ret);
  148. ret=_get_next_page(vf,og,CHUNKSIZE);
  149. if(ret<0)
  150. /* this shouldn't be possible */
  151. return(OV_EFAULT);
  152. }
  153. return(offset);
  154. }
  155. static void _add_serialno(ogg_page *og,long **serialno_list, int *n){
  156. long s = ogg_page_serialno(og);
  157. (*n)++;
  158. if(*serialno_list){
  159. *serialno_list = _ogg_realloc(*serialno_list, sizeof(**serialno_list)*(*n));
  160. }else{
  161. *serialno_list = _ogg_malloc(sizeof(**serialno_list));
  162. }
  163. (*serialno_list)[(*n)-1] = s;
  164. }
  165. /* returns nonzero if found */
  166. static int _lookup_serialno(long s, long *serialno_list, int n){
  167. if(serialno_list){
  168. while(n--){
  169. if(*serialno_list == s) return 1;
  170. serialno_list++;
  171. }
  172. }
  173. return 0;
  174. }
  175. static int _lookup_page_serialno(ogg_page *og, long *serialno_list, int n){
  176. long s = ogg_page_serialno(og);
  177. return _lookup_serialno(s,serialno_list,n);
  178. }
  179. /* performs the same search as _get_prev_page, but prefers pages of
  180. the specified serial number. If a page of the specified serialno is
  181. spotted during the seek-back-and-read-forward, it will return the
  182. info of last page of the matching serial number instead of the very
  183. last page. If no page of the specified serialno is seen, it will
  184. return the info of last page and alter *serialno. */
  185. static ogg_int64_t _get_prev_page_serial(OggVorbis_File *vf,
  186. long *serial_list, int serial_n,
  187. int *serialno, ogg_int64_t *granpos){
  188. ogg_page og;
  189. ogg_int64_t begin=vf->offset;
  190. ogg_int64_t end=begin;
  191. ogg_int64_t ret;
  192. ogg_int64_t prefoffset=-1;
  193. ogg_int64_t offset=-1;
  194. ogg_int64_t ret_serialno;
  195. ogg_int64_t ret_gran;
  196. while(offset==-1){
  197. begin-=CHUNKSIZE;
  198. if(begin<0)
  199. begin=0;
  200. ret=_seek_helper(vf,begin);
  201. if(ret)return(ret);
  202. while(vf->offset<end){
  203. ret=_get_next_page(vf,&og,end-vf->offset);
  204. if(ret==OV_EREAD)return(OV_EREAD);
  205. if(ret<0){
  206. break;
  207. }else{
  208. ret_serialno=ogg_page_serialno(&og);
  209. ret_gran=ogg_page_granulepos(&og);
  210. offset=ret;
  211. if(ret_serialno == *serialno){
  212. prefoffset=ret;
  213. *granpos=ret_gran;
  214. }
  215. if(!_lookup_serialno(ret_serialno,serial_list,serial_n)){
  216. /* we fell off the end of the link, which means we seeked
  217. back too far and shouldn't have been looking in that link
  218. to begin with. If we found the preferred serial number,
  219. forget that we saw it. */
  220. prefoffset=-1;
  221. }
  222. }
  223. }
  224. }
  225. /* we're not interested in the page... just the serialno and granpos. */
  226. if(prefoffset>=0)return(prefoffset);
  227. *serialno = ret_serialno;
  228. *granpos = ret_gran;
  229. return(offset);
  230. }
  231. /* uses the local ogg_stream storage in vf; this is important for
  232. non-streaming input sources */
  233. static int _fetch_headers(OggVorbis_File *vf,vorbis_info *vi,vorbis_comment *vc,
  234. long **serialno_list, int *serialno_n,
  235. ogg_page *og_ptr){
  236. ogg_page og;
  237. ogg_packet op;
  238. int i,ret;
  239. int allbos=0;
  240. if(!og_ptr){
  241. ogg_int64_t llret=_get_next_page(vf,&og,CHUNKSIZE);
  242. if(llret==OV_EREAD)return(OV_EREAD);
  243. if(llret<0)return(OV_ENOTVORBIS);
  244. og_ptr=&og;
  245. }
  246. vorbis_info_init(vi);
  247. vorbis_comment_init(vc);
  248. vf->ready_state=OPENED;
  249. /* extract the serialnos of all BOS pages + the first set of vorbis
  250. headers we see in the link */
  251. while(ogg_page_bos(og_ptr)){
  252. if(serialno_list){
  253. if(_lookup_page_serialno(og_ptr,*serialno_list,*serialno_n)){
  254. /* a dupe serialnumber in an initial header packet set == invalid stream */
  255. if(*serialno_list)_ogg_free(*serialno_list);
  256. *serialno_list=0;
  257. *serialno_n=0;
  258. ret=OV_EBADHEADER;
  259. goto bail_header;
  260. }
  261. _add_serialno(og_ptr,serialno_list,serialno_n);
  262. }
  263. if(vf->ready_state<STREAMSET){
  264. /* we don't have a vorbis stream in this link yet, so begin
  265. prospective stream setup. We need a stream to get packets */
  266. ogg_stream_reset_serialno(&vf->os,ogg_page_serialno(og_ptr));
  267. ogg_stream_pagein(&vf->os,og_ptr);
  268. if(ogg_stream_packetout(&vf->os,&op) > 0 &&
  269. vorbis_synthesis_idheader(&op)){
  270. /* vorbis header; continue setup */
  271. vf->ready_state=STREAMSET;
  272. if((ret=vorbis_synthesis_headerin(vi,vc,&op))){
  273. ret=OV_EBADHEADER;
  274. goto bail_header;
  275. }
  276. }
  277. }
  278. /* get next page */
  279. {
  280. ogg_int64_t llret=_get_next_page(vf,og_ptr,CHUNKSIZE);
  281. if(llret==OV_EREAD){
  282. ret=OV_EREAD;
  283. goto bail_header;
  284. }
  285. if(llret<0){
  286. ret=OV_ENOTVORBIS;
  287. goto bail_header;
  288. }
  289. /* if this page also belongs to our vorbis stream, submit it and break */
  290. if(vf->ready_state==STREAMSET &&
  291. vf->os.serialno == ogg_page_serialno(og_ptr)){
  292. ogg_stream_pagein(&vf->os,og_ptr);
  293. break;
  294. }
  295. }
  296. }
  297. if(vf->ready_state!=STREAMSET){
  298. ret = OV_ENOTVORBIS;
  299. goto bail_header;
  300. }
  301. while(1){
  302. i=0;
  303. while(i<2){ /* get a page loop */
  304. while(i<2){ /* get a packet loop */
  305. int result=ogg_stream_packetout(&vf->os,&op);
  306. if(result==0)break;
  307. if(result==-1){
  308. ret=OV_EBADHEADER;
  309. goto bail_header;
  310. }
  311. if((ret=vorbis_synthesis_headerin(vi,vc,&op)))
  312. goto bail_header;
  313. i++;
  314. }
  315. while(i<2){
  316. if(_get_next_page(vf,og_ptr,CHUNKSIZE)<0){
  317. ret=OV_EBADHEADER;
  318. goto bail_header;
  319. }
  320. /* if this page belongs to the correct stream, go parse it */
  321. if(vf->os.serialno == ogg_page_serialno(og_ptr)){
  322. ogg_stream_pagein(&vf->os,og_ptr);
  323. break;
  324. }
  325. /* if we never see the final vorbis headers before the link
  326. ends, abort */
  327. if(ogg_page_bos(og_ptr)){
  328. if(allbos){
  329. ret = OV_EBADHEADER;
  330. goto bail_header;
  331. }else
  332. allbos=1;
  333. }
  334. /* otherwise, keep looking */
  335. }
  336. }
  337. return 0;
  338. }
  339. bail_header:
  340. vorbis_info_clear(vi);
  341. vorbis_comment_clear(vc);
  342. vf->ready_state=OPENED;
  343. return ret;
  344. }
  345. /* Starting from current cursor position, get initial PCM offset of
  346. next page. Consumes the page in the process without decoding
  347. audio, however this is only called during stream parsing upon
  348. seekable open. */
  349. static ogg_int64_t _initial_pcmoffset(OggVorbis_File *vf, vorbis_info *vi){
  350. ogg_page og;
  351. ogg_int64_t accumulated=0;
  352. long lastblock=-1;
  353. int result;
  354. int serialno = vf->os.serialno;
  355. while(1){
  356. ogg_packet op;
  357. if(_get_next_page(vf,&og,-1)<0)
  358. break; /* should not be possible unless the file is truncated/mangled */
  359. if(ogg_page_bos(&og)) break;
  360. if(ogg_page_serialno(&og)!=serialno) continue;
  361. /* count blocksizes of all frames in the page */
  362. ogg_stream_pagein(&vf->os,&og);
  363. while((result=ogg_stream_packetout(&vf->os,&op))){
  364. if(result>0){ /* ignore holes */
  365. long thisblock=vorbis_packet_blocksize(vi,&op);
  366. if(lastblock!=-1)
  367. accumulated+=(lastblock+thisblock)>>2;
  368. lastblock=thisblock;
  369. }
  370. }
  371. if(ogg_page_granulepos(&og)!=-1){
  372. /* pcm offset of last packet on the first audio page */
  373. accumulated= ogg_page_granulepos(&og)-accumulated;
  374. break;
  375. }
  376. }
  377. /* less than zero? This is a stream with samples trimmed off
  378. the beginning, a normal occurrence; set the offset to zero */
  379. if(accumulated<0)accumulated=0;
  380. return accumulated;
  381. }
  382. /* finds each bitstream link one at a time using a bisection search
  383. (has to begin by knowing the offset of the lb's initial page).
  384. Recurses for each link so it can alloc the link storage after
  385. finding them all, then unroll and fill the cache at the same time */
  386. static int _bisect_forward_serialno(OggVorbis_File *vf,
  387. ogg_int64_t begin,
  388. ogg_int64_t searched,
  389. ogg_int64_t end,
  390. ogg_int64_t endgran,
  391. int endserial,
  392. long *currentno_list,
  393. int currentnos,
  394. long m){
  395. ogg_int64_t pcmoffset;
  396. ogg_int64_t dataoffset=searched;
  397. ogg_int64_t endsearched=end;
  398. ogg_int64_t next=end;
  399. ogg_int64_t searchgran=-1;
  400. ogg_page og;
  401. ogg_int64_t ret,last;
  402. int serialno = vf->os.serialno;
  403. /* invariants:
  404. we have the headers and serialnos for the link beginning at 'begin'
  405. we have the offset and granpos of the last page in the file (potentially
  406. not a page we care about)
  407. */
  408. /* Is the last page in our list of current serialnumbers? */
  409. if(_lookup_serialno(endserial,currentno_list,currentnos)){
  410. /* last page is in the starting serialno list, so we've bisected
  411. down to (or just started with) a single link. Now we need to
  412. find the last vorbis page belonging to the first vorbis stream
  413. for this link. */
  414. while(endserial != serialno){
  415. endserial = serialno;
  416. vf->offset=_get_prev_page_serial(vf,currentno_list,currentnos,&endserial,&endgran);
  417. }
  418. vf->links=m+1;
  419. if(vf->offsets)_ogg_free(vf->offsets);
  420. if(vf->serialnos)_ogg_free(vf->serialnos);
  421. if(vf->dataoffsets)_ogg_free(vf->dataoffsets);
  422. vf->offsets=_ogg_malloc((vf->links+1)*sizeof(*vf->offsets));
  423. vf->vi=_ogg_realloc(vf->vi,vf->links*sizeof(*vf->vi));
  424. vf->vc=_ogg_realloc(vf->vc,vf->links*sizeof(*vf->vc));
  425. vf->serialnos=_ogg_malloc(vf->links*sizeof(*vf->serialnos));
  426. vf->dataoffsets=_ogg_malloc(vf->links*sizeof(*vf->dataoffsets));
  427. vf->pcmlengths=_ogg_malloc(vf->links*2*sizeof(*vf->pcmlengths));
  428. vf->offsets[m+1]=end;
  429. vf->offsets[m]=begin;
  430. vf->pcmlengths[m*2+1]=endgran;
  431. }else{
  432. long *next_serialno_list=NULL;
  433. int next_serialnos=0;
  434. vorbis_info vi;
  435. vorbis_comment vc;
  436. /* the below guards against garbage seperating the last and
  437. first pages of two links. */
  438. while(searched<endsearched){
  439. ogg_int64_t bisect;
  440. if(endsearched-searched<CHUNKSIZE){
  441. bisect=searched;
  442. }else{
  443. bisect=(searched+endsearched)/2;
  444. }
  445. ret=_seek_helper(vf,bisect);
  446. if(ret)return(ret);
  447. last=_get_next_page(vf,&og,-1);
  448. if(last==OV_EREAD)return(OV_EREAD);
  449. if(last<0 || !_lookup_page_serialno(&og,currentno_list,currentnos)){
  450. endsearched=bisect;
  451. if(last>=0)next=last;
  452. }else{
  453. searched=last+og.header_len+og.body_len;
  454. }
  455. }
  456. /* Bisection point found */
  457. /* for the time being, fetch end PCM offset the simple way */
  458. {
  459. int testserial = serialno+1;
  460. vf->offset = next;
  461. while(testserial != serialno){
  462. testserial = serialno;
  463. vf->offset=_get_prev_page_serial(vf,currentno_list,currentnos,&testserial,&searchgran);
  464. }
  465. }
  466. if(vf->offset!=next){
  467. ret=_seek_helper(vf,next);
  468. if(ret)return(ret);
  469. }
  470. ret=_fetch_headers(vf,&vi,&vc,&next_serialno_list,&next_serialnos,NULL);
  471. if(ret)return(ret);
  472. serialno = vf->os.serialno;
  473. dataoffset = vf->offset;
  474. /* this will consume a page, however the next bistection always
  475. starts with a raw seek */
  476. pcmoffset = _initial_pcmoffset(vf,&vi);
  477. ret=_bisect_forward_serialno(vf,next,vf->offset,end,endgran,endserial,
  478. next_serialno_list,next_serialnos,m+1);
  479. if(ret)return(ret);
  480. if(next_serialno_list)_ogg_free(next_serialno_list);
  481. vf->offsets[m+1]=next;
  482. vf->serialnos[m+1]=serialno;
  483. vf->dataoffsets[m+1]=dataoffset;
  484. vf->vi[m+1]=vi;
  485. vf->vc[m+1]=vc;
  486. vf->pcmlengths[m*2+1]=searchgran;
  487. vf->pcmlengths[m*2+2]=pcmoffset;
  488. vf->pcmlengths[m*2+3]-=pcmoffset;
  489. }
  490. return(0);
  491. }
  492. static int _make_decode_ready(OggVorbis_File *vf){
  493. if(vf->ready_state>STREAMSET)return 0;
  494. if(vf->ready_state<STREAMSET)return OV_EFAULT;
  495. if(vf->seekable){
  496. if(vorbis_synthesis_init(&vf->vd,vf->vi+vf->current_link))
  497. return OV_EBADLINK;
  498. }else{
  499. if(vorbis_synthesis_init(&vf->vd,vf->vi))
  500. return OV_EBADLINK;
  501. }
  502. vorbis_block_init(&vf->vd,&vf->vb);
  503. vf->ready_state=INITSET;
  504. vf->bittrack=0.f;
  505. vf->samptrack=0.f;
  506. return 0;
  507. }
  508. static int _open_seekable2(OggVorbis_File *vf){
  509. ogg_int64_t dataoffset=vf->dataoffsets[0],end,endgran=-1;
  510. int endserial=vf->os.serialno;
  511. int serialno=vf->os.serialno;
  512. /* we're partially open and have a first link header state in
  513. storage in vf */
  514. /* fetch initial PCM offset */
  515. ogg_int64_t pcmoffset = _initial_pcmoffset(vf,vf->vi);
  516. /* we can seek, so set out learning all about this file */
  517. if(vf->callbacks.seek_func && vf->callbacks.tell_func){
  518. (vf->callbacks.seek_func)(vf->datasource,0,SEEK_END);
  519. vf->offset=vf->end=(vf->callbacks.tell_func)(vf->datasource);
  520. }else{
  521. vf->offset=vf->end=-1;
  522. }
  523. /* If seek_func is implemented, tell_func must also be implemented */
  524. if(vf->end==-1) return(OV_EINVAL);
  525. /* Get the offset of the last page of the physical bitstream, or, if
  526. we're lucky the last vorbis page of this link as most OggVorbis
  527. files will contain a single logical bitstream */
  528. end=_get_prev_page_serial(vf,vf->serialnos+2,vf->serialnos[1],&endserial,&endgran);
  529. if(end<0)return(end);
  530. /* now determine bitstream structure recursively */
  531. if(_bisect_forward_serialno(vf,0,dataoffset,vf->offset,endgran,endserial,
  532. vf->serialnos+2,vf->serialnos[1],0)<0)return(OV_EREAD);
  533. vf->offsets[0]=0;
  534. vf->serialnos[0]=serialno;
  535. vf->dataoffsets[0]=dataoffset;
  536. vf->pcmlengths[0]=pcmoffset;
  537. vf->pcmlengths[1]-=pcmoffset;
  538. return(ov_raw_seek(vf,dataoffset));
  539. }
  540. /* clear out the current logical bitstream decoder */
  541. static void _decode_clear(OggVorbis_File *vf){
  542. vorbis_dsp_clear(&vf->vd);
  543. vorbis_block_clear(&vf->vb);
  544. vf->ready_state=OPENED;
  545. }
  546. /* fetch and process a packet. Handles the case where we're at a
  547. bitstream boundary and dumps the decoding machine. If the decoding
  548. machine is unloaded, it loads it. It also keeps pcm_offset up to
  549. date (seek and read both use this. seek uses a special hack with
  550. readp).
  551. return: <0) error, OV_HOLE (lost packet) or OV_EOF
  552. 0) need more data (only if readp==0)
  553. 1) got a packet
  554. */
  555. static int _fetch_and_process_packet(OggVorbis_File *vf,
  556. ogg_packet *op_in,
  557. int readp,
  558. int spanp){
  559. ogg_page og;
  560. /* handle one packet. Try to fetch it from current stream state */
  561. /* extract packets from page */
  562. while(1){
  563. /* process a packet if we can. If the machine isn't loaded,
  564. neither is a page */
  565. if(vf->ready_state==INITSET){
  566. while(1) {
  567. ogg_packet op;
  568. ogg_packet *op_ptr=(op_in?op_in:&op);
  569. int result=ogg_stream_packetout(&vf->os,op_ptr);
  570. ogg_int64_t granulepos;
  571. op_in=NULL;
  572. if(result==-1)return(OV_HOLE); /* hole in the data. */
  573. if(result>0){
  574. /* got a packet. process it */
  575. granulepos=op_ptr->granulepos;
  576. if(!vorbis_synthesis(&vf->vb,op_ptr)){ /* lazy check for lazy
  577. header handling. The
  578. header packets aren't
  579. audio, so if/when we
  580. submit them,
  581. vorbis_synthesis will
  582. reject them */
  583. /* suck in the synthesis data and track bitrate */
  584. {
  585. int oldsamples=vorbis_synthesis_pcmout(&vf->vd,NULL);
  586. /* for proper use of libvorbis within libvorbisfile,
  587. oldsamples will always be zero. */
  588. if(oldsamples)return(OV_EFAULT);
  589. vorbis_synthesis_blockin(&vf->vd,&vf->vb);
  590. vf->samptrack+=vorbis_synthesis_pcmout(&vf->vd,NULL)-oldsamples;
  591. vf->bittrack+=op_ptr->bytes*8;
  592. }
  593. /* update the pcm offset. */
  594. if(granulepos!=-1 && !op_ptr->e_o_s){
  595. int link=(vf->seekable?vf->current_link:0);
  596. int i,samples;
  597. /* this packet has a pcm_offset on it (the last packet
  598. completed on a page carries the offset) After processing
  599. (above), we know the pcm position of the *last* sample
  600. ready to be returned. Find the offset of the *first*
  601. As an aside, this trick is inaccurate if we begin
  602. reading anew right at the last page; the end-of-stream
  603. granulepos declares the last frame in the stream, and the
  604. last packet of the last page may be a partial frame.
  605. So, we need a previous granulepos from an in-sequence page
  606. to have a reference point. Thus the !op_ptr->e_o_s clause
  607. above */
  608. if(vf->seekable && link>0)
  609. granulepos-=vf->pcmlengths[link*2];
  610. if(granulepos<0)granulepos=0; /* actually, this
  611. shouldn't be possible
  612. here unless the stream
  613. is very broken */
  614. samples=vorbis_synthesis_pcmout(&vf->vd,NULL);
  615. granulepos-=samples;
  616. for(i=0;i<link;i++)
  617. granulepos+=vf->pcmlengths[i*2+1];
  618. vf->pcm_offset=granulepos;
  619. }
  620. return(1);
  621. }
  622. }
  623. else
  624. break;
  625. }
  626. }
  627. if(vf->ready_state>=OPENED){
  628. ogg_int64_t ret;
  629. while(1){
  630. /* the loop is not strictly necessary, but there's no sense in
  631. doing the extra checks of the larger loop for the common
  632. case in a multiplexed bistream where the page is simply
  633. part of a different logical bitstream; keep reading until
  634. we get one with the correct serialno */
  635. if(!readp)return(0);
  636. if((ret=_get_next_page(vf,&og,-1))<0){
  637. return(OV_EOF); /* eof. leave unitialized */
  638. }
  639. /* bitrate tracking; add the header's bytes here, the body bytes
  640. are done by packet above */
  641. vf->bittrack+=og.header_len*8;
  642. if(vf->ready_state==INITSET){
  643. if(vf->current_serialno!=ogg_page_serialno(&og)){
  644. /* two possibilities:
  645. 1) our decoding just traversed a bitstream boundary
  646. 2) another stream is multiplexed into this logical section */
  647. if(ogg_page_bos(&og)){
  648. /* boundary case */
  649. if(!spanp)
  650. return(OV_EOF);
  651. _decode_clear(vf);
  652. if(!vf->seekable){
  653. vorbis_info_clear(vf->vi);
  654. vorbis_comment_clear(vf->vc);
  655. }
  656. break;
  657. }else
  658. continue; /* possibility #2 */
  659. }
  660. }
  661. break;
  662. }
  663. }
  664. /* Do we need to load a new machine before submitting the page? */
  665. /* This is different in the seekable and non-seekable cases.
  666. In the seekable case, we already have all the header
  667. information loaded and cached; we just initialize the machine
  668. with it and continue on our merry way.
  669. In the non-seekable (streaming) case, we'll only be at a
  670. boundary if we just left the previous logical bitstream and
  671. we're now nominally at the header of the next bitstream
  672. */
  673. if(vf->ready_state!=INITSET){
  674. int link;
  675. if(vf->ready_state<STREAMSET){
  676. if(vf->seekable){
  677. long serialno = ogg_page_serialno(&og);
  678. /* match the serialno to bitstream section. We use this rather than
  679. offset positions to avoid problems near logical bitstream
  680. boundaries */
  681. for(link=0;link<vf->links;link++)
  682. if(vf->serialnos[link]==serialno)break;
  683. if(link==vf->links) continue; /* not the desired Vorbis
  684. bitstream section; keep
  685. trying */
  686. vf->current_serialno=serialno;
  687. vf->current_link=link;
  688. ogg_stream_reset_serialno(&vf->os,vf->current_serialno);
  689. vf->ready_state=STREAMSET;
  690. }else{
  691. /* we're streaming */
  692. /* fetch the three header packets, build the info struct */
  693. int ret=_fetch_headers(vf,vf->vi,vf->vc,NULL,NULL,&og);
  694. if(ret)return(ret);
  695. vf->current_serialno=vf->os.serialno;
  696. vf->current_link++;
  697. link=0;
  698. }
  699. }
  700. {
  701. int ret=_make_decode_ready(vf);
  702. if(ret<0)return ret;
  703. }
  704. }
  705. /* the buffered page is the data we want, and we're ready for it;
  706. add it to the stream state */
  707. ogg_stream_pagein(&vf->os,&og);
  708. }
  709. }
  710. /* if, eg, 64 bit stdio is configured by default, this will build with
  711. fseek64 */
  712. static int _fseek64_wrap(FILE *f,ogg_int64_t off,int whence){
  713. if(f==NULL)return(-1);
  714. return fseek(f,off,whence);
  715. }
  716. static int _ov_open1(void *f,OggVorbis_File *vf,char *initial,
  717. long ibytes, ov_callbacks callbacks){
  718. int offsettest=((f && callbacks.seek_func)?callbacks.seek_func(f,0,SEEK_CUR):-1);
  719. long *serialno_list=NULL;
  720. int serialno_list_size=0;
  721. int ret;
  722. memset(vf,0,sizeof(*vf));
  723. vf->datasource=f;
  724. vf->callbacks = callbacks;
  725. /* init the framing state */
  726. ogg_sync_init(&vf->oy);
  727. /* perhaps some data was previously read into a buffer for testing
  728. against other stream types. Allow initialization from this
  729. previously read data (especially as we may be reading from a
  730. non-seekable stream) */
  731. if(initial){
  732. char *buffer=ogg_sync_buffer(&vf->oy,ibytes);
  733. memcpy(buffer,initial,ibytes);
  734. ogg_sync_wrote(&vf->oy,ibytes);
  735. }
  736. /* can we seek? Stevens suggests the seek test was portable */
  737. if(offsettest!=-1)vf->seekable=1;
  738. /* No seeking yet; Set up a 'single' (current) logical bitstream
  739. entry for partial open */
  740. vf->links=1;
  741. vf->vi=_ogg_calloc(vf->links,sizeof(*vf->vi));
  742. vf->vc=_ogg_calloc(vf->links,sizeof(*vf->vc));
  743. ogg_stream_init(&vf->os,-1); /* fill in the serialno later */
  744. /* Fetch all BOS pages, store the vorbis header and all seen serial
  745. numbers, load subsequent vorbis setup headers */
  746. if((ret=_fetch_headers(vf,vf->vi,vf->vc,&serialno_list,&serialno_list_size,NULL))<0){
  747. vf->datasource=NULL;
  748. ov_clear(vf);
  749. }else{
  750. /* serial number list for first link needs to be held somewhere
  751. for second stage of seekable stream open; this saves having to
  752. seek/reread first link's serialnumber data then. */
  753. vf->serialnos=_ogg_calloc(serialno_list_size+2,sizeof(*vf->serialnos));
  754. vf->serialnos[0]=vf->current_serialno;
  755. vf->serialnos[1]=serialno_list_size;
  756. memcpy(vf->serialnos+2,serialno_list,serialno_list_size*sizeof(*vf->serialnos));
  757. vf->offsets=_ogg_calloc(1,sizeof(*vf->offsets));
  758. vf->dataoffsets=_ogg_calloc(1,sizeof(*vf->dataoffsets));
  759. vf->offsets[0]=0;
  760. vf->dataoffsets[0]=vf->offset;
  761. vf->current_serialno=vf->os.serialno;
  762. vf->ready_state=PARTOPEN;
  763. }
  764. if(serialno_list)_ogg_free(serialno_list);
  765. return(ret);
  766. }
  767. static int _ov_open2(OggVorbis_File *vf){
  768. if(vf->ready_state != PARTOPEN) return OV_EINVAL;
  769. vf->ready_state=OPENED;
  770. if(vf->seekable){
  771. int ret=_open_seekable2(vf);
  772. if(ret){
  773. vf->datasource=NULL;
  774. ov_clear(vf);
  775. }
  776. return(ret);
  777. }else
  778. vf->ready_state=STREAMSET;
  779. return 0;
  780. }
  781. /* clear out the OggVorbis_File struct */
  782. int ov_clear(OggVorbis_File *vf){
  783. if(vf){
  784. vorbis_block_clear(&vf->vb);
  785. vorbis_dsp_clear(&vf->vd);
  786. ogg_stream_clear(&vf->os);
  787. if(vf->vi && vf->links){
  788. int i;
  789. for(i=0;i<vf->links;i++){
  790. vorbis_info_clear(vf->vi+i);
  791. vorbis_comment_clear(vf->vc+i);
  792. }
  793. _ogg_free(vf->vi);
  794. _ogg_free(vf->vc);
  795. }
  796. if(vf->dataoffsets)_ogg_free(vf->dataoffsets);
  797. if(vf->pcmlengths)_ogg_free(vf->pcmlengths);
  798. if(vf->serialnos)_ogg_free(vf->serialnos);
  799. if(vf->offsets)_ogg_free(vf->offsets);
  800. ogg_sync_clear(&vf->oy);
  801. if(vf->datasource && vf->callbacks.close_func)
  802. (vf->callbacks.close_func)(vf->datasource);
  803. memset(vf,0,sizeof(*vf));
  804. }
  805. #ifdef DEBUG_LEAKS
  806. _VDBG_dump();
  807. #endif
  808. return(0);
  809. }
  810. /* inspects the OggVorbis file and finds/documents all the logical
  811. bitstreams contained in it. Tries to be tolerant of logical
  812. bitstream sections that are truncated/woogie.
  813. return: -1) error
  814. 0) OK
  815. */
  816. int ov_open_callbacks(void *f,OggVorbis_File *vf,char *initial,long ibytes,
  817. ov_callbacks callbacks){
  818. int ret=_ov_open1(f,vf,initial,ibytes,callbacks);
  819. if(ret)return ret;
  820. return _ov_open2(vf);
  821. }
  822. int ov_open(FILE *f,OggVorbis_File *vf,char *initial,long ibytes){
  823. ov_callbacks callbacks = {
  824. (size_t (*)(void *, size_t, size_t, void *)) fread,
  825. (int (*)(void *, ogg_int64_t, int)) _fseek64_wrap,
  826. (int (*)(void *)) fclose,
  827. (long (*)(void *)) ftell
  828. };
  829. return ov_open_callbacks((void *)f, vf, initial, ibytes, callbacks);
  830. }
  831. int ov_fopen(char *path,OggVorbis_File *vf){
  832. int ret;
  833. FILE *f = fopen(path,"rb");
  834. if(!f) return -1;
  835. ret = ov_open(f,vf,NULL,0);
  836. if(ret) fclose(f);
  837. return ret;
  838. }
  839. /* cheap hack for game usage where downsampling is desirable; there's
  840. no need for SRC as we can just do it cheaply in libvorbis. */
  841. int ov_halfrate(OggVorbis_File *vf,int flag){
  842. int i;
  843. if(vf->vi==NULL)return OV_EINVAL;
  844. if(!vf->seekable)return OV_EINVAL;
  845. if(vf->ready_state>=STREAMSET)
  846. _decode_clear(vf); /* clear out stream state; later on libvorbis
  847. will be able to swap this on the fly, but
  848. for now dumping the decode machine is needed
  849. to reinit the MDCT lookups. 1.1 libvorbis
  850. is planned to be able to switch on the fly */
  851. for(i=0;i<vf->links;i++){
  852. if(vorbis_synthesis_halfrate(vf->vi+i,flag)){
  853. ov_halfrate(vf,0);
  854. return OV_EINVAL;
  855. }
  856. }
  857. return 0;
  858. }
  859. int ov_halfrate_p(OggVorbis_File *vf){
  860. if(vf->vi==NULL)return OV_EINVAL;
  861. return vorbis_synthesis_halfrate_p(vf->vi);
  862. }
  863. /* Only partially open the vorbis file; test for Vorbisness, and load
  864. the headers for the first chain. Do not seek (although test for
  865. seekability). Use ov_test_open to finish opening the file, else
  866. ov_clear to close/free it. Same return codes as open. */
  867. int ov_test_callbacks(void *f,OggVorbis_File *vf,char *initial,long ibytes,
  868. ov_callbacks callbacks)
  869. {
  870. return _ov_open1(f,vf,initial,ibytes,callbacks);
  871. }
  872. int ov_test(FILE *f,OggVorbis_File *vf,char *initial,long ibytes){
  873. ov_callbacks callbacks = {
  874. (size_t (*)(void *, size_t, size_t, void *)) fread,
  875. (int (*)(void *, ogg_int64_t, int)) _fseek64_wrap,
  876. (int (*)(void *)) fclose,
  877. (long (*)(void *)) ftell
  878. };
  879. return ov_test_callbacks((void *)f, vf, initial, ibytes, callbacks);
  880. }
  881. int ov_test_open(OggVorbis_File *vf){
  882. if(vf->ready_state!=PARTOPEN)return(OV_EINVAL);
  883. return _ov_open2(vf);
  884. }
  885. /* How many logical bitstreams in this physical bitstream? */
  886. long ov_streams(OggVorbis_File *vf){
  887. return vf->links;
  888. }
  889. /* Is the FILE * associated with vf seekable? */
  890. long ov_seekable(OggVorbis_File *vf){
  891. return vf->seekable;
  892. }
  893. /* returns the bitrate for a given logical bitstream or the entire
  894. physical bitstream. If the file is open for random access, it will
  895. find the *actual* average bitrate. If the file is streaming, it
  896. returns the nominal bitrate (if set) else the average of the
  897. upper/lower bounds (if set) else -1 (unset).
  898. If you want the actual bitrate field settings, get them from the
  899. vorbis_info structs */
  900. long ov_bitrate(OggVorbis_File *vf,int i){
  901. if(vf->ready_state<OPENED)return(OV_EINVAL);
  902. if(i>=vf->links)return(OV_EINVAL);
  903. if(!vf->seekable && i!=0)return(ov_bitrate(vf,0));
  904. if(i<0){
  905. ogg_int64_t bits=0;
  906. int i;
  907. float br;
  908. for(i=0;i<vf->links;i++)
  909. bits+=(vf->offsets[i+1]-vf->dataoffsets[i])*8;
  910. /* This once read: return(rint(bits/ov_time_total(vf,-1)));
  911. * gcc 3.x on x86 miscompiled this at optimisation level 2 and above,
  912. * so this is slightly transformed to make it work.
  913. */
  914. br = bits/ov_time_total(vf,-1);
  915. return(rint(br));
  916. }else{
  917. if(vf->seekable){
  918. /* return the actual bitrate */
  919. return(rint((vf->offsets[i+1]-vf->dataoffsets[i])*8/ov_time_total(vf,i)));
  920. }else{
  921. /* return nominal if set */
  922. if(vf->vi[i].bitrate_nominal>0){
  923. return vf->vi[i].bitrate_nominal;
  924. }else{
  925. if(vf->vi[i].bitrate_upper>0){
  926. if(vf->vi[i].bitrate_lower>0){
  927. return (vf->vi[i].bitrate_upper+vf->vi[i].bitrate_lower)/2;
  928. }else{
  929. return vf->vi[i].bitrate_upper;
  930. }
  931. }
  932. return(OV_FALSE);
  933. }
  934. }
  935. }
  936. }
  937. /* returns the actual bitrate since last call. returns -1 if no
  938. additional data to offer since last call (or at beginning of stream),
  939. EINVAL if stream is only partially open
  940. */
  941. long ov_bitrate_instant(OggVorbis_File *vf){
  942. int link=(vf->seekable?vf->current_link:0);
  943. long ret;
  944. if(vf->ready_state<OPENED)return(OV_EINVAL);
  945. if(vf->samptrack==0)return(OV_FALSE);
  946. ret=vf->bittrack/vf->samptrack*vf->vi[link].rate+.5;
  947. vf->bittrack=0.f;
  948. vf->samptrack=0.f;
  949. return(ret);
  950. }
  951. /* Guess */
  952. long ov_serialnumber(OggVorbis_File *vf,int i){
  953. if(i>=vf->links)return(ov_serialnumber(vf,vf->links-1));
  954. if(!vf->seekable && i>=0)return(ov_serialnumber(vf,-1));
  955. if(i<0){
  956. return(vf->current_serialno);
  957. }else{
  958. return(vf->serialnos[i]);
  959. }
  960. }
  961. /* returns: total raw (compressed) length of content if i==-1
  962. raw (compressed) length of that logical bitstream for i==0 to n
  963. OV_EINVAL if the stream is not seekable (we can't know the length)
  964. or if stream is only partially open
  965. */
  966. ogg_int64_t ov_raw_total(OggVorbis_File *vf,int i){
  967. if(vf->ready_state<OPENED)return(OV_EINVAL);
  968. if(!vf->seekable || i>=vf->links)return(OV_EINVAL);
  969. if(i<0){
  970. ogg_int64_t acc=0;
  971. int i;
  972. for(i=0;i<vf->links;i++)
  973. acc+=ov_raw_total(vf,i);
  974. return(acc);
  975. }else{
  976. return(vf->offsets[i+1]-vf->offsets[i]);
  977. }
  978. }
  979. /* returns: total PCM length (samples) of content if i==-1 PCM length
  980. (samples) of that logical bitstream for i==0 to n
  981. OV_EINVAL if the stream is not seekable (we can't know the
  982. length) or only partially open
  983. */
  984. ogg_int64_t ov_pcm_total(OggVorbis_File *vf,int i){
  985. if(vf->ready_state<OPENED)return(OV_EINVAL);
  986. if(!vf->seekable || i>=vf->links)return(OV_EINVAL);
  987. if(i<0){
  988. ogg_int64_t acc=0;
  989. int i;
  990. for(i=0;i<vf->links;i++)
  991. acc+=ov_pcm_total(vf,i);
  992. return(acc);
  993. }else{
  994. return(vf->pcmlengths[i*2+1]);
  995. }
  996. }
  997. /* returns: total seconds of content if i==-1
  998. seconds in that logical bitstream for i==0 to n
  999. OV_EINVAL if the stream is not seekable (we can't know the
  1000. length) or only partially open
  1001. */
  1002. double ov_time_total(OggVorbis_File *vf,int i){
  1003. if(vf->ready_state<OPENED)return(OV_EINVAL);
  1004. if(!vf->seekable || i>=vf->links)return(OV_EINVAL);
  1005. if(i<0){
  1006. double acc=0;
  1007. int i;
  1008. for(i=0;i<vf->links;i++)
  1009. acc+=ov_time_total(vf,i);
  1010. return(acc);
  1011. }else{
  1012. return((double)(vf->pcmlengths[i*2+1])/vf->vi[i].rate);
  1013. }
  1014. }
  1015. /* seek to an offset relative to the *compressed* data. This also
  1016. scans packets to update the PCM cursor. It will cross a logical
  1017. bitstream boundary, but only if it can't get any packets out of the
  1018. tail of the bitstream we seek to (so no surprises).
  1019. returns zero on success, nonzero on failure */
  1020. int ov_raw_seek(OggVorbis_File *vf,ogg_int64_t pos){
  1021. ogg_stream_state work_os;
  1022. int ret;
  1023. if(vf->ready_state<OPENED)return(OV_EINVAL);
  1024. if(!vf->seekable)
  1025. return(OV_ENOSEEK); /* don't dump machine if we can't seek */
  1026. if(pos<0 || pos>vf->end)return(OV_EINVAL);
  1027. /* don't yet clear out decoding machine (if it's initialized), in
  1028. the case we're in the same link. Restart the decode lapping, and
  1029. let _fetch_and_process_packet deal with a potential bitstream
  1030. boundary */
  1031. vf->pcm_offset=-1;
  1032. ogg_stream_reset_serialno(&vf->os,
  1033. vf->current_serialno); /* must set serialno */
  1034. vorbis_synthesis_restart(&vf->vd);
  1035. ret=_seek_helper(vf,pos);
  1036. if(ret)goto seek_error;
  1037. /* we need to make sure the pcm_offset is set, but we don't want to
  1038. advance the raw cursor past good packets just to get to the first
  1039. with a granulepos. That's not equivalent behavior to beginning
  1040. decoding as immediately after the seek position as possible.
  1041. So, a hack. We use two stream states; a local scratch state and
  1042. the shared vf->os stream state. We use the local state to
  1043. scan, and the shared state as a buffer for later decode.
  1044. Unfortuantely, on the last page we still advance to last packet
  1045. because the granulepos on the last page is not necessarily on a
  1046. packet boundary, and we need to make sure the granpos is
  1047. correct.
  1048. */
  1049. {
  1050. ogg_page og;
  1051. ogg_packet op;
  1052. int lastblock=0;
  1053. int accblock=0;
  1054. int thisblock=0;
  1055. int eosflag=0;
  1056. ogg_stream_init(&work_os,vf->current_serialno); /* get the memory ready */
  1057. ogg_stream_reset(&work_os); /* eliminate the spurious OV_HOLE
  1058. return from not necessarily
  1059. starting from the beginning */
  1060. while(1){
  1061. if(vf->ready_state>=STREAMSET){
  1062. /* snarf/scan a packet if we can */
  1063. int result=ogg_stream_packetout(&work_os,&op);
  1064. if(result>0){
  1065. if(vf->vi[vf->current_link].codec_setup){
  1066. thisblock=vorbis_packet_blocksize(vf->vi+vf->current_link,&op);
  1067. if(thisblock<0){
  1068. ogg_stream_packetout(&vf->os,NULL);
  1069. thisblock=0;
  1070. }else{
  1071. if(eosflag)
  1072. ogg_stream_packetout(&vf->os,NULL);
  1073. else
  1074. if(lastblock)accblock+=(lastblock+thisblock)>>2;
  1075. }
  1076. if(op.granulepos!=-1){
  1077. int i,link=vf->current_link;
  1078. ogg_int64_t granulepos=op.granulepos-vf->pcmlengths[link*2];
  1079. if(granulepos<0)granulepos=0;
  1080. for(i=0;i<link;i++)
  1081. granulepos+=vf->pcmlengths[i*2+1];
  1082. vf->pcm_offset=granulepos-accblock;
  1083. break;
  1084. }
  1085. lastblock=thisblock;
  1086. continue;
  1087. }else
  1088. ogg_stream_packetout(&vf->os,NULL);
  1089. }
  1090. }
  1091. if(!lastblock){
  1092. if(_get_next_page(vf,&og,-1)<0){
  1093. vf->pcm_offset=ov_pcm_total(vf,-1);
  1094. break;
  1095. }
  1096. }else{
  1097. /* huh? Bogus stream with packets but no granulepos */
  1098. vf->pcm_offset=-1;
  1099. break;
  1100. }
  1101. /* has our decoding just traversed a bitstream boundary? */
  1102. if(vf->ready_state>=STREAMSET){
  1103. if(vf->current_serialno!=ogg_page_serialno(&og)){
  1104. /* two possibilities:
  1105. 1) our decoding just traversed a bitstream boundary
  1106. 2) another stream is multiplexed into this logical section? */
  1107. if(ogg_page_bos(&og)){
  1108. /* we traversed */
  1109. _decode_clear(vf); /* clear out stream state */
  1110. ogg_stream_clear(&work_os);
  1111. } /* else, do nothing; next loop will scoop another page */
  1112. }
  1113. }
  1114. if(vf->ready_state<STREAMSET){
  1115. int link;
  1116. long serialno = ogg_page_serialno(&og);
  1117. for(link=0;link<vf->links;link++)
  1118. if(vf->serialnos[link]==serialno)break;
  1119. if(link==vf->links) continue; /* not the desired Vorbis
  1120. bitstream section; keep
  1121. trying */
  1122. vf->current_link=link;
  1123. vf->current_serialno=serialno;
  1124. ogg_stream_reset_serialno(&vf->os,serialno);
  1125. ogg_stream_reset_serialno(&work_os,serialno);
  1126. vf->ready_state=STREAMSET;
  1127. }
  1128. ogg_stream_pagein(&vf->os,&og);
  1129. ogg_stream_pagein(&work_os,&og);
  1130. eosflag=ogg_page_eos(&og);
  1131. }
  1132. }
  1133. ogg_stream_clear(&work_os);
  1134. vf->bittrack=0.f;
  1135. vf->samptrack=0.f;
  1136. return(0);
  1137. seek_error:
  1138. /* dump the machine so we're in a known state */
  1139. vf->pcm_offset=-1;
  1140. ogg_stream_clear(&work_os);
  1141. _decode_clear(vf);
  1142. return OV_EBADLINK;
  1143. }
  1144. /* Page granularity seek (faster than sample granularity because we
  1145. don't do the last bit of decode to find a specific sample).
  1146. Seek to the last [granule marked] page preceeding the specified pos
  1147. location, such that decoding past the returned point will quickly
  1148. arrive at the requested position. */
  1149. int ov_pcm_seek_page(OggVorbis_File *vf,ogg_int64_t pos){
  1150. int link=-1;
  1151. ogg_int64_t result=0;
  1152. ogg_int64_t total=ov_pcm_total(vf,-1);
  1153. if(vf->ready_state<OPENED)return(OV_EINVAL);
  1154. if(!vf->seekable)return(OV_ENOSEEK);
  1155. if(pos<0 || pos>total)return(OV_EINVAL);
  1156. /* which bitstream section does this pcm offset occur in? */
  1157. for(link=vf->links-1;link>=0;link--){
  1158. total-=vf->pcmlengths[link*2+1];
  1159. if(pos>=total)break;
  1160. }
  1161. /* search within the logical bitstream for the page with the highest
  1162. pcm_pos preceeding (or equal to) pos. There is a danger here;
  1163. missing pages or incorrect frame number information in the
  1164. bitstream could make our task impossible. Account for that (it
  1165. would be an error condition) */
  1166. /* new search algorithm by HB (Nicholas Vinen) */
  1167. {
  1168. ogg_int64_t end=vf->offsets[link+1];
  1169. ogg_int64_t begin=vf->offsets[link];
  1170. ogg_int64_t begintime = vf->pcmlengths[link*2];
  1171. ogg_int64_t endtime = vf->pcmlengths[link*2+1]+begintime;
  1172. ogg_int64_t target=pos-total+begintime;
  1173. ogg_int64_t best=begin;
  1174. ogg_page og;
  1175. while(begin<end){
  1176. ogg_int64_t bisect;
  1177. if(end-begin<CHUNKSIZE){
  1178. bisect=begin;
  1179. }else{
  1180. /* take a (pretty decent) guess. */
  1181. bisect=begin +
  1182. (ogg_int64_t)((double)(target-begintime)*(end-begin)/(endtime-begintime))
  1183. - CHUNKSIZE;
  1184. if(bisect<=begin)
  1185. bisect=begin+1;
  1186. }
  1187. result=_seek_helper(vf,bisect);
  1188. if(result) goto seek_error;
  1189. while(begin<end){
  1190. result=_get_next_page(vf,&og,end-vf->offset);
  1191. if(result==OV_EREAD) goto seek_error;
  1192. if(result<0){
  1193. if(bisect<=begin+1)
  1194. end=begin; /* found it */
  1195. else{
  1196. if(bisect==0) goto seek_error;
  1197. bisect-=CHUNKSIZE;
  1198. if(bisect<=begin)bisect=begin+1;
  1199. result=_seek_helper(vf,bisect);
  1200. if(result) goto seek_error;
  1201. }
  1202. }else{
  1203. ogg_int64_t granulepos;
  1204. if(ogg_page_serialno(&og)!=vf->serialnos[link])
  1205. continue;
  1206. granulepos=ogg_page_granulepos(&og);
  1207. if(granulepos==-1)continue;
  1208. if(granulepos<target){
  1209. best=result; /* raw offset of packet with granulepos */
  1210. begin=vf->offset; /* raw offset of next page */
  1211. begintime=granulepos;
  1212. if(target-begintime>44100)break;
  1213. bisect=begin; /* *not* begin + 1 */
  1214. }else{
  1215. if(bisect<=begin+1)
  1216. end=begin; /* found it */
  1217. else{
  1218. if(end==vf->offset){ /* we're pretty close - we'd be stuck in */
  1219. end=result;
  1220. bisect-=CHUNKSIZE; /* an endless loop otherwise. */
  1221. if(bisect<=begin)bisect=begin+1;
  1222. result=_seek_helper(vf,bisect);
  1223. if(result) goto seek_error;
  1224. }else{
  1225. end=bisect;
  1226. endtime=granulepos;
  1227. break;
  1228. }
  1229. }
  1230. }
  1231. }
  1232. }
  1233. }
  1234. /* found our page. seek to it, update pcm offset. Easier case than
  1235. raw_seek, don't keep packets preceeding granulepos. */
  1236. {
  1237. ogg_page og;
  1238. ogg_packet op;
  1239. /* seek */
  1240. result=_seek_helper(vf,best);
  1241. vf->pcm_offset=-1;
  1242. if(result) goto seek_error;
  1243. result=_get_next_page(vf,&og,-1);
  1244. if(result<0) goto seek_error;
  1245. if(link!=vf->current_link){
  1246. /* Different link; dump entire decode machine */
  1247. _decode_clear(vf);
  1248. vf->current_link=link;
  1249. vf->current_serialno=vf->serialnos[link];
  1250. vf->ready_state=STREAMSET;
  1251. }else{
  1252. vorbis_synthesis_restart(&vf->vd);
  1253. }
  1254. ogg_stream_reset_serialno(&vf->os,vf->current_serialno);
  1255. ogg_stream_pagein(&vf->os,&og);
  1256. /* pull out all but last packet; the one with granulepos */
  1257. while(1){
  1258. result=ogg_stream_packetpeek(&vf->os,&op);
  1259. if(result==0){
  1260. /* !!! the packet finishing this page originated on a
  1261. preceeding page. Keep fetching previous pages until we
  1262. get one with a granulepos or without the 'continued' flag
  1263. set. Then just use raw_seek for simplicity. */
  1264. result=_seek_helper(vf,best);
  1265. if(result<0) goto seek_error;
  1266. while(1){
  1267. result=_get_prev_page(vf,&og);
  1268. if(result<0) goto seek_error;
  1269. if(ogg_page_serialno(&og)==vf->current_serialno &&
  1270. (ogg_page_granulepos(&og)>-1 ||
  1271. !ogg_page_continued(&og))){
  1272. return ov_raw_seek(vf,result);
  1273. }
  1274. vf->offset=result;
  1275. }
  1276. }
  1277. if(result<0){
  1278. result = OV_EBADPACKET;
  1279. goto seek_error;
  1280. }
  1281. if(op.granulepos!=-1){
  1282. vf->pcm_offset=op.granulepos-vf->pcmlengths[vf->current_link*2];
  1283. if(vf->pcm_offset<0)vf->pcm_offset=0;
  1284. vf->pcm_offset+=total;
  1285. break;
  1286. }else
  1287. result=ogg_stream_packetout(&vf->os,NULL);
  1288. }
  1289. }
  1290. }
  1291. /* verify result */
  1292. if(vf->pcm_offset>pos || pos>ov_pcm_total(vf,-1)){
  1293. result=OV_EFAULT;
  1294. goto seek_error;
  1295. }
  1296. vf->bittrack=0.f;
  1297. vf->samptrack=0.f;
  1298. return(0);
  1299. seek_error:
  1300. /* dump machine so we're in a known state */
  1301. vf->pcm_offset=-1;
  1302. _decode_clear(vf);
  1303. return (int)result;
  1304. }
  1305. /* seek to a sample offset relative to the decompressed pcm stream
  1306. returns zero on success, nonzero on failure */
  1307. int ov_pcm_seek(OggVorbis_File *vf,ogg_int64_t pos){
  1308. int thisblock,lastblock=0;
  1309. int ret=ov_pcm_seek_page(vf,pos);
  1310. if(ret<0)return(ret);
  1311. if((ret=_make_decode_ready(vf)))return ret;
  1312. /* discard leading packets we don't need for the lapping of the
  1313. position we want; don't decode them */
  1314. while(1){
  1315. ogg_packet op;
  1316. ogg_page og;
  1317. int ret=ogg_stream_packetpeek(&vf->os,&op);
  1318. if(ret>0){
  1319. thisblock=vorbis_packet_blocksize(vf->vi+vf->current_link,&op);
  1320. if(thisblock<0){
  1321. ogg_stream_packetout(&vf->os,NULL);
  1322. continue; /* non audio packet */
  1323. }
  1324. if(lastblock)vf->pcm_offset+=(lastblock+thisblock)>>2;
  1325. if(vf->pcm_offset+((thisblock+
  1326. vorbis_info_blocksize(vf->vi,1))>>2)>=pos)break;
  1327. /* remove the packet from packet queue and track its granulepos */
  1328. ogg_stream_packetout(&vf->os,NULL);
  1329. vorbis_synthesis_trackonly(&vf->vb,&op); /* set up a vb with
  1330. only tracking, no
  1331. pcm_decode */
  1332. vorbis_synthesis_blockin(&vf->vd,&vf->vb);
  1333. /* end of logical stream case is hard, especially with exact
  1334. length positioning. */
  1335. if(op.granulepos>-1){
  1336. int i;
  1337. /* always believe the stream markers */
  1338. vf->pcm_offset=op.granulepos-vf->pcmlengths[vf->current_link*2];
  1339. if(vf->pcm_offset<0)vf->pcm_offset=0;
  1340. for(i=0;i<vf->current_link;i++)
  1341. vf->pcm_offset+=vf->pcmlengths[i*2+1];
  1342. }
  1343. lastblock=thisblock;
  1344. }else{
  1345. if(ret<0 && ret!=OV_HOLE)break;
  1346. /* suck in a new page */
  1347. if(_get_next_page(vf,&og,-1)<0)break;
  1348. if(ogg_page_bos(&og))_decode_clear(vf);
  1349. if(vf->ready_state<STREAMSET){
  1350. long serialno=ogg_page_serialno(&og);
  1351. int link;
  1352. for(link=0;link<vf->links;link++)
  1353. if(vf->serialnos[link]==serialno)break;
  1354. if(link==vf->links) continue;
  1355. vf->current_link=link;
  1356. vf->ready_state=STREAMSET;
  1357. vf->current_serialno=ogg_page_serialno(&og);
  1358. ogg_stream_reset_serialno(&vf->os,serialno);
  1359. ret=_make_decode_ready(vf);
  1360. if(ret)return ret;
  1361. lastblock=0;
  1362. }
  1363. ogg_stream_pagein(&vf->os,&og);
  1364. }
  1365. }
  1366. vf->bittrack=0.f;
  1367. vf->samptrack=0.f;
  1368. /* discard samples until we reach the desired position. Crossing a
  1369. logical bitstream boundary with abandon is OK. */
  1370. while(vf->pcm_offset<pos){
  1371. ogg_int64_t target=pos-vf->pcm_offset;
  1372. long samples=vorbis_synthesis_pcmout(&vf->vd,NULL);
  1373. if(samples>target)samples=target;
  1374. vorbis_synthesis_read(&vf->vd,samples);
  1375. vf->pcm_offset+=samples;
  1376. if(samples<target)
  1377. if(_fetch_and_process_packet(vf,NULL,1,1)<=0)
  1378. vf->pcm_offset=ov_pcm_total(vf,-1); /* eof */
  1379. }
  1380. return 0;
  1381. }
  1382. /* seek to a playback time relative to the decompressed pcm stream
  1383. returns zero on success, nonzero on failure */
  1384. int ov_time_seek(OggVorbis_File *vf,double seconds){
  1385. /* translate time to PCM position and call ov_pcm_seek */
  1386. int link=-1;
  1387. ogg_int64_t pcm_total=0;
  1388. double time_total=0.;
  1389. if(vf->ready_state<OPENED)return(OV_EINVAL);
  1390. if(!vf->seekable)return(OV_ENOSEEK);
  1391. if(seconds<0)return(OV_EINVAL);
  1392. /* which bitstream section does this time offset occur in? */
  1393. for(link=0;link<vf->links;link++){
  1394. double addsec = ov_time_total(vf,link);
  1395. if(seconds<time_total+addsec)break;
  1396. time_total+=addsec;
  1397. pcm_total+=vf->pcmlengths[link*2+1];
  1398. }
  1399. if(link==vf->links)return(OV_EINVAL);
  1400. /* enough information to convert time offset to pcm offset */
  1401. {
  1402. ogg_int64_t target=pcm_total+(seconds-time_total)*vf->vi[link].rate;
  1403. return(ov_pcm_seek(vf,target));
  1404. }
  1405. }
  1406. /* page-granularity version of ov_time_seek
  1407. returns zero on success, nonzero on failure */
  1408. int ov_time_seek_page(OggVorbis_File *vf,double seconds){
  1409. /* translate time to PCM position and call ov_pcm_seek */
  1410. int link=-1;
  1411. ogg_int64_t pcm_total=0;
  1412. double time_total=0.;
  1413. if(vf->ready_state<OPENED)return(OV_EINVAL);
  1414. if(!vf->seekable)return(OV_ENOSEEK);
  1415. if(seconds<0)return(OV_EINVAL);
  1416. /* which bitstream section does this time offset occur in? */
  1417. for(link=0;link<vf->links;link++){
  1418. double addsec = ov_time_total(vf,link);
  1419. if(seconds<time_total+addsec)break;
  1420. time_total+=addsec;
  1421. pcm_total+=vf->pcmlengths[link*2+1];
  1422. }
  1423. if(link==vf->links)return(OV_EINVAL);
  1424. /* enough information to convert time offset to pcm offset */
  1425. {
  1426. ogg_int64_t target=pcm_total+(seconds-time_total)*vf->vi[link].rate;
  1427. return(ov_pcm_seek_page(vf,target));
  1428. }
  1429. }
  1430. /* tell the current stream offset cursor. Note that seek followed by
  1431. tell will likely not give the set offset due to caching */
  1432. ogg_int64_t ov_raw_tell(OggVorbis_File *vf){
  1433. if(vf->ready_state<OPENED)return(OV_EINVAL);
  1434. return(vf->offset);
  1435. }
  1436. /* return PCM offset (sample) of next PCM sample to be read */
  1437. ogg_int64_t ov_pcm_tell(OggVorbis_File *vf){
  1438. if(vf->ready_state<OPENED)return(OV_EINVAL);
  1439. return(vf->pcm_offset);
  1440. }
  1441. /* return time offset (seconds) of next PCM sample to be read */
  1442. double ov_time_tell(OggVorbis_File *vf){
  1443. int link=0;
  1444. ogg_int64_t pcm_total=0;
  1445. double time_total=0.f;
  1446. if(vf->ready_state<OPENED)return(OV_EINVAL);
  1447. if(vf->seekable){
  1448. pcm_total=ov_pcm_total(vf,-1);
  1449. time_total=ov_time_total(vf,-1);
  1450. /* which bitstream section does this time offset occur in? */
  1451. for(link=vf->links-1;link>=0;link--){
  1452. pcm_total-=vf->pcmlengths[link*2+1];
  1453. time_total-=ov_time_total(vf,link);
  1454. if(vf->pcm_offset>=pcm_total)break;
  1455. }
  1456. }
  1457. return((double)time_total+(double)(vf->pcm_offset-pcm_total)/vf->vi[link].rate);
  1458. }
  1459. /* link: -1) return the vorbis_info struct for the bitstream section
  1460. currently being decoded
  1461. 0-n) to request information for a specific bitstream section
  1462. In the case of a non-seekable bitstream, any call returns the
  1463. current bitstream. NULL in the case that the machine is not
  1464. initialized */
  1465. vorbis_info *ov_info(OggVorbis_File *vf,int link){
  1466. if(vf->seekable){
  1467. if(link<0)
  1468. if(vf->ready_state>=STREAMSET)
  1469. return vf->vi+vf->current_link;
  1470. else
  1471. return vf->vi;
  1472. else
  1473. if(link>=vf->links)
  1474. return NULL;
  1475. else
  1476. return vf->vi+link;
  1477. }else{
  1478. return vf->vi;
  1479. }
  1480. }
  1481. /* grr, strong typing, grr, no templates/inheritence, grr */
  1482. vorbis_comment *ov_comment(OggVorbis_File *vf,int link){
  1483. if(vf->seekable){
  1484. if(link<0)
  1485. if(vf->ready_state>=STREAMSET)
  1486. return vf->vc+vf->current_link;
  1487. else
  1488. return vf->vc;
  1489. else
  1490. if(link>=vf->links)
  1491. return NULL;
  1492. else
  1493. return vf->vc+link;
  1494. }else{
  1495. return vf->vc;
  1496. }
  1497. }
  1498. static int host_is_big_endian() {
  1499. ogg_int32_t pattern = 0xfeedface; /* deadbeef */
  1500. unsigned char *bytewise = (unsigned char *)&pattern;
  1501. if (bytewise[0] == 0xfe) return 1;
  1502. return 0;
  1503. }
  1504. /* up to this point, everything could more or less hide the multiple
  1505. logical bitstream nature of chaining from the toplevel application
  1506. if the toplevel application didn't particularly care. However, at
  1507. the point that we actually read audio back, the multiple-section
  1508. nature must surface: Multiple bitstream sections do not necessarily
  1509. have to have the same number of channels or sampling rate.
  1510. ov_read returns the sequential logical bitstream number currently
  1511. being decoded along with the PCM data in order that the toplevel
  1512. application can take action on channel/sample rate changes. This
  1513. number will be incremented even for streamed (non-seekable) streams
  1514. (for seekable streams, it represents the actual logical bitstream
  1515. index within the physical bitstream. Note that the accessor
  1516. functions above are aware of this dichotomy).
  1517. ov_read_filter is exactly the same as ov_read except that it processes
  1518. the decoded audio data through a filter before packing it into the
  1519. requested format. This gives greater accuracy than applying a filter
  1520. after the audio has been converted into integral PCM.
  1521. input values: buffer) a buffer to hold packed PCM data for return
  1522. length) the byte length requested to be placed into buffer
  1523. bigendianp) should the data be packed LSB first (0) or
  1524. MSB first (1)
  1525. word) word size for output. currently 1 (byte) or
  1526. 2 (16 bit short)
  1527. return values: <0) error/hole in data (OV_HOLE), partial open (OV_EINVAL)
  1528. 0) EOF
  1529. n) number of bytes of PCM actually returned. The
  1530. below works on a packet-by-packet basis, so the
  1531. return length is not related to the 'length' passed
  1532. in, just guaranteed to fit.
  1533. *section) set to the logical bitstream number */
  1534. long ov_read_filter(OggVorbis_File *vf,char *buffer,int length,
  1535. int bigendianp,int word,int sgned,int *bitstream,
  1536. void (*filter)(float **pcm,long channels,long samples,void *filter_param),void *filter_param){
  1537. int i,j;
  1538. int host_endian = host_is_big_endian();
  1539. float **pcm;
  1540. long samples;
  1541. if(vf->ready_state<OPENED)return(OV_EINVAL);
  1542. while(1){
  1543. if(vf->ready_state==INITSET){
  1544. samples=vorbis_synthesis_pcmout(&vf->vd,&pcm);
  1545. if(samples)break;
  1546. }
  1547. /* suck in another packet */
  1548. {
  1549. int ret=_fetch_and_process_packet(vf,NULL,1,1);
  1550. if(ret==OV_EOF)
  1551. return(0);
  1552. if(ret<=0)
  1553. return(ret);
  1554. }
  1555. }
  1556. if(samples>0){
  1557. /* yay! proceed to pack data into the byte buffer */
  1558. long channels=ov_info(vf,-1)->channels;
  1559. long bytespersample=word * channels;
  1560. vorbis_fpu_control fpu;
  1561. if(samples>length/bytespersample)samples=length/bytespersample;
  1562. if(samples <= 0)
  1563. return OV_EINVAL;
  1564. /* Here. */
  1565. if(filter)
  1566. filter(pcm,channels,samples,filter_param);
  1567. /* a tight loop to pack each size */
  1568. {
  1569. int val;
  1570. if(word==1){
  1571. int off=(sgned?0:128);
  1572. vorbis_fpu_setround(&fpu);
  1573. for(j=0;j<samples;j++)
  1574. for(i=0;i<channels;i++){
  1575. val=vorbis_ftoi(pcm[i][j]*128.f);
  1576. if(val>127)val=127;
  1577. else if(val<-128)val=-128;
  1578. *buffer++=val+off;
  1579. }
  1580. vorbis_fpu_restore(fpu);
  1581. }else{
  1582. int off=(sgned?0:32768);
  1583. if(host_endian==bigendianp){
  1584. if(sgned){
  1585. vorbis_fpu_setround(&fpu);
  1586. for(i=0;i<channels;i++) { /* It's faster in this order */
  1587. float *src=pcm[i];
  1588. short *dest=((short *)buffer)+i;
  1589. for(j=0;j<samples;j++) {
  1590. val=vorbis_ftoi(src[j]*32768.f);
  1591. if(val>32767)val=32767;
  1592. else if(val<-32768)val=-32768;
  1593. *dest=val;
  1594. dest+=channels;
  1595. }
  1596. }
  1597. vorbis_fpu_restore(fpu);
  1598. }else{
  1599. vorbis_fpu_setround(&fpu);
  1600. for(i=0;i<channels;i++) {
  1601. float *src=pcm[i];
  1602. short *dest=((short *)buffer)+i;
  1603. for(j=0;j<samples;j++) {
  1604. val=vorbis_ftoi(src[j]*32768.f);
  1605. if(val>32767)val=32767;
  1606. else if(val<-32768)val=-32768;
  1607. *dest=val+off;
  1608. dest+=channels;
  1609. }
  1610. }
  1611. vorbis_fpu_restore(fpu);
  1612. }
  1613. }else if(bigendianp){
  1614. vorbis_fpu_setround(&fpu);
  1615. for(j=0;j<samples;j++)
  1616. for(i=0;i<channels;i++){
  1617. val=vorbis_ftoi(pcm[i][j]*32768.f);
  1618. if(val>32767)val=32767;
  1619. else if(val<-32768)val=-32768;
  1620. val+=off;
  1621. *buffer++=(val>>8);
  1622. *buffer++=(val&0xff);
  1623. }
  1624. vorbis_fpu_restore(fpu);
  1625. }else{
  1626. int val;
  1627. vorbis_fpu_setround(&fpu);
  1628. for(j=0;j<samples;j++)
  1629. for(i=0;i<channels;i++){
  1630. val=vorbis_ftoi(pcm[i][j]*32768.f);
  1631. if(val>32767)val=32767;
  1632. else if(val<-32768)val=-32768;
  1633. val+=off;
  1634. *buffer++=(val&0xff);
  1635. *buffer++=(val>>8);
  1636. }
  1637. vorbis_fpu_restore(fpu);
  1638. }
  1639. }
  1640. }
  1641. vorbis_synthesis_read(&vf->vd,samples);
  1642. vf->pcm_offset+=samples;
  1643. if(bitstream)*bitstream=vf->current_link;
  1644. return(samples*bytespersample);
  1645. }else{
  1646. return(samples);
  1647. }
  1648. }
  1649. long ov_read(OggVorbis_File *vf,char *buffer,int length,
  1650. int bigendianp,int word,int sgned,int *bitstream){
  1651. return ov_read_filter(vf, buffer, length, bigendianp, word, sgned, bitstream, NULL, NULL);
  1652. }
  1653. /* input values: pcm_channels) a float vector per channel of output
  1654. length) the sample length being read by the app
  1655. return values: <0) error/hole in data (OV_HOLE), partial open (OV_EINVAL)
  1656. 0) EOF
  1657. n) number of samples of PCM actually returned. The
  1658. below works on a packet-by-packet basis, so the
  1659. return length is not related to the 'length' passed
  1660. in, just guaranteed to fit.
  1661. *section) set to the logical bitstream number */
  1662. long ov_read_float(OggVorbis_File *vf,float ***pcm_channels,int length,
  1663. int *bitstream){
  1664. if(vf->ready_state<OPENED)return(OV_EINVAL);
  1665. while(1){
  1666. if(vf->ready_state==INITSET){
  1667. float **pcm;
  1668. long samples=vorbis_synthesis_pcmout(&vf->vd,&pcm);
  1669. if(samples){
  1670. if(pcm_channels)*pcm_channels=pcm;
  1671. if(samples>length)samples=length;
  1672. vorbis_synthesis_read(&vf->vd,samples);
  1673. vf->pcm_offset+=samples;
  1674. if(bitstream)*bitstream=vf->current_link;
  1675. return samples;
  1676. }
  1677. }
  1678. /* suck in another packet */
  1679. {
  1680. int ret=_fetch_and_process_packet(vf,NULL,1,1);
  1681. if(ret==OV_EOF)return(0);
  1682. if(ret<=0)return(ret);
  1683. }
  1684. }
  1685. }
  1686. extern float *vorbis_window(vorbis_dsp_state *v,int W);
  1687. extern void _analysis_output_always(char *base,int i,float *v,int n,int bark,int dB,
  1688. ogg_int64_t off);
  1689. static void _ov_splice(float **pcm,float **lappcm,
  1690. int n1, int n2,
  1691. int ch1, int ch2,
  1692. float *w1, float *w2){
  1693. int i,j;
  1694. float *w=w1;
  1695. int n=n1;
  1696. if(n1>n2){
  1697. n=n2;
  1698. w=w2;
  1699. }
  1700. /* splice */
  1701. for(j=0;j<ch1 && j<ch2;j++){
  1702. float *s=lappcm[j];
  1703. float *d=pcm[j];
  1704. for(i=0;i<n;i++){
  1705. float wd=w[i]*w[i];
  1706. float ws=1.-wd;
  1707. d[i]=d[i]*wd + s[i]*ws;
  1708. }
  1709. }
  1710. /* window from zero */
  1711. for(;j<ch2;j++){
  1712. float *d=pcm[j];
  1713. for(i=0;i<n;i++){
  1714. float wd=w[i]*w[i];
  1715. d[i]=d[i]*wd;
  1716. }
  1717. }
  1718. }
  1719. /* make sure vf is INITSET */
  1720. static int _ov_initset(OggVorbis_File *vf){
  1721. while(1){
  1722. if(vf->ready_state==INITSET)break;
  1723. /* suck in another packet */
  1724. {
  1725. int ret=_fetch_and_process_packet(vf,NULL,1,0);
  1726. if(ret<0 && ret!=OV_HOLE)return(ret);
  1727. }
  1728. }
  1729. return 0;
  1730. }
  1731. /* make sure vf is INITSET and that we have a primed buffer; if
  1732. we're crosslapping at a stream section boundary, this also makes
  1733. sure we're sanity checking against the right stream information */
  1734. static int _ov_initprime(OggVorbis_File *vf){
  1735. vorbis_dsp_state *vd=&vf->vd;
  1736. while(1){
  1737. if(vf->ready_state==INITSET)
  1738. if(vorbis_synthesis_pcmout(vd,NULL))break;
  1739. /* suck in another packet */
  1740. {
  1741. int ret=_fetch_and_process_packet(vf,NULL,1,0);
  1742. if(ret<0 && ret!=OV_HOLE)return(ret);
  1743. }
  1744. }
  1745. return 0;
  1746. }
  1747. /* grab enough data for lapping from vf; this may be in the form of
  1748. unreturned, already-decoded pcm, remaining PCM we will need to
  1749. decode, or synthetic postextrapolation from last packets. */
  1750. static void _ov_getlap(OggVorbis_File *vf,vorbis_info *vi,vorbis_dsp_state *vd,
  1751. float **lappcm,int lapsize){
  1752. int lapcount=0,i;
  1753. float **pcm;
  1754. /* try first to decode the lapping data */
  1755. while(lapcount<lapsize){
  1756. int samples=vorbis_synthesis_pcmout(vd,&pcm);
  1757. if(samples){
  1758. if(samples>lapsize-lapcount)samples=lapsize-lapcount;
  1759. for(i=0;i<vi->channels;i++)
  1760. memcpy(lappcm[i]+lapcount,pcm[i],sizeof(**pcm)*samples);
  1761. lapcount+=samples;
  1762. vorbis_synthesis_read(vd,samples);
  1763. }else{
  1764. /* suck in another packet */
  1765. int ret=_fetch_and_process_packet(vf,NULL,1,0); /* do *not* span */
  1766. if(ret==OV_EOF)break;
  1767. }
  1768. }
  1769. if(lapcount<lapsize){
  1770. /* failed to get lapping data from normal decode; pry it from the
  1771. postextrapolation buffering, or the second half of the MDCT
  1772. from the last packet */
  1773. int samples=vorbis_synthesis_lapout(&vf->vd,&pcm);
  1774. if(samples==0){
  1775. for(i=0;i<vi->channels;i++)
  1776. memset(lappcm[i]+lapcount,0,sizeof(**pcm)*lapsize-lapcount);
  1777. lapcount=lapsize;
  1778. }else{
  1779. if(samples>lapsize-lapcount)samples=lapsize-lapcount;
  1780. for(i=0;i<vi->channels;i++)
  1781. memcpy(lappcm[i]+lapcount,pcm[i],sizeof(**pcm)*samples);
  1782. lapcount+=samples;
  1783. }
  1784. }
  1785. }
  1786. /* this sets up crosslapping of a sample by using trailing data from
  1787. sample 1 and lapping it into the windowing buffer of sample 2 */
  1788. int ov_crosslap(OggVorbis_File *vf1, OggVorbis_File *vf2){
  1789. vorbis_info *vi1,*vi2;
  1790. float **lappcm;
  1791. float **pcm;
  1792. float *w1,*w2;
  1793. int n1,n2,i,ret,hs1,hs2;
  1794. if(vf1==vf2)return(0); /* degenerate case */
  1795. if(vf1->ready_state<OPENED)return(OV_EINVAL);
  1796. if(vf2->ready_state<OPENED)return(OV_EINVAL);
  1797. /* the relevant overlap buffers must be pre-checked and pre-primed
  1798. before looking at settings in the event that priming would cross
  1799. a bitstream boundary. So, do it now */
  1800. ret=_ov_initset(vf1);
  1801. if(ret)return(ret);
  1802. ret=_ov_initprime(vf2);
  1803. if(ret)return(ret);
  1804. vi1=ov_info(vf1,-1);
  1805. vi2=ov_info(vf2,-1);
  1806. hs1=ov_halfrate_p(vf1);
  1807. hs2=ov_halfrate_p(vf2);
  1808. lappcm=alloca(sizeof(*lappcm)*vi1->channels);
  1809. n1=vorbis_info_blocksize(vi1,0)>>(1+hs1);
  1810. n2=vorbis_info_blocksize(vi2,0)>>(1+hs2);
  1811. w1=vorbis_window(&vf1->vd,0);
  1812. w2=vorbis_window(&vf2->vd,0);
  1813. for(i=0;i<vi1->channels;i++)
  1814. lappcm[i]=alloca(sizeof(**lappcm)*n1);
  1815. _ov_getlap(vf1,vi1,&vf1->vd,lappcm,n1);
  1816. /* have a lapping buffer from vf1; now to splice it into the lapping
  1817. buffer of vf2 */
  1818. /* consolidate and expose the buffer. */
  1819. vorbis_synthesis_lapout(&vf2->vd,&pcm);
  1820. _analysis_output_always("pcmL",0,pcm[0],n1*2,0,0,0);
  1821. _analysis_output_always("pcmR",0,pcm[1],n1*2,0,0,0);
  1822. /* splice */
  1823. _ov_splice(pcm,lappcm,n1,n2,vi1->channels,vi2->channels,w1,w2);
  1824. /* done */
  1825. return(0);
  1826. }
  1827. static int _ov_64_seek_lap(OggVorbis_File *vf,ogg_int64_t pos,
  1828. int (*localseek)(OggVorbis_File *,ogg_int64_t)){
  1829. vorbis_info *vi;
  1830. float **lappcm;
  1831. float **pcm;
  1832. float *w1,*w2;
  1833. int n1,n2,ch1,ch2,hs;
  1834. int i,ret;
  1835. if(vf->ready_state<OPENED)return(OV_EINVAL);
  1836. ret=_ov_initset(vf);
  1837. if(ret)return(ret);
  1838. vi=ov_info(vf,-1);
  1839. hs=ov_halfrate_p(vf);
  1840. ch1=vi->channels;
  1841. n1=vorbis_info_blocksize(vi,0)>>(1+hs);
  1842. w1=vorbis_window(&vf->vd,0); /* window arrays from libvorbis are
  1843. persistent; even if the decode state
  1844. from this link gets dumped, this
  1845. window array continues to exist */
  1846. lappcm=alloca(sizeof(*lappcm)*ch1);
  1847. for(i=0;i<ch1;i++)
  1848. lappcm[i]=alloca(sizeof(**lappcm)*n1);
  1849. _ov_getlap(vf,vi,&vf->vd,lappcm,n1);
  1850. /* have lapping data; seek and prime the buffer */
  1851. ret=localseek(vf,pos);
  1852. if(ret)return ret;
  1853. ret=_ov_initprime(vf);
  1854. if(ret)return(ret);
  1855. /* Guard against cross-link changes; they're perfectly legal */
  1856. vi=ov_info(vf,-1);
  1857. ch2=vi->channels;
  1858. n2=vorbis_info_blocksize(vi,0)>>(1+hs);
  1859. w2=vorbis_window(&vf->vd,0);
  1860. /* consolidate and expose the buffer. */
  1861. vorbis_synthesis_lapout(&vf->vd,&pcm);
  1862. /* splice */
  1863. _ov_splice(pcm,lappcm,n1,n2,ch1,ch2,w1,w2);
  1864. /* done */
  1865. return(0);
  1866. }
  1867. int ov_raw_seek_lap(OggVorbis_File *vf,ogg_int64_t pos){
  1868. return _ov_64_seek_lap(vf,pos,ov_raw_seek);
  1869. }
  1870. int ov_pcm_seek_lap(OggVorbis_File *vf,ogg_int64_t pos){
  1871. return _ov_64_seek_lap(vf,pos,ov_pcm_seek);
  1872. }
  1873. int ov_pcm_seek_page_lap(OggVorbis_File *vf,ogg_int64_t pos){
  1874. return _ov_64_seek_lap(vf,pos,ov_pcm_seek_page);
  1875. }
  1876. static int _ov_d_seek_lap(OggVorbis_File *vf,double pos,
  1877. int (*localseek)(OggVorbis_File *,double)){
  1878. vorbis_info *vi;
  1879. float **lappcm;
  1880. float **pcm;
  1881. float *w1,*w2;
  1882. int n1,n2,ch1,ch2,hs;
  1883. int i,ret;
  1884. if(vf->ready_state<OPENED)return(OV_EINVAL);
  1885. ret=_ov_initset(vf);
  1886. if(ret)return(ret);
  1887. vi=ov_info(vf,-1);
  1888. hs=ov_halfrate_p(vf);
  1889. ch1=vi->channels;
  1890. n1=vorbis_info_blocksize(vi,0)>>(1+hs);
  1891. w1=vorbis_window(&vf->vd,0); /* window arrays from libvorbis are
  1892. persistent; even if the decode state
  1893. from this link gets dumped, this
  1894. window array continues to exist */
  1895. lappcm=alloca(sizeof(*lappcm)*ch1);
  1896. for(i=0;i<ch1;i++)
  1897. lappcm[i]=alloca(sizeof(**lappcm)*n1);
  1898. _ov_getlap(vf,vi,&vf->vd,lappcm,n1);
  1899. /* have lapping data; seek and prime the buffer */
  1900. ret=localseek(vf,pos);
  1901. if(ret)return ret;
  1902. ret=_ov_initprime(vf);
  1903. if(ret)return(ret);
  1904. /* Guard against cross-link changes; they're perfectly legal */
  1905. vi=ov_info(vf,-1);
  1906. ch2=vi->channels;
  1907. n2=vorbis_info_blocksize(vi,0)>>(1+hs);
  1908. w2=vorbis_window(&vf->vd,0);
  1909. /* consolidate and expose the buffer. */
  1910. vorbis_synthesis_lapout(&vf->vd,&pcm);
  1911. /* splice */
  1912. _ov_splice(pcm,lappcm,n1,n2,ch1,ch2,w1,w2);
  1913. /* done */
  1914. return(0);
  1915. }
  1916. int ov_time_seek_lap(OggVorbis_File *vf,double pos){
  1917. return _ov_d_seek_lap(vf,pos,ov_time_seek);
  1918. }
  1919. int ov_time_seek_page_lap(OggVorbis_File *vf,double pos){
  1920. return _ov_d_seek_lap(vf,pos,ov_time_seek_page);
  1921. }