vorbisfile.c 45 KB

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