vorbisfile.c 47 KB

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