vorbisfile.c 46 KB

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