Try out a new seek bisect optimization in vorbisfile.
[platform/upstream/libvorbis.git] / lib / vorbisfile.c
index 7c7edd3..a3569c9 100644 (file)
@@ -22,6 +22,9 @@
 #include <math.h>
 
 #include "vorbis/codec.h"
+
+/* we don't need or want the static callback symbols here */
+#define OV_EXCLUDE_STATIC_CALLBACKS
 #include "vorbis/vorbisfile.h"
 
 #include "os.h"
 
 /* read a little more data from the file/pipe into the ogg_sync framer
 */
-#define CHUNKSIZE 65536
+#define CHUNKSIZE 65536 /* greater-than-page-size granularity seeking */
+#define READSIZE 2048 /* a smaller read size is needed for low-rate streaming. */
 
 static long _get_data(OggVorbis_File *vf){
   errno=0;
   if(!(vf->callbacks.read_func))return(-1);
   if(vf->datasource){
-    char *buffer=ogg_sync_buffer(&vf->oy,CHUNKSIZE);
-    long bytes=(vf->callbacks.read_func)(buffer,1,CHUNKSIZE,vf->datasource);
+    char *buffer=ogg_sync_buffer(&vf->oy,READSIZE);
+    long bytes=(vf->callbacks.read_func)(buffer,1,READSIZE,vf->datasource);
     if(bytes>0)ogg_sync_wrote(&vf->oy,bytes);
     if(bytes==0 && errno)return(-1);
     return(bytes);
@@ -226,8 +230,8 @@ static ogg_int64_t _get_prev_page_serial(OggVorbis_File *vf,
 
   ogg_int64_t prefoffset=-1;
   ogg_int64_t offset=-1;
-  ogg_int64_t ret_serialno;
-  ogg_int64_t ret_gran;
+  ogg_int64_t ret_serialno=-1;
+  ogg_int64_t ret_gran=-1;
 
   while(offset==-1){
     begin-=CHUNKSIZE;
@@ -1423,13 +1427,14 @@ int ov_pcm_seek_page(OggVorbis_File *vf,ogg_int64_t pos){
         bisect=begin +
           (ogg_int64_t)((double)(target-begintime)*(end-begin)/(endtime-begintime))
           - CHUNKSIZE;
-        if(bisect<=begin)
-          bisect=begin+1;
+        if(bisect>begin+CHUNKSIZE){
+          result=_seek_helper(vf,bisect);
+          if(result) goto seek_error;
+        }else{
+          bisect=begin;
+        }
       }
 
-      result=_seek_helper(vf,bisect);
-      if(result) goto seek_error;
-
       while(begin<end){
         result=_get_next_page(vf,&og,end-vf->offset);
         if(result==OV_EREAD) goto seek_error;