vorbisfile.c 58 KB

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