external libs (libvorbisidec-1.2.1): updates from xiph.org tremor.git
authorOzkan Sezer <sezeroz@gmail.com>
Mon, 07 Oct 2019 01:04:02 +0300
changeset 9584641084da786
parent 957 fa94604ebea3
child 959 e1919e398c29
external libs (libvorbisidec-1.2.1): updates from xiph.org tremor.git
external/libvorbisidec-1.2.1/0000-tremor.git-2e87629f.patch
external/libvorbisidec-1.2.1/0000-tremor.git-49c8a74e.patch
external/libvorbisidec-1.2.1/0000-tremor.git-562307a4.patch
external/libvorbisidec-1.2.1/0000-tremor.git-7c30a663.patch
external/libvorbisidec-1.2.1/0000-tremor.git-b56ffce0.patch
external/libvorbisidec-1.2.1/codebook.c
external/libvorbisidec-1.2.1/info.c
external/libvorbisidec-1.2.1/iseeking_example.c
external/libvorbisidec-1.2.1/ivorbisfile.h
external/libvorbisidec-1.2.1/mdct.c
external/libvorbisidec-1.2.1/synthesis.c
external/libvorbisidec-1.2.1/vorbisfile.c
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/external/libvorbisidec-1.2.1/0000-tremor.git-2e87629f.patch	Mon Oct 07 01:04:02 2019 +0300
     1.3 @@ -0,0 +1,102 @@
     1.4 +From: Monty <xiphmont@xiph.org>
     1.5 +Date: Tue, 24 Jun 2014 14:44:34 +0000 (+0000)
     1.6 +Subject: Commit suggested fix for #2021 Backport additional #1486 -n -r 19164 set keyword... 
     1.7 +X-Git-Url: http://git.xiph.org/?p=tremor.git;a=commitdiff_plain;h=2e87629fb56cb23f969fb5862e721fc1a53f9725
     1.8 +
     1.9 +Commit suggested fix for #2021 Backport additional #1486 -n -r 19164 set keyword property on source files
    1.10 +
    1.11 +git-svn-id: https://svn.xiph.org/trunk/Tremor@19172 0101bb08-14d6-0310-b084-bc0e0c8e3800
    1.12 +---
    1.13 +
    1.14 +diff --git a/iseeking_example.c b/iseeking_example.c
    1.15 +index e3bf883..dc971ba 100644
    1.16 +--- a/iseeking_example.c
    1.17 ++++ b/iseeking_example.c
    1.18 +@@ -12,7 +12,7 @@
    1.19 +  ********************************************************************
    1.20 + 
    1.21 +  function: illustrate seeking, and test it too
    1.22 +- last mod: $Id: iseeking_example.c 16037 2009-05-26 21:10:58Z xiphmont $
    1.23 ++ last mod: $Id$
    1.24 + 
    1.25 +  ********************************************************************/
    1.26 + 
    1.27 +diff --git a/mdct.c b/mdct.c
    1.28 +index 4f39e7d..2aed62c 100644
    1.29 +--- a/mdct.c
    1.30 ++++ b/mdct.c
    1.31 +@@ -13,7 +13,7 @@
    1.32 + 
    1.33 +  function: normalized modified discrete cosine transform
    1.34 +            power of two length transform only [64 <= n ]
    1.35 +- last mod: $Id: mdct.c,v 1.9 2002/10/16 09:17:39 xiphmont Exp $
    1.36 ++ last mod: $Id$
    1.37 + 
    1.38 +  Original algorithm adapted long ago from _The use of multirate filter
    1.39 +  banks for coding of high quality digital audio_, by T. Sporer,
    1.40 +diff --git a/synthesis.c b/synthesis.c
    1.41 +index 38c3eb8..fe63924 100644
    1.42 +--- a/synthesis.c
    1.43 ++++ b/synthesis.c
    1.44 +@@ -67,7 +67,7 @@ static int _vorbis_synthesis1(vorbis_block *vb,ogg_packet *op,int decodep){
    1.45 +   
    1.46 +   /* more setup */
    1.47 +   vb->granulepos=op->granulepos;
    1.48 +-  vb->sequence=op->packetno-3; /* first block is third packet */
    1.49 ++  vb->sequence=op->packetno; /* first block is third packet */
    1.50 +   vb->eofflag=op->e_o_s;
    1.51 + 
    1.52 +   if(decodep){
    1.53 +diff --git a/vorbisfile.c b/vorbisfile.c
    1.54 +index 9413289..cd4814d 100644
    1.55 +--- a/vorbisfile.c
    1.56 ++++ b/vorbisfile.c
    1.57 +@@ -12,7 +12,7 @@
    1.58 +  ********************************************************************
    1.59 + 
    1.60 +  function: stdio-based convenience library for opening/seeking/decoding
    1.61 +- last mod: $Id: vorbisfile.c,v 1.6 2003/03/30 23:40:56 xiphmont Exp $
    1.62 ++ last mod: $Id$
    1.63 + 
    1.64 +  ********************************************************************/
    1.65 + 
    1.66 +@@ -1421,8 +1421,21 @@ int ov_pcm_seek_page(OggVorbis_File *vf,ogg_int64_t pos){
    1.67 +     ogg_int64_t endtime = vf->pcmlengths[link*2+1]+begintime;
    1.68 +     ogg_int64_t target=pos-total+begintime;
    1.69 +     ogg_int64_t best=-1;
    1.70 ++    int         got_page=0;
    1.71 + 
    1.72 +     ogg_page og;
    1.73 ++
    1.74 ++    /* if we have only one page, there will be no bisection.  Grab the page here */
    1.75 ++    if(begin==end){
    1.76 ++      result=_seek_helper(vf,begin);
    1.77 ++      if(result) goto seek_error;
    1.78 ++
    1.79 ++      result=_get_next_page(vf,&og,1);
    1.80 ++      if(result<0) goto seek_error;
    1.81 ++
    1.82 ++      got_page=1;
    1.83 ++    }
    1.84 ++
    1.85 +     /* bisection loop */
    1.86 +     while(begin<end){
    1.87 +       ogg_int64_t bisect;
    1.88 +@@ -1466,6 +1479,7 @@ int ov_pcm_seek_page(OggVorbis_File *vf,ogg_int64_t pos){
    1.89 +           }
    1.90 +         }else{
    1.91 +           ogg_int64_t granulepos;
    1.92 ++          got_page=1;
    1.93 + 
    1.94 +           /* got a page. analyze it */
    1.95 +           /* only consider pages from primary vorbis stream */
    1.96 +@@ -1528,7 +1542,8 @@ int ov_pcm_seek_page(OggVorbis_File *vf,ogg_int64_t pos){
    1.97 +          bisection would 'fail' because our search target was before the
    1.98 +          first PCM granule position fencepost. */
    1.99 + 
   1.100 +-      if(begin == vf->dataoffsets[link] &&
   1.101 ++      if(got_page &&
   1.102 ++         begin == vf->dataoffsets[link] &&
   1.103 +          ogg_page_serialno(&og)==vf->serialnos[link]){
   1.104 + 
   1.105 +         /* Yes, this is the beginning-of-stream case. We already have
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/external/libvorbisidec-1.2.1/0000-tremor.git-49c8a74e.patch	Mon Oct 07 01:04:02 2019 +0300
     2.3 @@ -0,0 +1,457 @@
     2.4 +From: Monty <xiphmont@xiph.org>
     2.5 +Date: Tue, 24 Jun 2014 12:12:08 +0000 (+0000)
     2.6 +Subject: Backport #1486 fixes from libvorbis to Tremor (trunk)
     2.7 +X-Git-Url: http://git.xiph.org/?p=tremor.git;a=commitdiff_plain;h=49c8a74e51a9441e2a8acea65fec7689c807507e
     2.8 +
     2.9 +Backport #1486 fixes from libvorbis to Tremor (trunk)
    2.10 +
    2.11 +git-svn-id: https://svn.xiph.org/trunk/Tremor@19170 0101bb08-14d6-0310-b084-bc0e0c8e3800
    2.12 +---
    2.13 +
    2.14 +diff --git a/iseeking_example.c b/iseeking_example.c
    2.15 +index de534eb..e3bf883 100644
    2.16 +--- a/iseeking_example.c
    2.17 ++++ b/iseeking_example.c
    2.18 +@@ -160,7 +160,7 @@ int main(){
    2.19 +              (long)pcmlength);
    2.20 +     
    2.21 +       for(i=0;i<1000;i++){
    2.22 +-        ogg_int64_t val=(double)rand()*pcmlength/RAND_MAX;
    2.23 ++        ogg_int64_t val=i==0?0:(double)rand()*pcmlength/RAND_MAX;
    2.24 +         fprintf(stderr,"\r\t%d [pcm position %ld]...     ",i,(long)val);
    2.25 +         ret=ov_pcm_seek_page(&ov,val);
    2.26 +         if(ret<0){
    2.27 +@@ -179,7 +179,7 @@ int main(){
    2.28 +              (long)pcmlength);
    2.29 +     
    2.30 +       for(i=0;i<1000;i++){
    2.31 +-        ogg_int64_t val=(double)rand()*pcmlength/RAND_MAX;
    2.32 ++        ogg_int64_t val=i==0?0:(double)rand()*pcmlength/RAND_MAX;
    2.33 +         fprintf(stderr,"\r\t%d [pcm position %ld]...     ",i,(long)val);
    2.34 +         ret=ov_pcm_seek(&ov,val);
    2.35 +         if(ret<0){
    2.36 +diff --git a/ivorbisfile.h b/ivorbisfile.h
    2.37 +index fda6205..f6ecb0e 100644
    2.38 +--- a/ivorbisfile.h
    2.39 ++++ b/ivorbisfile.h
    2.40 +@@ -26,7 +26,8 @@ extern "C"
    2.41 + #include <stdio.h>
    2.42 + #include "ivorbiscodec.h"
    2.43 + 
    2.44 +-#define CHUNKSIZE 1024
    2.45 ++#define CHUNKSIZE 65535
    2.46 ++#define READSIZE  1024
    2.47 + /* The function prototypes for the callbacks are basically the same as for
    2.48 +  * the stdio functions fread, fseek, fclose, ftell. 
    2.49 +  * The one difference is that the FILE * arguments have been replaced with
    2.50 +diff --git a/vorbisfile.c b/vorbisfile.c
    2.51 +index ac0eb88..9413289 100644
    2.52 +--- a/vorbisfile.c
    2.53 ++++ b/vorbisfile.c
    2.54 +@@ -6,7 +6,7 @@
    2.55 +  * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
    2.56 +  * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.       *
    2.57 +  *                                                                  *
    2.58 +- * THE OggVorbis 'TREMOR' SOURCE CODE IS (C) COPYRIGHT 1994-2009    *
    2.59 ++ * THE OggVorbis 'TREMOR' SOURCE CODE IS (C) COPYRIGHT 1994-2014    *
    2.60 +  * BY THE Xiph.Org FOUNDATION http://www.xiph.org/                  *
    2.61 +  *                                                                  *
    2.62 +  ********************************************************************
    2.63 +@@ -63,8 +63,8 @@ static long _get_data(OggVorbis_File *vf){
    2.64 +   errno=0;
    2.65 +   if(!(vf->callbacks.read_func))return(-1);
    2.66 +   if(vf->datasource){
    2.67 +-    char *buffer=ogg_sync_buffer(&vf->oy,CHUNKSIZE);
    2.68 +-    long bytes=(vf->callbacks.read_func)(buffer,1,CHUNKSIZE,vf->datasource);
    2.69 ++    char *buffer=ogg_sync_buffer(&vf->oy,READSIZE);
    2.70 ++    long bytes=(vf->callbacks.read_func)(buffer,1,READSIZE,vf->datasource);
    2.71 +     if(bytes>0)ogg_sync_wrote(&vf->oy,bytes);
    2.72 +     if(bytes==0 && errno)return(-1);
    2.73 +     return(bytes);
    2.74 +@@ -75,11 +75,14 @@ static long _get_data(OggVorbis_File *vf){
    2.75 + /* save a tiny smidge of verbosity to make the code more readable */
    2.76 + static int _seek_helper(OggVorbis_File *vf,ogg_int64_t offset){
    2.77 +   if(vf->datasource){
    2.78 +-    if(!(vf->callbacks.seek_func)||
    2.79 +-       (vf->callbacks.seek_func)(vf->datasource, offset, SEEK_SET) == -1)
    2.80 +-      return OV_EREAD;
    2.81 +-    vf->offset=offset;
    2.82 +-    ogg_sync_reset(&vf->oy);
    2.83 ++    /* only seek if the file position isn't already there */
    2.84 ++    if(vf->offset != offset){
    2.85 ++      if(!(vf->callbacks.seek_func)||
    2.86 ++         (vf->callbacks.seek_func)(vf->datasource, offset, SEEK_SET) == -1)
    2.87 ++        return OV_EREAD;
    2.88 ++      vf->offset=offset;
    2.89 ++      ogg_sync_reset(&vf->oy);
    2.90 ++    }
    2.91 +   }else{
    2.92 +     /* shouldn't happen unless someone writes a broken callback */
    2.93 +     return OV_EFAULT;
    2.94 +@@ -133,14 +136,12 @@ static ogg_int64_t _get_next_page(OggVorbis_File *vf,ogg_page *og,
    2.95 +   }
    2.96 + }
    2.97 + 
    2.98 +-/* find the latest page beginning before the current stream cursor
    2.99 +-   position. Much dirtier than the above as Ogg doesn't have any
   2.100 +-   backward search linkage.  no 'readp' as it will certainly have to
   2.101 +-   read. */
   2.102 ++/* find the latest page beginning before the passed in position. Much
   2.103 ++   dirtier than the above as Ogg doesn't have any backward search
   2.104 ++   linkage.  no 'readp' as it will certainly have to read. */
   2.105 + /* returns offset or OV_EREAD, OV_FAULT */
   2.106 +-static ogg_int64_t _get_prev_page(OggVorbis_File *vf,ogg_page *og){
   2.107 +-  ogg_int64_t begin=vf->offset;
   2.108 +-  ogg_int64_t end=begin;
   2.109 ++static ogg_int64_t _get_prev_page(OggVorbis_File *vf,ogg_int64_t begin,ogg_page *og){
   2.110 ++  ogg_int64_t end = begin;
   2.111 +   ogg_int64_t ret;
   2.112 +   ogg_int64_t offset=-1;
   2.113 + 
   2.114 +@@ -215,11 +216,10 @@ static int _lookup_page_serialno(ogg_page *og, ogg_uint32_t *serialno_list, int
   2.115 +    info of last page of the matching serial number instead of the very
   2.116 +    last page.  If no page of the specified serialno is seen, it will
   2.117 +    return the info of last page and alter *serialno.  */
   2.118 +-static ogg_int64_t _get_prev_page_serial(OggVorbis_File *vf,
   2.119 ++static ogg_int64_t _get_prev_page_serial(OggVorbis_File *vf, ogg_int64_t begin,
   2.120 +                                          ogg_uint32_t *serial_list, int serial_n,
   2.121 +                                          int *serialno, ogg_int64_t *granpos){
   2.122 +   ogg_page og;
   2.123 +-  ogg_int64_t begin=vf->offset;
   2.124 +   ogg_int64_t end=begin;
   2.125 +   ogg_int64_t ret;
   2.126 + 
   2.127 +@@ -488,10 +488,10 @@ static int _bisect_forward_serialno(OggVorbis_File *vf,
   2.128 +        down to (or just started with) a single link.  Now we need to
   2.129 +        find the last vorbis page belonging to the first vorbis stream
   2.130 +        for this link. */
   2.131 +-
   2.132 ++    searched = end;
   2.133 +     while(endserial != serialno){
   2.134 +       endserial = serialno;
   2.135 +-      vf->offset=_get_prev_page_serial(vf,currentno_list,currentnos,&endserial,&endgran);
   2.136 ++      searched=_get_prev_page_serial(vf,searched,currentno_list,currentnos,&endserial,&endgran);
   2.137 +     }
   2.138 + 
   2.139 +     vf->links=m+1;
   2.140 +@@ -508,14 +508,19 @@ static int _bisect_forward_serialno(OggVorbis_File *vf,
   2.141 + 
   2.142 +     vf->offsets[m+1]=end;
   2.143 +     vf->offsets[m]=begin;
   2.144 +-    vf->pcmlengths[m*2+1]=endgran;
   2.145 ++    vf->pcmlengths[m*2+1]=(endgran<0?0:endgran);
   2.146 + 
   2.147 +   }else{
   2.148 + 
   2.149 ++    /* last page is not in the starting stream's serial number list,
   2.150 ++       so we have multiple links.  Find where the stream that begins
   2.151 ++       our bisection ends. */
   2.152 ++
   2.153 +     ogg_uint32_t *next_serialno_list=NULL;
   2.154 +     int next_serialnos=0;
   2.155 +     vorbis_info vi;
   2.156 +     vorbis_comment vc;
   2.157 ++    int testserial = serialno+1;
   2.158 + 
   2.159 +     /* the below guards against garbage seperating the last and
   2.160 +        first pages of two links. */
   2.161 +@@ -528,10 +533,8 @@ static int _bisect_forward_serialno(OggVorbis_File *vf,
   2.162 +         bisect=(searched+endsearched)/2;
   2.163 +       }
   2.164 + 
   2.165 +-      if(bisect != vf->offset){
   2.166 +-        ret=_seek_helper(vf,bisect);
   2.167 +-        if(ret)return(ret);
   2.168 +-      }
   2.169 ++      ret=_seek_helper(vf,bisect);
   2.170 ++      if(ret)return(ret);
   2.171 + 
   2.172 +       last=_get_next_page(vf,&og,-1);
   2.173 +       if(last==OV_EREAD)return(OV_EREAD);
   2.174 +@@ -544,28 +547,22 @@ static int _bisect_forward_serialno(OggVorbis_File *vf,
   2.175 +     }
   2.176 + 
   2.177 +     /* Bisection point found */
   2.178 +-
   2.179 +     /* for the time being, fetch end PCM offset the simple way */
   2.180 +-    {
   2.181 +-      int testserial = serialno+1;
   2.182 +-      vf->offset = next;
   2.183 +-      while(testserial != serialno){
   2.184 +-        testserial = serialno;
   2.185 +-        vf->offset=_get_prev_page_serial(vf,currentno_list,currentnos,&testserial,&searchgran);
   2.186 +-      }
   2.187 ++    searched = next;
   2.188 ++    while(testserial != serialno){
   2.189 ++      testserial = serialno;
   2.190 ++      searched = _get_prev_page_serial(vf,searched,currentno_list,currentnos,&testserial,&searchgran);
   2.191 +     }
   2.192 + 
   2.193 +-    if(vf->offset!=next){
   2.194 +-      ret=_seek_helper(vf,next);
   2.195 +-      if(ret)return(ret);
   2.196 +-    }
   2.197 ++    ret=_seek_helper(vf,next);
   2.198 ++    if(ret)return(ret);
   2.199 + 
   2.200 +     ret=_fetch_headers(vf,&vi,&vc,&next_serialno_list,&next_serialnos,NULL);
   2.201 +     if(ret)return(ret);
   2.202 +     serialno = vf->os.serialno;
   2.203 +     dataoffset = vf->offset;
   2.204 + 
   2.205 +-    /* this will consume a page, however the next bistection always
   2.206 ++    /* this will consume a page, however the next bisection always
   2.207 +        starts with a raw seek */
   2.208 +     pcmoffset = _initial_pcmoffset(vf,&vi);
   2.209 + 
   2.210 +@@ -585,6 +582,7 @@ static int _bisect_forward_serialno(OggVorbis_File *vf,
   2.211 +     vf->pcmlengths[m*2+1]=searchgran;
   2.212 +     vf->pcmlengths[m*2+2]=pcmoffset;
   2.213 +     vf->pcmlengths[m*2+3]-=pcmoffset;
   2.214 ++    if(vf->pcmlengths[m*2+3]<0)vf->pcmlengths[m*2+3]=0;
   2.215 + 
   2.216 +   }
   2.217 +   return(0);
   2.218 +@@ -632,11 +630,11 @@ static int _open_seekable2(OggVorbis_File *vf){
   2.219 +   /* Get the offset of the last page of the physical bitstream, or, if
   2.220 +      we're lucky the last vorbis page of this link as most OggVorbis
   2.221 +      files will contain a single logical bitstream */
   2.222 +-  end=_get_prev_page_serial(vf,vf->serialnos+2,vf->serialnos[1],&endserial,&endgran);
   2.223 ++  end=_get_prev_page_serial(vf,vf->end,vf->serialnos+2,vf->serialnos[1],&endserial,&endgran);
   2.224 +   if(end<0)return(end);
   2.225 + 
   2.226 +   /* now determine bitstream structure recursively */
   2.227 +-  if(_bisect_forward_serialno(vf,0,dataoffset,vf->offset,endgran,endserial,
   2.228 ++  if(_bisect_forward_serialno(vf,0,dataoffset,end,endgran,endserial,
   2.229 +                               vf->serialnos+2,vf->serialnos[1],0)<0)return(OV_EREAD);
   2.230 + 
   2.231 +   vf->offsets[0]=0;
   2.232 +@@ -644,6 +642,7 @@ static int _open_seekable2(OggVorbis_File *vf){
   2.233 +   vf->dataoffsets[0]=dataoffset;
   2.234 +   vf->pcmlengths[0]=pcmoffset;
   2.235 +   vf->pcmlengths[1]-=pcmoffset;
   2.236 ++  if(vf->pcmlengths[1]<0)vf->pcmlengths[1]=0;
   2.237 + 
   2.238 +   return(ov_raw_seek(vf,dataoffset));
   2.239 + }
   2.240 +@@ -711,7 +710,7 @@ static int _fetch_and_process_packet(OggVorbis_File *vf,
   2.241 +               if(oldsamples)return(OV_EFAULT);
   2.242 + 
   2.243 +               vorbis_synthesis_blockin(&vf->vd,&vf->vb);
   2.244 +-              vf->samptrack+=vorbis_synthesis_pcmout(&vf->vd,NULL)-oldsamples;
   2.245 ++              vf->samptrack+=vorbis_synthesis_pcmout(&vf->vd,NULL);
   2.246 +               vf->bittrack+=op_ptr->bytes*8;
   2.247 +             }
   2.248 + 
   2.249 +@@ -1403,22 +1402,28 @@ int ov_pcm_seek_page(OggVorbis_File *vf,ogg_int64_t pos){
   2.250 +     if(pos>=total)break;
   2.251 +   }
   2.252 + 
   2.253 +-  /* search within the logical bitstream for the page with the highest
   2.254 +-     pcm_pos preceding (or equal to) pos.  There is a danger here;
   2.255 +-     missing pages or incorrect frame number information in the
   2.256 +-     bitstream could make our task impossible.  Account for that (it
   2.257 +-     would be an error condition) */
   2.258 ++  /* Search within the logical bitstream for the page with the highest
   2.259 ++     pcm_pos preceding pos.  If we're looking for a position on the
   2.260 ++     first page, bisection will halt without finding our position as
   2.261 ++     it's before the first explicit granulepos fencepost. That case is
   2.262 ++     handled separately below.
   2.263 ++
   2.264 ++     There is a danger here; missing pages or incorrect frame number
   2.265 ++     information in the bitstream could make our task impossible.
   2.266 ++     Account for that (it would be an error condition) */
   2.267 ++
   2.268 ++  /* new search algorithm originally by HB (Nicholas Vinen) */
   2.269 + 
   2.270 +-  /* new search algorithm by HB (Nicholas Vinen) */
   2.271 +   {
   2.272 +     ogg_int64_t end=vf->offsets[link+1];
   2.273 +-    ogg_int64_t begin=vf->offsets[link];
   2.274 ++    ogg_int64_t begin=vf->dataoffsets[link];
   2.275 +     ogg_int64_t begintime = vf->pcmlengths[link*2];
   2.276 +     ogg_int64_t endtime = vf->pcmlengths[link*2+1]+begintime;
   2.277 +     ogg_int64_t target=pos-total+begintime;
   2.278 +-    ogg_int64_t best=begin;
   2.279 ++    ogg_int64_t best=-1;
   2.280 + 
   2.281 +     ogg_page og;
   2.282 ++    /* bisection loop */
   2.283 +     while(begin<end){
   2.284 +       ogg_int64_t bisect;
   2.285 + 
   2.286 +@@ -1433,51 +1438,79 @@ int ov_pcm_seek_page(OggVorbis_File *vf,ogg_int64_t pos){
   2.287 +           bisect=begin;
   2.288 +       }
   2.289 + 
   2.290 +-      if(bisect!=vf->offset){
   2.291 +-        result=_seek_helper(vf,bisect);
   2.292 +-        if(result) goto seek_error;
   2.293 +-      }
   2.294 ++      result=_seek_helper(vf,bisect);
   2.295 ++      if(result) goto seek_error;
   2.296 + 
   2.297 ++      /* read loop within the bisection loop */
   2.298 +       while(begin<end){
   2.299 +         result=_get_next_page(vf,&og,end-vf->offset);
   2.300 +         if(result==OV_EREAD) goto seek_error;
   2.301 +         if(result<0){
   2.302 ++          /* there is no next page! */
   2.303 +           if(bisect<=begin+1)
   2.304 +-            end=begin; /* found it */
   2.305 ++            /* No bisection left to perform.  We've either found the
   2.306 ++               best candidate already or failed. Exit loop. */
   2.307 ++            end=begin;
   2.308 +           else{
   2.309 ++            /* We tried to load a fraction of the last page; back up a
   2.310 ++               bit and try to get the whole last page */
   2.311 +             if(bisect==0) goto seek_error;
   2.312 +             bisect-=CHUNKSIZE;
   2.313 ++
   2.314 ++            /* don't repeat/loop on a read we've already performed */
   2.315 +             if(bisect<=begin)bisect=begin+1;
   2.316 ++
   2.317 ++            /* seek and continue bisection */
   2.318 +             result=_seek_helper(vf,bisect);
   2.319 +             if(result) goto seek_error;
   2.320 +           }
   2.321 +         }else{
   2.322 +           ogg_int64_t granulepos;
   2.323 + 
   2.324 ++          /* got a page. analyze it */
   2.325 ++          /* only consider pages from primary vorbis stream */
   2.326 +           if(ogg_page_serialno(&og)!=vf->serialnos[link])
   2.327 +             continue;
   2.328 + 
   2.329 ++          /* only consider pages with the granulepos set */
   2.330 +           granulepos=ogg_page_granulepos(&og);
   2.331 +           if(granulepos==-1)continue;
   2.332 + 
   2.333 +           if(granulepos<target){
   2.334 ++            /* this page is a successful candidate! Set state */
   2.335 ++
   2.336 +             best=result;  /* raw offset of packet with granulepos */
   2.337 +             begin=vf->offset; /* raw offset of next page */
   2.338 +             begintime=granulepos;
   2.339 + 
   2.340 ++            /* if we're before our target but within a short distance,
   2.341 ++               don't bisect; read forward */
   2.342 +             if(target-begintime>44100)break;
   2.343 +-            bisect=begin; /* *not* begin + 1 */
   2.344 ++
   2.345 ++            bisect=begin; /* *not* begin + 1 as above */
   2.346 +           }else{
   2.347 +-            if(bisect<=begin+1)
   2.348 +-              end=begin;  /* found it */
   2.349 +-            else{
   2.350 +-              if(end==vf->offset){ /* we're pretty close - we'd be stuck in */
   2.351 ++
   2.352 ++            /* This is one of our pages, but the granpos is
   2.353 ++               post-target; it is not a bisection return
   2.354 ++               candidate. (The only way we'd use it is if it's the
   2.355 ++               first page in the stream; we handle that case later
   2.356 ++               outside the bisection) */
   2.357 ++            if(bisect<=begin+1){
   2.358 ++              /* No bisection left to perform.  We've either found the
   2.359 ++                 best candidate already or failed. Exit loop. */
   2.360 ++              end=begin;
   2.361 ++            }else{
   2.362 ++              if(end==vf->offset){
   2.363 ++                /* bisection read to the end; use the known page
   2.364 ++                   boundary (result) to update bisection, back up a
   2.365 ++                   little bit, and try again */
   2.366 +                 end=result;
   2.367 +-                bisect-=CHUNKSIZE; /* an endless loop otherwise. */
   2.368 ++                bisect-=CHUNKSIZE;
   2.369 +                 if(bisect<=begin)bisect=begin+1;
   2.370 +                 result=_seek_helper(vf,bisect);
   2.371 +                 if(result) goto seek_error;
   2.372 +               }else{
   2.373 ++                /* Normal bisection */
   2.374 +                 end=bisect;
   2.375 +                 endtime=granulepos;
   2.376 +                 break;
   2.377 +@@ -1488,9 +1521,45 @@ int ov_pcm_seek_page(OggVorbis_File *vf,ogg_int64_t pos){
   2.378 +       }
   2.379 +     }
   2.380 + 
   2.381 +-    /* found our page. seek to it, update pcm offset. Easier case than
   2.382 +-       raw_seek, don't keep packets preceding granulepos. */
   2.383 +-    {
   2.384 ++    /* Out of bisection: did it 'fail?' */
   2.385 ++    if(best == -1){
   2.386 ++
   2.387 ++      /* Check the 'looking for data in first page' special case;
   2.388 ++         bisection would 'fail' because our search target was before the
   2.389 ++         first PCM granule position fencepost. */
   2.390 ++
   2.391 ++      if(begin == vf->dataoffsets[link] &&
   2.392 ++         ogg_page_serialno(&og)==vf->serialnos[link]){
   2.393 ++
   2.394 ++        /* Yes, this is the beginning-of-stream case. We already have
   2.395 ++           our page, right at the beginning of PCM data.  Set state
   2.396 ++           and return. */
   2.397 ++
   2.398 ++        vf->pcm_offset=total;
   2.399 ++
   2.400 ++        if(link!=vf->current_link){
   2.401 ++          /* Different link; dump entire decode machine */
   2.402 ++          _decode_clear(vf);
   2.403 ++
   2.404 ++          vf->current_link=link;
   2.405 ++          vf->current_serialno=vf->serialnos[link];
   2.406 ++          vf->ready_state=STREAMSET;
   2.407 ++
   2.408 ++        }else{
   2.409 ++          vorbis_synthesis_restart(&vf->vd);
   2.410 ++        }
   2.411 ++
   2.412 ++        ogg_stream_reset_serialno(&vf->os,vf->current_serialno);
   2.413 ++        ogg_stream_pagein(&vf->os,&og);
   2.414 ++
   2.415 ++      }else
   2.416 ++        goto seek_error;
   2.417 ++
   2.418 ++    }else{
   2.419 ++
   2.420 ++      /* Bisection found our page. seek to it, update pcm offset. Easier case than
   2.421 ++         raw_seek, don't keep packets preceding granulepos. */
   2.422 ++
   2.423 +       ogg_page og;
   2.424 +       ogg_packet op;
   2.425 + 
   2.426 +@@ -1520,23 +1589,23 @@ int ov_pcm_seek_page(OggVorbis_File *vf,ogg_int64_t pos){
   2.427 +       while(1){
   2.428 +         result=ogg_stream_packetpeek(&vf->os,&op);
   2.429 +         if(result==0){
   2.430 +-          /* !!! the packet finishing this page originated on a
   2.431 +-             preceding page. Keep fetching previous pages until we
   2.432 +-             get one with a granulepos or without the 'continued' flag
   2.433 +-             set.  Then just use raw_seek for simplicity. */
   2.434 +-
   2.435 +-          result=_seek_helper(vf,best);
   2.436 +-          if(result<0) goto seek_error;
   2.437 +-
   2.438 +-          while(1){
   2.439 +-            result=_get_prev_page(vf,&og);
   2.440 ++          /* No packet returned; we exited the bisection with 'best'
   2.441 ++             pointing to a page with a granule position, so the packet
   2.442 ++             finishing this page ('best') originated on a preceding
   2.443 ++             page. Keep fetching previous pages until we get one with
   2.444 ++             a granulepos or without the 'continued' flag set.  Then
   2.445 ++             just use raw_seek for simplicity. */
   2.446 ++          /* Do not rewind past the beginning of link data; if we do,
   2.447 ++             it's either a bug or a broken stream */
   2.448 ++          result=best;
   2.449 ++          while(result>vf->dataoffsets[link]){
   2.450 ++            result=_get_prev_page(vf,result,&og);
   2.451 +             if(result<0) goto seek_error;
   2.452 +             if(ogg_page_serialno(&og)==vf->current_serialno &&
   2.453 +                (ogg_page_granulepos(&og)>-1 ||
   2.454 +                 !ogg_page_continued(&og))){
   2.455 +               return ov_raw_seek(vf,result);
   2.456 +             }
   2.457 +-            vf->offset=result;
   2.458 +           }
   2.459 +         }
   2.460 +         if(result<0){
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/external/libvorbisidec-1.2.1/0000-tremor.git-562307a4.patch	Mon Oct 07 01:04:02 2019 +0300
     3.3 @@ -0,0 +1,85 @@
     3.4 +From: Thomas Daede <daede003@umn.edu>
     3.5 +Date: Fri, 16 Mar 2018 04:47:09 +0000 (-0700)
     3.6 +Subject: Prevent out-of-bounds write in codebook decoding.
     3.7 +X-Git-Url: http://git.xiph.org/?p=tremor.git;a=commitdiff_plain;h=562307a4a7082e24553f3d2c55dab397a17c4b4f
     3.8 +
     3.9 +Prevent out-of-bounds write in codebook decoding.
    3.10 +
    3.11 +Codebooks that are not an exact divisor of the partition size are now
    3.12 +truncated to fit within the partition.
    3.13 +---
    3.14 +
    3.15 +diff --git a/codebook.c b/codebook.c
    3.16 +index f8b7983..1e1ae8a 100644
    3.17 +--- a/codebook.c
    3.18 ++++ b/codebook.c
    3.19 +@@ -258,7 +258,7 @@ long vorbis_book_decodevs_add(codebook *book,ogg_int32_t *a,
    3.20 + 	t[i] = book->valuelist+entry[i]*book->dim;
    3.21 +       }
    3.22 +       for(i=0,o=0;i<book->dim;i++,o+=step)
    3.23 +-	for (j=0;j<step;j++)
    3.24 ++	for (j=0;o+j<n && j<step;j++)
    3.25 + 	  a[o+j]+=t[j][i]>>shift;
    3.26 +     }else{
    3.27 +       for (i = 0; i < step; i++) {
    3.28 +@@ -267,7 +267,7 @@ long vorbis_book_decodevs_add(codebook *book,ogg_int32_t *a,
    3.29 + 	t[i] = book->valuelist+entry[i]*book->dim;
    3.30 +       }
    3.31 +       for(i=0,o=0;i<book->dim;i++,o+=step)
    3.32 +-	for (j=0;j<step;j++)
    3.33 ++	for (j=0;o+j<n && j<step;j++)
    3.34 + 	  a[o+j]+=t[j][i]<<-shift;
    3.35 +     }
    3.36 +   }
    3.37 +@@ -287,7 +287,7 @@ long vorbis_book_decodev_add(codebook *book,ogg_int32_t *a,
    3.38 + 	entry = decode_packed_entry_number(book,b);
    3.39 + 	if(entry==-1)return(-1);
    3.40 + 	t     = book->valuelist+entry*book->dim;
    3.41 +-	for (j=0;j<book->dim;)
    3.42 ++	for (j=0;i<n && j<book->dim;)
    3.43 + 	  a[i++]+=t[j++]>>shift;
    3.44 +       }
    3.45 +     }else{
    3.46 +@@ -295,7 +295,7 @@ long vorbis_book_decodev_add(codebook *book,ogg_int32_t *a,
    3.47 + 	entry = decode_packed_entry_number(book,b);
    3.48 + 	if(entry==-1)return(-1);
    3.49 + 	t     = book->valuelist+entry*book->dim;
    3.50 +-	for (j=0;j<book->dim;)
    3.51 ++	for (j=0;i<n && j<book->dim;)
    3.52 + 	  a[i++]+=t[j++]<<-shift;
    3.53 +       }
    3.54 +     }
    3.55 +@@ -352,15 +352,15 @@ long vorbis_book_decodevv_add(codebook *book,ogg_int32_t **a,\
    3.56 +     long i,j,entry;
    3.57 +     int chptr=0;
    3.58 +     int shift=point-book->binarypoint;
    3.59 +-    
    3.60 ++    int m=offset+n;
    3.61 +     if(shift>=0){
    3.62 +       
    3.63 +-      for(i=offset;i<offset+n;){
    3.64 ++      for(i=offset;i<m;){
    3.65 + 	entry = decode_packed_entry_number(book,b);
    3.66 + 	if(entry==-1)return(-1);
    3.67 + 	{
    3.68 + 	  const ogg_int32_t *t = book->valuelist+entry*book->dim;
    3.69 +-	  for (j=0;j<book->dim;j++){
    3.70 ++	  for (j=0;i<m && j<book->dim;j++){
    3.71 + 	    a[chptr++][i]+=t[j]>>shift;
    3.72 + 	    if(chptr==ch){
    3.73 + 	      chptr=0;
    3.74 +@@ -371,12 +371,12 @@ long vorbis_book_decodevv_add(codebook *book,ogg_int32_t **a,\
    3.75 +       }
    3.76 +     }else{
    3.77 +       
    3.78 +-      for(i=offset;i<offset+n;){
    3.79 ++      for(i=offset;i<m;){
    3.80 + 	entry = decode_packed_entry_number(book,b);
    3.81 + 	if(entry==-1)return(-1);
    3.82 + 	{
    3.83 + 	  const ogg_int32_t *t = book->valuelist+entry*book->dim;
    3.84 +-	  for (j=0;j<book->dim;j++){
    3.85 ++	  for (j=0;i<m && j<book->dim;j++){
    3.86 + 	    a[chptr++][i]+=t[j]<<-shift;
    3.87 + 	    if(chptr==ch){
    3.88 + 	      chptr=0;
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/external/libvorbisidec-1.2.1/0000-tremor.git-7c30a663.patch	Mon Oct 07 01:04:02 2019 +0300
     4.3 @@ -0,0 +1,48 @@
     4.4 +From: Tim Terriberry <tterribe@xiph.org>
     4.5 +Date: Mon, 19 Mar 2018 20:08:21 +0000 (-0700)
     4.6 +Subject: Port r19426 from libvorbis.
     4.7 +X-Git-Url: http://git.xiph.org/?p=tremor.git;a=commitdiff_plain;h=7c30a66346199f3f09017a09567c6c8a3a0eedc8
     4.8 +
     4.9 +Port r19426 from libvorbis.
    4.10 +
    4.11 +Reject multiple headers of the same type.
    4.12 +---
    4.13 +
    4.14 +diff --git a/info.c b/info.c
    4.15 +index 3f3308e..24e24ac 100644
    4.16 +--- a/info.c
    4.17 ++++ b/info.c
    4.18 +@@ -227,7 +227,6 @@ static int _vorbis_unpack_comment(vorbis_comment *vc,oggpack_buffer *opb){
    4.19 + static int _vorbis_unpack_books(vorbis_info *vi,oggpack_buffer *opb){
    4.20 +   codec_setup_info     *ci=(codec_setup_info *)vi->codec_setup;
    4.21 +   int i;
    4.22 +-  if(!ci)return(OV_EFAULT);
    4.23 + 
    4.24 +   /* codebooks */
    4.25 +   ci->books=oggpack_read(opb,8)+1;
    4.26 +@@ -367,6 +366,10 @@ int vorbis_synthesis_headerin(vorbis_info *vi,vorbis_comment *vc,ogg_packet *op)
    4.27 + 	  /* um... we didn't get the initial header */
    4.28 + 	  return(OV_EBADHEADER);
    4.29 + 	}
    4.30 ++        if(vc->vendor!=NULL){
    4.31 ++          /* previously initialized comment header */
    4.32 ++          return(OV_EBADHEADER);
    4.33 ++        }
    4.34 + 
    4.35 + 	return(_vorbis_unpack_comment(vc,&opb));
    4.36 + 
    4.37 +@@ -375,6 +378,14 @@ int vorbis_synthesis_headerin(vorbis_info *vi,vorbis_comment *vc,ogg_packet *op)
    4.38 + 	  /* um... we didn;t get the initial header or comments yet */
    4.39 + 	  return(OV_EBADHEADER);
    4.40 + 	}
    4.41 ++        if(vi->codec_setup==NULL){
    4.42 ++          /* improperly initialized vorbis_info */
    4.43 ++          return(OV_EFAULT);
    4.44 ++        }
    4.45 ++        if(((codec_setup_info *)vi->codec_setup)->books>0){
    4.46 ++          /* previously initialized setup header */
    4.47 ++          return(OV_EBADHEADER);
    4.48 ++        }
    4.49 + 
    4.50 + 	return(_vorbis_unpack_books(vi,&opb));
    4.51 + 
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/external/libvorbisidec-1.2.1/0000-tremor.git-b56ffce0.patch	Mon Oct 07 01:04:02 2019 +0300
     5.3 @@ -0,0 +1,26 @@
     5.4 +From: Tim Terriberry <tterribe@xiph.org>
     5.5 +Date: Sun, 4 Jan 2015 19:11:49 +0000 (+0000)
     5.6 +Subject: Port r19419 to Tremor.
     5.7 +X-Git-Url: http://git.xiph.org/?p=tremor.git;a=commitdiff_plain;h=b56ffce0c0773ec5ca04c466bc00b1bbcaf65aef
     5.8 +
     5.9 +Port r19419 to Tremor.
    5.10 +
    5.11 +Fix crash on corrupt input file (invalid mode index)
    5.12 +
    5.13 +
    5.14 +git-svn-id: https://svn.xiph.org/trunk/Tremor@19420 0101bb08-14d6-0310-b084-bc0e0c8e3800
    5.15 +---
    5.16 +
    5.17 +diff --git a/synthesis.c b/synthesis.c
    5.18 +index fe63924..d22cb82 100644
    5.19 +--- a/synthesis.c
    5.20 ++++ b/synthesis.c
    5.21 +@@ -124,7 +124,7 @@ long vorbis_packet_blocksize(vorbis_info *vi,ogg_packet *op){
    5.22 +     /* read our mode and pre/post windowsize */
    5.23 +     mode=oggpack_read(&opb,modebits);
    5.24 +   }
    5.25 +-  if(mode==-1)return(OV_EBADPACKET);
    5.26 ++  if(mode==-1 || !ci->mode_param[mode])return(OV_EBADPACKET);
    5.27 +   return(ci->blocksizes[ci->mode_param[mode]->blockflag]);
    5.28 + }
    5.29 + 
     6.1 --- a/external/libvorbisidec-1.2.1/codebook.c	Mon Oct 07 00:33:55 2019 +0300
     6.2 +++ b/external/libvorbisidec-1.2.1/codebook.c	Mon Oct 07 01:04:02 2019 +0300
     6.3 @@ -258,7 +258,7 @@
     6.4  	t[i] = book->valuelist+entry[i]*book->dim;
     6.5        }
     6.6        for(i=0,o=0;i<book->dim;i++,o+=step)
     6.7 -	for (j=0;j<step;j++)
     6.8 +	for (j=0;o+j<n && j<step;j++)
     6.9  	  a[o+j]+=t[j][i]>>shift;
    6.10      }else{
    6.11        for (i = 0; i < step; i++) {
    6.12 @@ -267,7 +267,7 @@
    6.13  	t[i] = book->valuelist+entry[i]*book->dim;
    6.14        }
    6.15        for(i=0,o=0;i<book->dim;i++,o+=step)
    6.16 -	for (j=0;j<step;j++)
    6.17 +	for (j=0;o+j<n && j<step;j++)
    6.18  	  a[o+j]+=t[j][i]<<-shift;
    6.19      }
    6.20    }
    6.21 @@ -287,7 +287,7 @@
    6.22  	entry = decode_packed_entry_number(book,b);
    6.23  	if(entry==-1)return(-1);
    6.24  	t     = book->valuelist+entry*book->dim;
    6.25 -	for (j=0;j<book->dim;)
    6.26 +	for (j=0;i<n && j<book->dim;)
    6.27  	  a[i++]+=t[j++]>>shift;
    6.28        }
    6.29      }else{
    6.30 @@ -295,7 +295,7 @@
    6.31  	entry = decode_packed_entry_number(book,b);
    6.32  	if(entry==-1)return(-1);
    6.33  	t     = book->valuelist+entry*book->dim;
    6.34 -	for (j=0;j<book->dim;)
    6.35 +	for (j=0;i<n && j<book->dim;)
    6.36  	  a[i++]+=t[j++]<<-shift;
    6.37        }
    6.38      }
    6.39 @@ -352,15 +352,15 @@
    6.40      long i,j,entry;
    6.41      int chptr=0;
    6.42      int shift=point-book->binarypoint;
    6.43 -    
    6.44 +    int m=offset+n;
    6.45      if(shift>=0){
    6.46        
    6.47 -      for(i=offset;i<offset+n;){
    6.48 +      for(i=offset;i<m;){
    6.49  	entry = decode_packed_entry_number(book,b);
    6.50  	if(entry==-1)return(-1);
    6.51  	{
    6.52  	  const ogg_int32_t *t = book->valuelist+entry*book->dim;
    6.53 -	  for (j=0;j<book->dim;j++){
    6.54 +	  for (j=0;i<m && j<book->dim;j++){
    6.55  	    a[chptr++][i]+=t[j]>>shift;
    6.56  	    if(chptr==ch){
    6.57  	      chptr=0;
    6.58 @@ -371,12 +371,12 @@
    6.59        }
    6.60      }else{
    6.61        
    6.62 -      for(i=offset;i<offset+n;){
    6.63 +      for(i=offset;i<m;){
    6.64  	entry = decode_packed_entry_number(book,b);
    6.65  	if(entry==-1)return(-1);
    6.66  	{
    6.67  	  const ogg_int32_t *t = book->valuelist+entry*book->dim;
    6.68 -	  for (j=0;j<book->dim;j++){
    6.69 +	  for (j=0;i<m && j<book->dim;j++){
    6.70  	    a[chptr++][i]+=t[j]<<-shift;
    6.71  	    if(chptr==ch){
    6.72  	      chptr=0;
     7.1 --- a/external/libvorbisidec-1.2.1/info.c	Mon Oct 07 00:33:55 2019 +0300
     7.2 +++ b/external/libvorbisidec-1.2.1/info.c	Mon Oct 07 01:04:02 2019 +0300
     7.3 @@ -230,7 +230,6 @@
     7.4  static int _vorbis_unpack_books(vorbis_info *vi,oggpack_buffer *opb){
     7.5    codec_setup_info     *ci=(codec_setup_info *)vi->codec_setup;
     7.6    int i;
     7.7 -  if(!ci)return(OV_EFAULT);
     7.8  
     7.9    /* codebooks */
    7.10    ci->books=oggpack_read(opb,8)+1;
    7.11 @@ -370,6 +369,10 @@
    7.12  	  /* um... we didn't get the initial header */
    7.13  	  return(OV_EBADHEADER);
    7.14  	}
    7.15 +        if(vc->vendor!=NULL){
    7.16 +          /* previously initialized comment header */
    7.17 +          return(OV_EBADHEADER);
    7.18 +        }
    7.19  
    7.20  	return(_vorbis_unpack_comment(vc,&opb));
    7.21  
    7.22 @@ -378,6 +381,14 @@
    7.23  	  /* um... we didn;t get the initial header or comments yet */
    7.24  	  return(OV_EBADHEADER);
    7.25  	}
    7.26 +        if(vi->codec_setup==NULL){
    7.27 +          /* improperly initialized vorbis_info */
    7.28 +          return(OV_EFAULT);
    7.29 +        }
    7.30 +        if(((codec_setup_info *)vi->codec_setup)->books>0){
    7.31 +          /* previously initialized setup header */
    7.32 +          return(OV_EBADHEADER);
    7.33 +        }
    7.34  
    7.35  	return(_vorbis_unpack_books(vi,&opb));
    7.36  
     8.1 --- a/external/libvorbisidec-1.2.1/iseeking_example.c	Mon Oct 07 00:33:55 2019 +0300
     8.2 +++ b/external/libvorbisidec-1.2.1/iseeking_example.c	Mon Oct 07 01:04:02 2019 +0300
     8.3 @@ -12,7 +12,7 @@
     8.4   ********************************************************************
     8.5  
     8.6   function: illustrate seeking, and test it too
     8.7 - last mod: $Id: iseeking_example.c 16037 2009-05-26 21:10:58Z xiphmont $
     8.8 + last mod: $Id$
     8.9  
    8.10   ********************************************************************/
    8.11  
    8.12 @@ -160,7 +160,7 @@
    8.13               (long)pcmlength);
    8.14      
    8.15        for(i=0;i<1000;i++){
    8.16 -        ogg_int64_t val=(double)rand()*pcmlength/RAND_MAX;
    8.17 +        ogg_int64_t val=i==0?0:(double)rand()*pcmlength/RAND_MAX;
    8.18          fprintf(stderr,"\r\t%d [pcm position %ld]...     ",i,(long)val);
    8.19          ret=ov_pcm_seek_page(&ov,val);
    8.20          if(ret<0){
    8.21 @@ -179,7 +179,7 @@
    8.22               (long)pcmlength);
    8.23      
    8.24        for(i=0;i<1000;i++){
    8.25 -        ogg_int64_t val=(double)rand()*pcmlength/RAND_MAX;
    8.26 +        ogg_int64_t val=i==0?0:(double)rand()*pcmlength/RAND_MAX;
    8.27          fprintf(stderr,"\r\t%d [pcm position %ld]...     ",i,(long)val);
    8.28          ret=ov_pcm_seek(&ov,val);
    8.29          if(ret<0){
     9.1 --- a/external/libvorbisidec-1.2.1/ivorbisfile.h	Mon Oct 07 00:33:55 2019 +0300
     9.2 +++ b/external/libvorbisidec-1.2.1/ivorbisfile.h	Mon Oct 07 01:04:02 2019 +0300
     9.3 @@ -26,7 +26,8 @@
     9.4  #include <stdio.h>
     9.5  #include "ivorbiscodec.h"
     9.6  
     9.7 -#define CHUNKSIZE 1024
     9.8 +#define CHUNKSIZE 65535
     9.9 +#define READSIZE  1024
    9.10  /* The function prototypes for the callbacks are basically the same as for
    9.11   * the stdio functions fread, fseek, fclose, ftell. 
    9.12   * The one difference is that the FILE * arguments have been replaced with
    10.1 --- a/external/libvorbisidec-1.2.1/mdct.c	Mon Oct 07 00:33:55 2019 +0300
    10.2 +++ b/external/libvorbisidec-1.2.1/mdct.c	Mon Oct 07 01:04:02 2019 +0300
    10.3 @@ -13,7 +13,7 @@
    10.4  
    10.5   function: normalized modified discrete cosine transform
    10.6             power of two length transform only [64 <= n ]
    10.7 - last mod: $Id: mdct.c,v 1.9 2002/10/16 09:17:39 xiphmont Exp $
    10.8 + last mod: $Id$
    10.9  
   10.10   Original algorithm adapted long ago from _The use of multirate filter
   10.11   banks for coding of high quality digital audio_, by T. Sporer,
    11.1 --- a/external/libvorbisidec-1.2.1/synthesis.c	Mon Oct 07 00:33:55 2019 +0300
    11.2 +++ b/external/libvorbisidec-1.2.1/synthesis.c	Mon Oct 07 01:04:02 2019 +0300
    11.3 @@ -67,7 +67,7 @@
    11.4    
    11.5    /* more setup */
    11.6    vb->granulepos=op->granulepos;
    11.7 -  vb->sequence=op->packetno-3; /* first block is third packet */
    11.8 +  vb->sequence=op->packetno; /* first block is third packet */
    11.9    vb->eofflag=op->e_o_s;
   11.10  
   11.11    if(decodep){
   11.12 @@ -124,7 +124,7 @@
   11.13      /* read our mode and pre/post windowsize */
   11.14      mode=oggpack_read(&opb,modebits);
   11.15    }
   11.16 -  if(mode==-1)return(OV_EBADPACKET);
   11.17 +  if(mode==-1 || !ci->mode_param[mode])return(OV_EBADPACKET);
   11.18    return(ci->blocksizes[ci->mode_param[mode]->blockflag]);
   11.19  }
   11.20  
    12.1 --- a/external/libvorbisidec-1.2.1/vorbisfile.c	Mon Oct 07 00:33:55 2019 +0300
    12.2 +++ b/external/libvorbisidec-1.2.1/vorbisfile.c	Mon Oct 07 01:04:02 2019 +0300
    12.3 @@ -6,13 +6,13 @@
    12.4   * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
    12.5   * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.       *
    12.6   *                                                                  *
    12.7 - * THE OggVorbis 'TREMOR' SOURCE CODE IS (C) COPYRIGHT 1994-2009    *
    12.8 + * THE OggVorbis 'TREMOR' SOURCE CODE IS (C) COPYRIGHT 1994-2014    *
    12.9   * BY THE Xiph.Org FOUNDATION http://www.xiph.org/                  *
   12.10   *                                                                  *
   12.11   ********************************************************************
   12.12  
   12.13   function: stdio-based convenience library for opening/seeking/decoding
   12.14 - last mod: $Id: vorbisfile.c,v 1.6 2003/03/30 23:40:56 xiphmont Exp $
   12.15 + last mod: $Id$
   12.16  
   12.17   ********************************************************************/
   12.18  
   12.19 @@ -63,8 +63,8 @@
   12.20    errno=0;
   12.21    if(!(vf->callbacks.read_func))return(-1);
   12.22    if(vf->datasource){
   12.23 -    char *buffer=ogg_sync_buffer(&vf->oy,CHUNKSIZE);
   12.24 -    long bytes=(vf->callbacks.read_func)(buffer,1,CHUNKSIZE,vf->datasource);
   12.25 +    char *buffer=ogg_sync_buffer(&vf->oy,READSIZE);
   12.26 +    long bytes=(vf->callbacks.read_func)(buffer,1,READSIZE,vf->datasource);
   12.27      if(bytes>0)ogg_sync_wrote(&vf->oy,bytes);
   12.28      if(bytes==0 && errno)return(-1);
   12.29      return(bytes);
   12.30 @@ -75,11 +75,14 @@
   12.31  /* save a tiny smidge of verbosity to make the code more readable */
   12.32  static int _seek_helper(OggVorbis_File *vf,ogg_int64_t offset){
   12.33    if(vf->datasource){
   12.34 -    if(!(vf->callbacks.seek_func)||
   12.35 -       (vf->callbacks.seek_func)(vf->datasource, offset, SEEK_SET) == -1)
   12.36 -      return OV_EREAD;
   12.37 -    vf->offset=offset;
   12.38 -    ogg_sync_reset(&vf->oy);
   12.39 +    /* only seek if the file position isn't already there */
   12.40 +    if(vf->offset != offset){
   12.41 +      if(!(vf->callbacks.seek_func)||
   12.42 +         (vf->callbacks.seek_func)(vf->datasource, offset, SEEK_SET) == -1)
   12.43 +        return OV_EREAD;
   12.44 +      vf->offset=offset;
   12.45 +      ogg_sync_reset(&vf->oy);
   12.46 +    }
   12.47    }else{
   12.48      /* shouldn't happen unless someone writes a broken callback */
   12.49      return OV_EFAULT;
   12.50 @@ -133,14 +136,12 @@
   12.51    }
   12.52  }
   12.53  
   12.54 -/* find the latest page beginning before the current stream cursor
   12.55 -   position. Much dirtier than the above as Ogg doesn't have any
   12.56 -   backward search linkage.  no 'readp' as it will certainly have to
   12.57 -   read. */
   12.58 +/* find the latest page beginning before the passed in position. Much
   12.59 +   dirtier than the above as Ogg doesn't have any backward search
   12.60 +   linkage.  no 'readp' as it will certainly have to read. */
   12.61  /* returns offset or OV_EREAD, OV_FAULT */
   12.62 -static ogg_int64_t _get_prev_page(OggVorbis_File *vf,ogg_page *og){
   12.63 -  ogg_int64_t begin=vf->offset;
   12.64 -  ogg_int64_t end=begin;
   12.65 +static ogg_int64_t _get_prev_page(OggVorbis_File *vf,ogg_int64_t begin,ogg_page *og){
   12.66 +  ogg_int64_t end = begin;
   12.67    ogg_int64_t ret;
   12.68    ogg_int64_t offset=-1;
   12.69  
   12.70 @@ -215,11 +216,10 @@
   12.71     info of last page of the matching serial number instead of the very
   12.72     last page.  If no page of the specified serialno is seen, it will
   12.73     return the info of last page and alter *serialno.  */
   12.74 -static ogg_int64_t _get_prev_page_serial(OggVorbis_File *vf,
   12.75 +static ogg_int64_t _get_prev_page_serial(OggVorbis_File *vf, ogg_int64_t begin,
   12.76                                           ogg_uint32_t *serial_list, int serial_n,
   12.77                                           int *serialno, ogg_int64_t *granpos){
   12.78    ogg_page og;
   12.79 -  ogg_int64_t begin=vf->offset;
   12.80    ogg_int64_t end=begin;
   12.81    ogg_int64_t ret;
   12.82  
   12.83 @@ -490,10 +490,10 @@
   12.84         down to (or just started with) a single link.  Now we need to
   12.85         find the last vorbis page belonging to the first vorbis stream
   12.86         for this link. */
   12.87 -
   12.88 +    searched = end;
   12.89      while(endserial != serialno){
   12.90        endserial = serialno;
   12.91 -      vf->offset=_get_prev_page_serial(vf,currentno_list,currentnos,&endserial,&endgran);
   12.92 +      searched=_get_prev_page_serial(vf,searched,currentno_list,currentnos,&endserial,&endgran);
   12.93      }
   12.94  
   12.95      vf->links=m+1;
   12.96 @@ -510,14 +510,19 @@
   12.97  
   12.98      vf->offsets[m+1]=end;
   12.99      vf->offsets[m]=begin;
  12.100 -    vf->pcmlengths[m*2+1]=endgran;
  12.101 +    vf->pcmlengths[m*2+1]=(endgran<0?0:endgran);
  12.102  
  12.103    }else{
  12.104  
  12.105 +    /* last page is not in the starting stream's serial number list,
  12.106 +       so we have multiple links.  Find where the stream that begins
  12.107 +       our bisection ends. */
  12.108 +
  12.109      ogg_uint32_t *next_serialno_list=NULL;
  12.110      int next_serialnos=0;
  12.111      vorbis_info vi;
  12.112      vorbis_comment vc;
  12.113 +    int testserial = serialno+1;
  12.114  
  12.115      /* the below guards against garbage seperating the last and
  12.116         first pages of two links. */
  12.117 @@ -530,10 +535,8 @@
  12.118          bisect=(searched+endsearched)/2;
  12.119        }
  12.120  
  12.121 -      if(bisect != vf->offset){
  12.122 -        ret=_seek_helper(vf,bisect);
  12.123 -        if(ret)return(ret);
  12.124 -      }
  12.125 +      ret=_seek_helper(vf,bisect);
  12.126 +      if(ret)return(ret);
  12.127  
  12.128        last=_get_next_page(vf,&og,-1);
  12.129        if(last==OV_EREAD)return(OV_EREAD);
  12.130 @@ -546,28 +549,22 @@
  12.131      }
  12.132  
  12.133      /* Bisection point found */
  12.134 -
  12.135      /* for the time being, fetch end PCM offset the simple way */
  12.136 -    {
  12.137 -      int testserial = serialno+1;
  12.138 -      vf->offset = next;
  12.139 -      while(testserial != serialno){
  12.140 -        testserial = serialno;
  12.141 -        vf->offset=_get_prev_page_serial(vf,currentno_list,currentnos,&testserial,&searchgran);
  12.142 -      }
  12.143 +    searched = next;
  12.144 +    while(testserial != serialno){
  12.145 +      testserial = serialno;
  12.146 +      searched = _get_prev_page_serial(vf,searched,currentno_list,currentnos,&testserial,&searchgran);
  12.147      }
  12.148  
  12.149 -    if(vf->offset!=next){
  12.150 -      ret=_seek_helper(vf,next);
  12.151 -      if(ret)return(ret);
  12.152 -    }
  12.153 +    ret=_seek_helper(vf,next);
  12.154 +    if(ret)return(ret);
  12.155  
  12.156      ret=_fetch_headers(vf,&vi,&vc,&next_serialno_list,&next_serialnos,NULL);
  12.157      if(ret)return(ret);
  12.158      serialno = vf->os.serialno;
  12.159      dataoffset = vf->offset;
  12.160  
  12.161 -    /* this will consume a page, however the next bistection always
  12.162 +    /* this will consume a page, however the next bisection always
  12.163         starts with a raw seek */
  12.164      pcmoffset = _initial_pcmoffset(vf,&vi);
  12.165  
  12.166 @@ -587,6 +584,7 @@
  12.167      vf->pcmlengths[m*2+1]=searchgran;
  12.168      vf->pcmlengths[m*2+2]=pcmoffset;
  12.169      vf->pcmlengths[m*2+3]-=pcmoffset;
  12.170 +    if(vf->pcmlengths[m*2+3]<0)vf->pcmlengths[m*2+3]=0;
  12.171  
  12.172    }
  12.173    return(0);
  12.174 @@ -634,11 +632,11 @@
  12.175    /* Get the offset of the last page of the physical bitstream, or, if
  12.176       we're lucky the last vorbis page of this link as most OggVorbis
  12.177       files will contain a single logical bitstream */
  12.178 -  end=_get_prev_page_serial(vf,vf->serialnos+2,vf->serialnos[1],&endserial,&endgran);
  12.179 +  end=_get_prev_page_serial(vf,vf->end,vf->serialnos+2,vf->serialnos[1],&endserial,&endgran);
  12.180    if(end<0)return(end);
  12.181  
  12.182    /* now determine bitstream structure recursively */
  12.183 -  if(_bisect_forward_serialno(vf,0,dataoffset,vf->offset,endgran,endserial,
  12.184 +  if(_bisect_forward_serialno(vf,0,dataoffset,end,endgran,endserial,
  12.185                                vf->serialnos+2,vf->serialnos[1],0)<0)return(OV_EREAD);
  12.186  
  12.187    vf->offsets[0]=0;
  12.188 @@ -646,6 +644,7 @@
  12.189    vf->dataoffsets[0]=dataoffset;
  12.190    vf->pcmlengths[0]=pcmoffset;
  12.191    vf->pcmlengths[1]-=pcmoffset;
  12.192 +  if(vf->pcmlengths[1]<0)vf->pcmlengths[1]=0;
  12.193  
  12.194    return(ov_raw_seek(vf,dataoffset));
  12.195  }
  12.196 @@ -713,7 +712,7 @@
  12.197                if(oldsamples)return(OV_EFAULT);
  12.198  
  12.199                vorbis_synthesis_blockin(&vf->vd,&vf->vb);
  12.200 -              vf->samptrack+=vorbis_synthesis_pcmout(&vf->vd,NULL)-oldsamples;
  12.201 +              vf->samptrack+=vorbis_synthesis_pcmout(&vf->vd,NULL);
  12.202                vf->bittrack+=op_ptr->bytes*8;
  12.203              }
  12.204  
  12.205 @@ -1401,22 +1400,41 @@
  12.206      if(pos>=total)break;
  12.207    }
  12.208  
  12.209 -  /* search within the logical bitstream for the page with the highest
  12.210 -     pcm_pos preceding (or equal to) pos.  There is a danger here;
  12.211 -     missing pages or incorrect frame number information in the
  12.212 -     bitstream could make our task impossible.  Account for that (it
  12.213 -     would be an error condition) */
  12.214 +  /* Search within the logical bitstream for the page with the highest
  12.215 +     pcm_pos preceding pos.  If we're looking for a position on the
  12.216 +     first page, bisection will halt without finding our position as
  12.217 +     it's before the first explicit granulepos fencepost. That case is
  12.218 +     handled separately below.
  12.219  
  12.220 -  /* new search algorithm by HB (Nicholas Vinen) */
  12.221 +     There is a danger here; missing pages or incorrect frame number
  12.222 +     information in the bitstream could make our task impossible.
  12.223 +     Account for that (it would be an error condition) */
  12.224 +
  12.225 +  /* new search algorithm originally by HB (Nicholas Vinen) */
  12.226 +
  12.227    {
  12.228      ogg_int64_t end=vf->offsets[link+1];
  12.229 -    ogg_int64_t begin=vf->offsets[link];
  12.230 +    ogg_int64_t begin=vf->dataoffsets[link];
  12.231      ogg_int64_t begintime = vf->pcmlengths[link*2];
  12.232      ogg_int64_t endtime = vf->pcmlengths[link*2+1]+begintime;
  12.233      ogg_int64_t target=pos-total+begintime;
  12.234 -    ogg_int64_t best=begin;
  12.235 +    ogg_int64_t best=-1;
  12.236 +    int         got_page=0;
  12.237  
  12.238      ogg_page og;
  12.239 +
  12.240 +    /* if we have only one page, there will be no bisection.  Grab the page here */
  12.241 +    if(begin==end){
  12.242 +      result=_seek_helper(vf,begin);
  12.243 +      if(result) goto seek_error;
  12.244 +
  12.245 +      result=_get_next_page(vf,&og,1);
  12.246 +      if(result<0) goto seek_error;
  12.247 +
  12.248 +      got_page=1;
  12.249 +    }
  12.250 +
  12.251 +    /* bisection loop */
  12.252      while(begin<end){
  12.253        ogg_int64_t bisect;
  12.254  
  12.255 @@ -1431,51 +1449,80 @@
  12.256            bisect=begin;
  12.257        }
  12.258  
  12.259 -      if(bisect!=vf->offset){
  12.260 -        result=_seek_helper(vf,bisect);
  12.261 -        if(result) goto seek_error;
  12.262 -      }
  12.263 +      result=_seek_helper(vf,bisect);
  12.264 +      if(result) goto seek_error;
  12.265  
  12.266 +      /* read loop within the bisection loop */
  12.267        while(begin<end){
  12.268          result=_get_next_page(vf,&og,end-vf->offset);
  12.269          if(result==OV_EREAD) goto seek_error;
  12.270          if(result<0){
  12.271 +          /* there is no next page! */
  12.272            if(bisect<=begin+1)
  12.273 -            end=begin; /* found it */
  12.274 +            /* No bisection left to perform.  We've either found the
  12.275 +               best candidate already or failed. Exit loop. */
  12.276 +            end=begin;
  12.277            else{
  12.278 +            /* We tried to load a fraction of the last page; back up a
  12.279 +               bit and try to get the whole last page */
  12.280              if(bisect==0) goto seek_error;
  12.281              bisect-=CHUNKSIZE;
  12.282 +
  12.283 +            /* don't repeat/loop on a read we've already performed */
  12.284              if(bisect<=begin)bisect=begin+1;
  12.285 +
  12.286 +            /* seek and continue bisection */
  12.287              result=_seek_helper(vf,bisect);
  12.288              if(result) goto seek_error;
  12.289            }
  12.290          }else{
  12.291            ogg_int64_t granulepos;
  12.292 +          got_page=1;
  12.293  
  12.294 +          /* got a page. analyze it */
  12.295 +          /* only consider pages from primary vorbis stream */
  12.296            if(ogg_page_serialno(&og)!=vf->serialnos[link])
  12.297              continue;
  12.298  
  12.299 +          /* only consider pages with the granulepos set */
  12.300            granulepos=ogg_page_granulepos(&og);
  12.301            if(granulepos==-1)continue;
  12.302  
  12.303            if(granulepos<target){
  12.304 +            /* this page is a successful candidate! Set state */
  12.305 +
  12.306              best=result;  /* raw offset of packet with granulepos */
  12.307              begin=vf->offset; /* raw offset of next page */
  12.308              begintime=granulepos;
  12.309  
  12.310 +            /* if we're before our target but within a short distance,
  12.311 +               don't bisect; read forward */
  12.312              if(target-begintime>44100)break;
  12.313 -            bisect=begin; /* *not* begin + 1 */
  12.314 +
  12.315 +            bisect=begin; /* *not* begin + 1 as above */
  12.316            }else{
  12.317 -            if(bisect<=begin+1)
  12.318 -              end=begin;  /* found it */
  12.319 -            else{
  12.320 -              if(end==vf->offset){ /* we're pretty close - we'd be stuck in */
  12.321 +
  12.322 +            /* This is one of our pages, but the granpos is
  12.323 +               post-target; it is not a bisection return
  12.324 +               candidate. (The only way we'd use it is if it's the
  12.325 +               first page in the stream; we handle that case later
  12.326 +               outside the bisection) */
  12.327 +            if(bisect<=begin+1){
  12.328 +              /* No bisection left to perform.  We've either found the
  12.329 +                 best candidate already or failed. Exit loop. */
  12.330 +              end=begin;
  12.331 +            }else{
  12.332 +              if(end==vf->offset){
  12.333 +                /* bisection read to the end; use the known page
  12.334 +                   boundary (result) to update bisection, back up a
  12.335 +                   little bit, and try again */
  12.336                  end=result;
  12.337 -                bisect-=CHUNKSIZE; /* an endless loop otherwise. */
  12.338 +                bisect-=CHUNKSIZE;
  12.339                  if(bisect<=begin)bisect=begin+1;
  12.340                  result=_seek_helper(vf,bisect);
  12.341                  if(result) goto seek_error;
  12.342                }else{
  12.343 +                /* Normal bisection */
  12.344                  end=bisect;
  12.345                  endtime=granulepos;
  12.346                  break;
  12.347 @@ -1486,9 +1533,46 @@
  12.348        }
  12.349      }
  12.350  
  12.351 -    /* found our page. seek to it, update pcm offset. Easier case than
  12.352 -       raw_seek, don't keep packets preceding granulepos. */
  12.353 -    {
  12.354 +    /* Out of bisection: did it 'fail?' */
  12.355 +    if(best == -1){
  12.356 +
  12.357 +      /* Check the 'looking for data in first page' special case;
  12.358 +         bisection would 'fail' because our search target was before the
  12.359 +         first PCM granule position fencepost. */
  12.360 +
  12.361 +      if(got_page &&
  12.362 +         begin == vf->dataoffsets[link] &&
  12.363 +         ogg_page_serialno(&og)==vf->serialnos[link]){
  12.364 +
  12.365 +        /* Yes, this is the beginning-of-stream case. We already have
  12.366 +           our page, right at the beginning of PCM data.  Set state
  12.367 +           and return. */
  12.368 +
  12.369 +        vf->pcm_offset=total;
  12.370 +
  12.371 +        if(link!=vf->current_link){
  12.372 +          /* Different link; dump entire decode machine */
  12.373 +          _decode_clear(vf);
  12.374 +
  12.375 +          vf->current_link=link;
  12.376 +          vf->current_serialno=vf->serialnos[link];
  12.377 +          vf->ready_state=STREAMSET;
  12.378 +
  12.379 +        }else{
  12.380 +          vorbis_synthesis_restart(&vf->vd);
  12.381 +        }
  12.382 +
  12.383 +        ogg_stream_reset_serialno(&vf->os,vf->current_serialno);
  12.384 +        ogg_stream_pagein(&vf->os,&og);
  12.385 +
  12.386 +      }else
  12.387 +        goto seek_error;
  12.388 +
  12.389 +    }else{
  12.390 +
  12.391 +      /* Bisection found our page. seek to it, update pcm offset. Easier case than
  12.392 +         raw_seek, don't keep packets preceding granulepos. */
  12.393 +
  12.394        ogg_page og;
  12.395        ogg_packet op;
  12.396  
  12.397 @@ -1518,23 +1602,23 @@
  12.398        while(1){
  12.399          result=ogg_stream_packetpeek(&vf->os,&op);
  12.400          if(result==0){
  12.401 -          /* !!! the packet finishing this page originated on a
  12.402 -             preceding page. Keep fetching previous pages until we
  12.403 -             get one with a granulepos or without the 'continued' flag
  12.404 -             set.  Then just use raw_seek for simplicity. */
  12.405 -
  12.406 -          result=_seek_helper(vf,best);
  12.407 -          if(result<0) goto seek_error;
  12.408 -
  12.409 -          while(1){
  12.410 -            result=_get_prev_page(vf,&og);
  12.411 +          /* No packet returned; we exited the bisection with 'best'
  12.412 +             pointing to a page with a granule position, so the packet
  12.413 +             finishing this page ('best') originated on a preceding
  12.414 +             page. Keep fetching previous pages until we get one with
  12.415 +             a granulepos or without the 'continued' flag set.  Then
  12.416 +             just use raw_seek for simplicity. */
  12.417 +          /* Do not rewind past the beginning of link data; if we do,
  12.418 +             it's either a bug or a broken stream */
  12.419 +          result=best;
  12.420 +          while(result>vf->dataoffsets[link]){
  12.421 +            result=_get_prev_page(vf,result,&og);
  12.422              if(result<0) goto seek_error;
  12.423              if(ogg_page_serialno(&og)==vf->current_serialno &&
  12.424                 (ogg_page_granulepos(&og)>-1 ||
  12.425                  !ogg_page_continued(&og))){
  12.426                return ov_raw_seek(vf,result);
  12.427              }
  12.428 -            vf->offset=result;
  12.429            }
  12.430          }
  12.431          if(result<0){