vorbisfile.c 61 KB

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