- result=_get_next_page(vf,&og,end-vf->offset);
- if(result==OV_EREAD) goto seek_error;
- if(result<0){
- if(bisect<=begin+1)
- end=begin; /* found it */
- else{
- if(bisect==0) goto seek_error;
- bisect-=CHUNKSIZE;
- if(bisect<=begin)bisect=begin+1;
- result=_seek_helper(vf,bisect);
- if(result) goto seek_error;
- }
- }else{
- ogg_int64_t granulepos;
-
- if(ogg_page_serialno(&og)!=vf->serialnos[link])
- continue;
-
- granulepos=ogg_page_granulepos(&og);
- if(granulepos==-1)continue;
-
- if(granulepos<target){
- best=result; /* raw offset of packet with granulepos */
- begin=vf->offset; /* raw offset of next page */
- begintime=granulepos;
-
- if(target-begintime>44100)break;
- bisect=begin; /* *not* begin + 1 */
- }else{
- if(bisect<=begin+1)
- end=begin; /* found it */
- else{
- if(end==vf->offset){ /* we're pretty close - we'd be stuck in */
- end=result;
- bisect-=CHUNKSIZE; /* an endless loop otherwise. */
- if(bisect<=begin)bisect=begin+1;
- result=_seek_helper(vf,bisect);
- if(result) goto seek_error;
- }else{
- end=bisect;
- endtime=granulepos;
- break;
- }
- }
- }
- }
+ result=_get_next_page(vf,&og,end-vf->offset);
+ if(result==OV_EREAD) goto seek_error;
+ if(result<0){
+ /* there is no next page! */
+ if(bisect<=begin+1)
+ /* No bisection left to perform. We've either found the
+ best candidate already or failed. Exit loop. */
+ end=begin;
+ else{
+ /* We tried to load a fraction of the last page; back up a
+ bit and try to get the whole last page */
+ if(bisect==0) goto seek_error;
+ bisect-=CHUNKSIZE;
+
+ /* don't repeat/loop on a read we've already performed */
+ if(bisect<=begin)bisect=begin+1;
+
+ /* seek and cntinue bisection */
+ result=_seek_helper(vf,bisect);
+ if(result) goto seek_error;
+ }
+ }else{
+ ogg_int64_t granulepos;
+ got_page=1;
+
+ /* got a page. analyze it */
+ /* only consider pages from primary vorbis stream */
+ if(ogg_page_serialno(&og)!=vf->serialnos[link])
+ continue;
+
+ /* only consider pages with the granulepos set */
+ granulepos=ogg_page_granulepos(&og);
+ if(granulepos==-1)continue;
+
+ if(granulepos<target){
+ /* this page is a successful candidate! Set state */
+
+ best=result; /* raw offset of packet with granulepos */
+ begin=vf->offset; /* raw offset of next page */
+ begintime=granulepos;
+
+ /* if we're before our target but within a short distance,
+ don't bisect; read forward */
+ if(target-begintime>44100)break;
+
+ bisect=begin; /* *not* begin + 1 as above */
+ }else{
+
+ /* This is one of our pages, but the granpos is
+ post-target; it is not a bisection return
+ candidate. (The only way we'd use it is if it's the
+ first page in the stream; we handle that case later
+ outside the bisection) */
+ if(bisect<=begin+1){
+ /* No bisection left to perform. We've either found the
+ best candidate already or failed. Exit loop. */
+ end=begin;
+ }else{
+ if(end==vf->offset){
+ /* bisection read to the end; use the known page
+ boundary (result) to update bisection, back up a
+ little bit, and try again */
+ end=result;
+ bisect-=CHUNKSIZE;
+ if(bisect<=begin)bisect=begin+1;
+ result=_seek_helper(vf,bisect);
+ if(result) goto seek_error;
+ }else{
+ /* Normal bisection */
+ end=bisect;
+ endtime=granulepos;
+ break;
+ }
+ }
+ }
+ }