diff options
author | Aleksoid <aleksoid@users.sourceforge.net> | 2010-04-28 08:58:20 +0400 |
---|---|---|
committer | Aleksoid <aleksoid@users.sourceforge.net> | 2010-04-28 08:58:20 +0400 |
commit | 5f5d87ef81a2933b3c7047c997a824e1bb693d4c (patch) | |
tree | 9043157920c49500d8b4c952c2e2b4ffccc0ca8d /src | |
parent | 3caef5862cc014f950483f7d14da21c9c59fe900 (diff) |
Update : libVorbis 1.3.1(20100325);
git-svn-id: https://mpc-hc.svn.sourceforge.net/svnroot/mpc-hc/trunk@1816 10f7b99b-c216-0410-bff0-8a66a9350fd8
Diffstat (limited to 'src')
21 files changed, 3587 insertions, 1231 deletions
diff --git a/src/filters/transform/MpaDecFilter/libvorbisidec/backends.h b/src/filters/transform/MpaDecFilter/libvorbisidec/backends.h index 2437d6c76..ff5bcc95f 100644 --- a/src/filters/transform/MpaDecFilter/libvorbisidec/backends.h +++ b/src/filters/transform/MpaDecFilter/libvorbisidec/backends.h @@ -12,7 +12,7 @@ function: libvorbis backend and mapping structures; needed for static mode headers - last mod: $Id: backends.h 16552 2009-09-12 02:09:04Z xiphmont $ + last mod: $Id: backends.h 16962 2010-03-11 07:30:34Z xiphmont $ ********************************************************************/ @@ -93,10 +93,10 @@ typedef struct{ void (*free_info) (vorbis_info_residue *); void (*free_look) (vorbis_look_residue *); long **(*class) (struct vorbis_block *,vorbis_look_residue *, - float **,int *,int); + int **,int *,int); int (*forward) (oggpack_buffer *,struct vorbis_block *, vorbis_look_residue *, - float **,float **,int *,int,long **); + int **,int *,int,long **,int); int (*inverse) (struct vorbis_block *,vorbis_look_residue *, float **,int *,int); } vorbis_func_residue; @@ -114,8 +114,8 @@ typedef struct vorbis_info_residue0{ int secondstages[64]; /* expanded out to pointers in lookup */ int booklist[512]; /* list of second stage books */ - const float classmetric1[64]; - const float classmetric2[64]; + const int classmetric1[64]; + const int classmetric2[64]; } vorbis_info_residue0; /* Mapping backend generic *****************************************/ diff --git a/src/filters/transform/MpaDecFilter/libvorbisidec/barkmel.c b/src/filters/transform/MpaDecFilter/libvorbisidec/barkmel.c new file mode 100644 index 000000000..ce34b6204 --- /dev/null +++ b/src/filters/transform/MpaDecFilter/libvorbisidec/barkmel.c @@ -0,0 +1,64 @@ +/********************************************************************
+ * *
+ * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
+ * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
+ * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
+ * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
+ * *
+ * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2007 *
+ * by the Xiph.Org Foundation http://www.xiph.org/ *
+ * *
+ ********************************************************************
+
+ function: bark scale utility
+ last mod: $Id: barkmel.c 16037 2009-05-26 21:10:58Z xiphmont $
+
+ ********************************************************************/
+
+#include <stdio.h>
+#include "scales.h"
+int main(){
+ int i;
+ double rate;
+ for(i=64;i<32000;i*=2){
+ rate=48000.f;
+ fprintf(stderr,"rate=%gHz, block=%d, f(1)=%.2gHz bark(1)=%.2g (of %.2g)\n",
+ rate,i,rate/2 / (i/2),toBARK(rate/2 /(i/2)),toBARK(rate/2));
+
+ rate=44100.f;
+ fprintf(stderr,"rate=%gHz, block=%d, f(1)=%.2gHz bark(1)=%.2g (of %.2g)\n",
+ rate,i,rate/2 / (i/2),toBARK(rate/2 /(i/2)),toBARK(rate/2));
+
+ rate=32000.f;
+ fprintf(stderr,"rate=%gHz, block=%d, f(1)=%.2gHz bark(1)=%.2g (of %.2g)\n",
+ rate,i,rate/2 / (i/2),toBARK(rate/2 /(i/2)),toBARK(rate/2));
+
+ rate=22050.f;
+ fprintf(stderr,"rate=%gHz, block=%d, f(1)=%.2gHz bark(1)=%.2g (of %.2g)\n",
+ rate,i,rate/2 / (i/2),toBARK(rate/2 /(i/2)),toBARK(rate/2));
+
+ rate=16000.f;
+ fprintf(stderr,"rate=%gHz, block=%d, f(1)=%.2gHz bark(1)=%.2g (of %.2g)\n",
+ rate,i,rate/2 / (i/2),toBARK(rate/2 /(i/2)),toBARK(rate/2));
+
+ rate=11025.f;
+ fprintf(stderr,"rate=%gHz, block=%d, f(1)=%.2gHz bark(1)=%.2g (of %.2g)\n",
+ rate,i,rate/2 / (i/2),toBARK(rate/2 /(i/2)),toBARK(rate/2));
+
+ rate=8000.f;
+ fprintf(stderr,"rate=%gHz, block=%d, f(1)=%.2gHz bark(1)=%.2g (of %.2g)\n\n",
+ rate,i,rate/2 / (i/2),toBARK(rate/2 /(i/2)),toBARK(rate/2));
+
+
+ }
+ {
+ float i;
+ int j;
+ for(i=0.,j=0;i<28;i+=1,j++){
+ fprintf(stderr,"(%d) bark=%f %gHz (%d of 128)\n",
+ j,i,fromBARK(i),(int)(fromBARK(i)/22050.*128.));
+ }
+ }
+ return(0);
+}
+
diff --git a/src/filters/transform/MpaDecFilter/libvorbisidec/bitwise.c b/src/filters/transform/MpaDecFilter/libvorbisidec/bitwise.c index 6eb58ad3d..0d381bdc8 100644 --- a/src/filters/transform/MpaDecFilter/libvorbisidec/bitwise.c +++ b/src/filters/transform/MpaDecFilter/libvorbisidec/bitwise.c @@ -1,17 +1,17 @@ /******************************************************************** * * - * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. * + * THIS FILE IS PART OF THE Ogg CONTAINER SOURCE CODE. * * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * * * - * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2009 * + * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2010 * * by the Xiph.Org Foundation http://www.xiph.org/ * * * ******************************************************************** function: packing variable sized words into an octet stream - last mod: $Id: bitwise.c 16051 2009-05-27 05:00:06Z xiphmont $ + last mod: $Id: bitwise.c 17063 2010-03-26 03:38:04Z giles $ ********************************************************************/ @@ -692,7 +692,7 @@ int main(void){ fprintf(stderr,"ok."); fprintf(stderr,"\nTesting read past end (LSb): "); - oggpack_readinit(&r,"\0\0\0\0\0\0\0\0",8); + oggpack_readinit(&r,(unsigned char *)"\0\0\0\0\0\0\0\0",8); for(i=0;i<64;i++){ if(oggpack_read(&r,1)!=0){ fprintf(stderr,"failed; got -1 prematurely.\n"); @@ -704,7 +704,7 @@ int main(void){ fprintf(stderr,"failed; read past end without -1.\n"); exit(1); } - oggpack_readinit(&r,"\0\0\0\0\0\0\0\0",8); + oggpack_readinit(&r,(unsigned char *)"\0\0\0\0\0\0\0\0",8); if(oggpack_read(&r,30)!=0 || oggpack_read(&r,16)!=0){ fprintf(stderr,"failed 2; got -1 prematurely.\n"); exit(1); @@ -778,7 +778,7 @@ int main(void){ fprintf(stderr,"ok."); fprintf(stderr,"\nTesting read past end (MSb): "); - oggpackB_readinit(&r,"\0\0\0\0\0\0\0\0",8); + oggpackB_readinit(&r,(unsigned char *)"\0\0\0\0\0\0\0\0",8); for(i=0;i<64;i++){ if(oggpackB_read(&r,1)!=0){ fprintf(stderr,"failed; got -1 prematurely.\n"); @@ -790,7 +790,7 @@ int main(void){ fprintf(stderr,"failed; read past end without -1.\n"); exit(1); } - oggpackB_readinit(&r,"\0\0\0\0\0\0\0\0",8); + oggpackB_readinit(&r,(unsigned char *)"\0\0\0\0\0\0\0\0",8); if(oggpackB_read(&r,30)!=0 || oggpackB_read(&r,16)!=0){ fprintf(stderr,"failed 2; got -1 prematurely.\n"); exit(1); diff --git a/src/filters/transform/MpaDecFilter/libvorbisidec/block.c b/src/filters/transform/MpaDecFilter/libvorbisidec/block.c index 58f7fc7ef..d233177a7 100644 --- a/src/filters/transform/MpaDecFilter/libvorbisidec/block.c +++ b/src/filters/transform/MpaDecFilter/libvorbisidec/block.c @@ -11,7 +11,7 @@ ******************************************************************** function: PCM data vector blocking, windowing and dis/reassembly - last mod: $Id: block.c 16330 2009-07-24 01:58:50Z xiphmont $ + last mod: $Id: block.c 17177 2010-04-26 14:24:08Z xiphmont $ Handle windowing, overlap-add, etc of the PCM vectors. This is made more amusing by Vorbis' current two allowed block sizes. @@ -232,16 +232,17 @@ static int _vds_shared_init(vorbis_dsp_state *v,vorbis_info *vi,int encp){ v->analysisp=1; }else{ /* finish the codebooks */ - if(!ci->fullbooks) + if(!ci->fullbooks){ ci->fullbooks=_ogg_calloc(ci->books,sizeof(*ci->fullbooks)); - for(i=0;i<ci->books;i++){ - if(ci->book_param[i]==NULL) - goto abort_books; - if(vorbis_book_init_decode(ci->fullbooks+i,ci->book_param[i])) - goto abort_books; + for(i=0;i<ci->books;i++){ + if(ci->book_param[i]==NULL) + goto abort_books; + if(vorbis_book_init_decode(ci->fullbooks+i,ci->book_param[i])) + goto abort_books; /* decode codebooks are now standalone after init */ - vorbis_staticbook_destroy(ci->book_param[i]); - ci->book_param[i]=NULL; + vorbis_staticbook_destroy(ci->book_param[i]); + ci->book_param[i]=NULL; + } } } diff --git a/src/filters/transform/MpaDecFilter/libvorbisidec/codebook.c b/src/filters/transform/MpaDecFilter/libvorbisidec/codebook.c index 156ca1204..772eea6d7 100644 --- a/src/filters/transform/MpaDecFilter/libvorbisidec/codebook.c +++ b/src/filters/transform/MpaDecFilter/libvorbisidec/codebook.c @@ -11,7 +11,7 @@ ******************************************************************** function: basic codebook pack/unpack/code/decode operations - last mod: $Id: codebook.c 16597 2009-10-01 02:54:22Z tterribe $ + last mod: $Id: codebook.c 17030 2010-03-25 06:52:55Z xiphmont $ ********************************************************************/ @@ -146,9 +146,9 @@ int vorbis_staticbook_pack(const static_codebook *c,oggpack_buffer *opb){ /* unpacks a codebook from the packet buffer into the codebook struct, readies the codebook auxiliary structures for decode *************/ -int vorbis_staticbook_unpack(oggpack_buffer *opb,static_codebook *s){ +static_codebook *vorbis_staticbook_unpack(oggpack_buffer *opb){ long i,j; - memset(s,0,sizeof(*s)); + static_codebook *s=_ogg_calloc(1,sizeof(*s)); s->allocedp=1; /* make sure alignment is correct */ @@ -207,7 +207,7 @@ int vorbis_staticbook_unpack(oggpack_buffer *opb,static_codebook *s){ break; default: /* EOF */ - return(-1); + goto _eofout; } /* Do we have a mapping to unpack? */ @@ -249,12 +249,12 @@ int vorbis_staticbook_unpack(oggpack_buffer *opb,static_codebook *s){ } /* all set */ - return(0); + return(s); _errout: _eofout: - vorbis_staticbook_clear(s); - return(-1); + vorbis_staticbook_destroy(s); + return(NULL); } /* returns the number of bits ************************************************/ @@ -264,37 +264,6 @@ int vorbis_book_encode(codebook *book, int a, oggpack_buffer *b){ return(book->c->lengthlist[a]); } -/* One the encode side, our vector writers are each designed for a -specific purpose, and the encoder is not flexible without modification: - -The LSP vector coder uses a single stage nearest-match with no -interleave, so no step and no error return. This is specced by floor0 -and doesn't change. - -Residue0 encoding interleaves, uses multiple stages, and each stage -peels of a specific amount of resolution from a lattice (thus we want -to match by threshold, not nearest match). Residue doesn't *have* to -be encoded that way, but to change it, one will need to add more -infrastructure on the encode side (decode side is specced and simpler) */ - -/* floor0 LSP (single stage, non interleaved, nearest match) */ -/* returns entry number and *modifies a* to the quantization value *****/ -int vorbis_book_errorv(codebook *book,float *a){ - int dim=book->dim,k; - int best=_best(book,a,1); - for(k=0;k<dim;k++) - a[k]=(book->valuelist+best*dim)[k]; - return(best); -} - -/* returns the number of bits and *modifies a* to the quantization value *****/ -int vorbis_book_encodev(codebook *book,int best,float *a,oggpack_buffer *b){ - int k,dim=book->dim; - for(k=0;k<dim;k++) - a[k]=(book->valuelist+best*dim)[k]; - return(vorbis_book_encode(book,best,b)); -} - /* the 'eliminate the decode tree' optimization actually requires the codewords to be MSb first, not LSb. This is an annoying inelegancy (and one of the first places where carefully thought out design @@ -495,144 +464,3 @@ long vorbis_book_decodevv_add(codebook *book,float **a,long offset,int ch, } return(0); } - -#ifdef _V_SELFTEST -/* Simple enough; pack a few candidate codebooks, unpack them. Code a - number of vectors through (keeping track of the quantized values), - and decode using the unpacked book. quantized version of in should - exactly equal out */ - -#include <stdio.h> - -#include "vorbis/book/lsp20_0.vqh" -#include "vorbis/book/res0a_13.vqh" -#define TESTSIZE 40 - -float test1[TESTSIZE]={ - 0.105939f, - 0.215373f, - 0.429117f, - 0.587974f, - - 0.181173f, - 0.296583f, - 0.515707f, - 0.715261f, - - 0.162327f, - 0.263834f, - 0.342876f, - 0.406025f, - - 0.103571f, - 0.223561f, - 0.368513f, - 0.540313f, - - 0.136672f, - 0.395882f, - 0.587183f, - 0.652476f, - - 0.114338f, - 0.417300f, - 0.525486f, - 0.698679f, - - 0.147492f, - 0.324481f, - 0.643089f, - 0.757582f, - - 0.139556f, - 0.215795f, - 0.324559f, - 0.399387f, - - 0.120236f, - 0.267420f, - 0.446940f, - 0.608760f, - - 0.115587f, - 0.287234f, - 0.571081f, - 0.708603f, -}; - -float test3[TESTSIZE]={ - 0,1,-2,3,4,-5,6,7,8,9, - 8,-2,7,-1,4,6,8,3,1,-9, - 10,11,12,13,14,15,26,17,18,19, - 30,-25,-30,-1,-5,-32,4,3,-2,0}; - -static_codebook *testlist[]={&_vq_book_lsp20_0, - &_vq_book_res0a_13,NULL}; -float *testvec[]={test1,test3}; - -int main(){ - oggpack_buffer write; - oggpack_buffer read; - long ptr=0,i; - oggpack_writeinit(&write); - - fprintf(stderr,"Testing codebook abstraction...:\n"); - - while(testlist[ptr]){ - codebook c; - static_codebook s; - float *qv=alloca(sizeof(*qv)*TESTSIZE); - float *iv=alloca(sizeof(*iv)*TESTSIZE); - memcpy(qv,testvec[ptr],sizeof(*qv)*TESTSIZE); - memset(iv,0,sizeof(*iv)*TESTSIZE); - - fprintf(stderr,"\tpacking/coding %ld... ",ptr); - - /* pack the codebook, write the testvector */ - oggpack_reset(&write); - vorbis_book_init_encode(&c,testlist[ptr]); /* get it into memory - we can write */ - vorbis_staticbook_pack(testlist[ptr],&write); - fprintf(stderr,"Codebook size %ld bytes... ",oggpack_bytes(&write)); - for(i=0;i<TESTSIZE;i+=c.dim){ - int best=_best(&c,qv+i,1); - vorbis_book_encodev(&c,best,qv+i,&write); - } - vorbis_book_clear(&c); - - fprintf(stderr,"OK.\n"); - fprintf(stderr,"\tunpacking/decoding %ld... ",ptr); - - /* transfer the write data to a read buffer and unpack/read */ - oggpack_readinit(&read,oggpack_get_buffer(&write),oggpack_bytes(&write)); - if(vorbis_staticbook_unpack(&read,&s)){ - fprintf(stderr,"Error unpacking codebook.\n"); - exit(1); - } - if(vorbis_book_init_decode(&c,&s)){ - fprintf(stderr,"Error initializing codebook.\n"); - exit(1); - } - - for(i=0;i<TESTSIZE;i+=c.dim) - if(vorbis_book_decodev_set(&c,iv+i,&read,c.dim)==-1){ - fprintf(stderr,"Error reading codebook test data (EOP).\n"); - exit(1); - } - for(i=0;i<TESTSIZE;i++) - if(fabs(qv[i]-iv[i])>.000001){ - fprintf(stderr,"read (%g) != written (%g) at position (%ld)\n", - iv[i],qv[i],i); - exit(1); - } - - fprintf(stderr,"OK\n"); - ptr++; - } - - /* The above is the trivial stuff; now try unquantizing a log scale codebook */ - - exit(0); -} - -#endif diff --git a/src/filters/transform/MpaDecFilter/libvorbisidec/codebook.h b/src/filters/transform/MpaDecFilter/libvorbisidec/codebook.h index 9174de136..94c30054c 100644 --- a/src/filters/transform/MpaDecFilter/libvorbisidec/codebook.h +++ b/src/filters/transform/MpaDecFilter/libvorbisidec/codebook.h @@ -11,7 +11,7 @@ ******************************************************************** function: basic shared codebook operations - last mod: $Id: codebook.h 16227 2009-07-08 06:58:46Z xiphmont $ + last mod: $Id: codebook.h 17030 2010-03-25 06:52:55Z xiphmont $ ********************************************************************/ @@ -52,50 +52,9 @@ typedef struct static_codebook{ long *quantlist; /* map == 1: (int)(entries^(1/dim)) element column map map == 2: list of dim*entries quantized entry vals */ - - /* encode helpers ********************************************************/ - struct encode_aux_nearestmatch *nearest_tree; - struct encode_aux_threshmatch *thresh_tree; - struct encode_aux_pigeonhole *pigeon_tree; - int allocedp; } static_codebook; -/* this structures an arbitrary trained book to quickly find the - nearest cell match */ -typedef struct encode_aux_nearestmatch{ - /* pre-calculated partitioning tree */ - long *ptr0; - long *ptr1; - - long *p; /* decision points (each is an entry) */ - long *q; /* decision points (each is an entry) */ - long aux; /* number of tree entries */ - long alloc; -} encode_aux_nearestmatch; - -/* assumes a maptype of 1; encode side only, so that's OK */ -typedef struct encode_aux_threshmatch{ - float *quantthresh; - long *quantmap; - int quantvals; - int threshvals; -} encode_aux_threshmatch; - -typedef struct encode_aux_pigeonhole{ - float min; - float del; - - int mapentries; - int quantvals; - long *pigeonmap; - - long fittotal; - long *fitlist; - long *fitmap; - long *fitlength; -} encode_aux_pigeonhole; - typedef struct codebook{ long dim; /* codebook dimensions (elements per vector) */ long entries; /* codebook entries */ @@ -114,9 +73,12 @@ typedef struct codebook{ int dec_firsttablen; int dec_maxlength; + /* The current encoder uses only centered, integer-only lattice books. */ + int quantvals; + int minval; + int delta; } codebook; -extern void vorbis_staticbook_clear(static_codebook *b); extern void vorbis_staticbook_destroy(static_codebook *b); extern int vorbis_book_init_encode(codebook *dest,const static_codebook *source); extern int vorbis_book_init_decode(codebook *dest,const static_codebook *source); @@ -137,12 +99,9 @@ extern long vorbis_book_codelen(codebook *book,int entry); extern int vorbis_staticbook_pack(const static_codebook *c,oggpack_buffer *b); -extern int vorbis_staticbook_unpack(oggpack_buffer *b,static_codebook *c); +extern static_codebook *vorbis_staticbook_unpack(oggpack_buffer *b); extern int vorbis_book_encode(codebook *book, int a, oggpack_buffer *b); -extern int vorbis_book_errorv(codebook *book, float *a); -extern int vorbis_book_encodev(codebook *book, int best,float *a, - oggpack_buffer *b); extern long vorbis_book_decode(codebook *book, oggpack_buffer *b); extern long vorbis_book_decodevs_add(codebook *book, float *a, diff --git a/src/filters/transform/MpaDecFilter/libvorbisidec/floor1.c b/src/filters/transform/MpaDecFilter/libvorbisidec/floor1.c index c031a2362..cebb40d75 100644 --- a/src/filters/transform/MpaDecFilter/libvorbisidec/floor1.c +++ b/src/filters/transform/MpaDecFilter/libvorbisidec/floor1.c @@ -11,7 +11,7 @@ ******************************************************************** function: floor backend 1 implementation - last mod: $Id: floor1.c 16227 2009-07-08 06:58:46Z xiphmont $ + last mod: $Id: floor1.c 17079 2010-03-26 06:51:41Z xiphmont $ ********************************************************************/ @@ -31,15 +31,22 @@ #define floor1_rangedB 140 /* floor 1 fixed at -140dB to 0dB range */ typedef struct lsfit_acc{ - long x0; - long x1; - - long xa; - long ya; - long x2a; - long y2a; - long xya; - long an; + int x0; + int x1; + + int xa; + int ya; + int x2a; + int y2a; + int xya; + int an; + + int xb; + int yb; + int x2b; + int y2b; + int xyb; + int bn; } lsfit_acc; /***********************************************/ @@ -381,7 +388,7 @@ static void render_line(int n, int x0,int x1,int y0,int y1,float *d){ } } -static void render_line0(int x0,int x1,int y0,int y1,int *d){ +static void render_line0(int n, int x0,int x1,int y0,int y1,int *d){ int dy=y1-y0; int adx=x1-x0; int ady=abs(dy); @@ -393,8 +400,12 @@ static void render_line0(int x0,int x1,int y0,int y1,int *d){ ady-=abs(base*adx); - d[x]=y; - while(++x<x1){ + if(n>x1)n=x1; + + if(x<n) + d[x]=y; + + while(++x<n){ err=err+ady; if(err>=adx){ err-=adx; @@ -412,7 +423,7 @@ static int accumulate_fit(const float *flr,const float *mdct, int n,vorbis_info_floor1 *info){ long i; - long xa=0,ya=0,x2a=0,y2a=0,xya=0,na=0, xb=0,yb=0,x2b=0,y2b=0,xyb=0,nb=0; + int xa=0,ya=0,x2a=0,y2a=0,xya=0,na=0, xb=0,yb=0,x2b=0,y2b=0,xyb=0,nb=0; memset(a,0,sizeof(*a)); a->x0=x0; @@ -440,72 +451,65 @@ static int accumulate_fit(const float *flr,const float *mdct, } } - xb+=xa; - yb+=ya; - x2b+=x2a; - y2b+=y2a; - xyb+=xya; - nb+=na; + a->xa=xa; + a->ya=ya; + a->x2a=x2a; + a->y2a=y2a; + a->xya=xya; + a->an=na; - /* weight toward the actually used frequencies if we meet the threshhold */ - { - int weight=nb*info->twofitweight/(na+1); - - a->xa=xa*weight+xb; - a->ya=ya*weight+yb; - a->x2a=x2a*weight+x2b; - a->y2a=y2a*weight+y2b; - a->xya=xya*weight+xyb; - a->an=na*weight+nb; - } + a->xb=xb; + a->yb=yb; + a->x2b=x2b; + a->y2b=y2b; + a->xyb=xyb; + a->bn=nb; return(na); } -static int fit_line(lsfit_acc *a,int fits,int *y0,int *y1){ - long x=0,y=0,x2=0,y2=0,xy=0,an=0,i; - long x0=a[0].x0; - long x1=a[fits-1].x1; +static int fit_line(lsfit_acc *a,int fits,int *y0,int *y1, + vorbis_info_floor1 *info){ + double xb=0,yb=0,x2b=0,y2b=0,xyb=0,bn=0; + int i; + int x0=a[0].x0; + int x1=a[fits-1].x1; for(i=0;i<fits;i++){ - x+=a[i].xa; - y+=a[i].ya; - x2+=a[i].x2a; - y2+=a[i].y2a; - xy+=a[i].xya; - an+=a[i].an; + double weight = (a[i].bn+a[i].an)*info->twofitweight/(a[i].an+1)+1.; + + xb+=a[i].xb + a[i].xa * weight; + yb+=a[i].yb + a[i].ya * weight; + x2b+=a[i].x2b + a[i].x2a * weight; + y2b+=a[i].y2b + a[i].y2a * weight; + xyb+=a[i].xyb + a[i].xya * weight; + bn+=a[i].bn + a[i].an * weight; } if(*y0>=0){ - x+= x0; - y+= *y0; - x2+= x0 * x0; - y2+= *y0 * *y0; - xy+= *y0 * x0; - an++; + xb+= x0; + yb+= *y0; + x2b+= x0 * x0; + y2b+= *y0 * *y0; + xyb+= *y0 * x0; + bn++; } if(*y1>=0){ - x+= x1; - y+= *y1; - x2+= x1 * x1; - y2+= *y1 * *y1; - xy+= *y1 * x1; - an++; + xb+= x1; + yb+= *y1; + x2b+= x1 * x1; + y2b+= *y1 * *y1; + xyb+= *y1 * x1; + bn++; } { - /* need 64 bit multiplies, which C doesn't give portably as int */ - double fx=x; - double fx2=x2; - double denom=(an*fx2-fx*fx); + double denom=(bn*x2b-xb*xb); if(denom>0.){ - double fy=y; - double fxy=xy; - - double a=(fy*fx2-fxy*fx)/denom; - double b=(an*fxy-fx*fy)/denom; + double a=(yb*x2b-xyb*xb)/denom; + double b=(bn*xyb-xb*yb)/denom; *y0=rint(a+b*x0); *y1=rint(a+b*x1); @@ -524,16 +528,6 @@ static int fit_line(lsfit_acc *a,int fits,int *y0,int *y1){ } } -/*static void fit_line_point(lsfit_acc *a,int fits,int *y0,int *y1){ - long y=0; - int i; - - for(i=0;i<fits && y==0;i++) - y+=a[i].ya; - - *y0=*y1=y; - }*/ - static int inspect_error(int x0,int x1,int y0,int y1,const float *mask, const float *mdct, vorbis_info_floor1 *info){ @@ -632,7 +626,7 @@ int *floor1_fit(vorbis_block *vb,vorbis_look_floor1 *look, /* start by fitting the implicit base case.... */ int y0=-200; int y1=-200; - fit_line(fits,posts-1,&y0,&y1); + fit_line(fits,posts-1,&y0,&y1,info); fit_valueA[0]=y0; fit_valueB[0]=y0; @@ -672,8 +666,8 @@ int *floor1_fit(vorbis_block *vb,vorbis_look_floor1 *look, int ly1=-200; int hy0=-200; int hy1=-200; - int ret0=fit_line(fits+lsortpos,sortpos-lsortpos,&ly0,&ly1); - int ret1=fit_line(fits+sortpos,hsortpos-sortpos,&hy0,&hy1); + int ret0=fit_line(fits+lsortpos,sortpos-lsortpos,&ly0,&ly1,info); + int ret1=fit_line(fits+sortpos,hsortpos-sortpos,&hy0,&hy1,info); if(ret0){ ly0=ly; @@ -945,6 +939,8 @@ int floor1_encode(oggpack_buffer *opb,vorbis_block *vb, int hx=0; int lx=0; int ly=post[0]*info->mult; + int n=ci->blocksizes[vb->W]/2; + for(j=1;j<look->posts;j++){ int current=look->forward_index[j]; int hy=post[current]&0x7fff; @@ -953,7 +949,7 @@ int floor1_encode(oggpack_buffer *opb,vorbis_block *vb, hy*=info->mult; hx=info->postlist[current]; - render_line0(lx,hx,ly,hy,ilogmask); + render_line0(n,lx,hx,ly,hy,ilogmask); lx=hx; ly=hy; diff --git a/src/filters/transform/MpaDecFilter/libvorbisidec/framing.c b/src/filters/transform/MpaDecFilter/libvorbisidec/framing.c index b8a6fb0c9..e56898abd 100644 --- a/src/filters/transform/MpaDecFilter/libvorbisidec/framing.c +++ b/src/filters/transform/MpaDecFilter/libvorbisidec/framing.c @@ -1,18 +1,18 @@ /******************************************************************** * * - * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. * + * THIS FILE IS PART OF THE Ogg CONTAINER SOURCE CODE. * * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * * * - * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2009 * + * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2010 * * by the Xiph.Org Foundation http://www.xiph.org/ * * * ******************************************************************** function: code raw packets into framed OggSquish stream and decode Ogg streams back into raw packets - last mod: $Id: framing.c 16051 2009-05-27 05:00:06Z xiphmont $ + last mod: $Id: framing.c 17124 2010-04-05 03:31:02Z gmaxwell $ note: The CRC code is directly derived from public domain code by Ross Williams (ross@guest.adelaide.edu.au). See docs/framing.html @@ -366,21 +366,10 @@ int ogg_stream_packetin(ogg_stream_state *os,ogg_packet *op){ return ogg_stream_iovecin(os, &iov, 1, op->e_o_s, op->granulepos); } -/* This will flush remaining packets into a page (returning nonzero), - even if there is not enough data to trigger a flush normally - (undersized page). If there are no packets or partial packets to - flush, ogg_stream_flush returns 0. Note that ogg_stream_flush will - try to flush a normal sized page like ogg_stream_pageout; a call to - ogg_stream_flush does not guarantee that all packets have flushed. - Only a return value of 0 from ogg_stream_flush indicates all packet - data is flushed into pages. - - since ogg_stream_flush will flush the last page in a stream even if - it's undersized, you almost certainly want to use ogg_stream_pageout - (and *not* ogg_stream_flush) unless you specifically need to flush - an page regardless of size in the middle of a stream. */ - -int ogg_stream_flush(ogg_stream_state *os,ogg_page *og){ +/* Conditionally flush a page; force==0 will only flush nominal-size + pages, force==1 forces us to flush a page regardless of page size + so long as there's any data available at all. */ +static int ogg_stream_flush_i(ogg_stream_state *os,ogg_page *og, int force, int nfill){ int i; int vals=0; int maxvals=(os->lacing_fill>255?255:os->lacing_fill); @@ -388,12 +377,12 @@ int ogg_stream_flush(ogg_stream_state *os,ogg_page *og){ long acc=0; ogg_int64_t granule_pos=-1; - if(ogg_stream_check(os)) return 0; - if(maxvals==0)return 0; - + if(ogg_stream_check(os)) return(0); + if(maxvals==0) return(0); + /* construct a page */ /* decide how many segments to include */ - + /* If this is the initial header case, the first page must only include the initial header packet */ if(os->b_o_s==0){ /* 'initial header page' case */ @@ -405,20 +394,41 @@ int ogg_stream_flush(ogg_stream_state *os,ogg_page *og){ } } }else{ + + /* The extra packets_done, packet_just_done logic here attempts to do two things: + 1) Don't unneccessarily span pages. + 2) Unless necessary, don't flush pages if there are less than four packets on + them; this expands page size to reduce unneccessary overhead if incoming packets + are large. + These are not necessary behaviors, just 'always better than naive flushing' + without requiring an application to explicitly request a specific optimized + behavior. We'll want an explicit behavior setup pathway eventually as well. */ + + int packets_done=0; + int packet_just_done=0; for(vals=0;vals<maxvals;vals++){ - if(acc>4096)break; + if(acc>nfill && packet_just_done>=8){ + force=1; + break; + } acc+=os->lacing_vals[vals]&0x0ff; - if((os->lacing_vals[vals]&0xff)<255) + if((os->lacing_vals[vals]&0xff)<255){ granule_pos=os->granule_vals[vals]; + packet_just_done=++packets_done; + }else + packet_just_done=0; } + if(vals==255)force=1; } - + + if(!force) return(0); + /* construct the header in temp storage */ memcpy(os->header,"OggS",4); - + /* stream structure version */ os->header[4]=0x00; - + /* continued packet flag? */ os->header[5]=0x00; if((os->lacing_vals[0]&0x100)==0)os->header[5]|=0x01; @@ -490,24 +500,52 @@ int ogg_stream_flush(ogg_stream_state *os,ogg_page *og){ return(1); } +/* This will flush remaining packets into a page (returning nonzero), + even if there is not enough data to trigger a flush normally + (undersized page). If there are no packets or partial packets to + flush, ogg_stream_flush returns 0. Note that ogg_stream_flush will + try to flush a normal sized page like ogg_stream_pageout; a call to + ogg_stream_flush does not guarantee that all packets have flushed. + Only a return value of 0 from ogg_stream_flush indicates all packet + data is flushed into pages. + + since ogg_stream_flush will flush the last page in a stream even if + it's undersized, you almost certainly want to use ogg_stream_pageout + (and *not* ogg_stream_flush) unless you specifically need to flush + an page regardless of size in the middle of a stream. */ + +int ogg_stream_flush(ogg_stream_state *os,ogg_page *og){ + return ogg_stream_flush_i(os,og,1,4096); +} /* This constructs pages from buffered packet segments. The pointers returned are to static buffers; do not free. The returned buffers are good only until the next call (using the same ogg_stream_state) */ int ogg_stream_pageout(ogg_stream_state *os, ogg_page *og){ + int force=0; if(ogg_stream_check(os)) return 0; if((os->e_o_s&&os->lacing_fill) || /* 'were done, now flush' case */ - os->body_fill-os->body_returned > 4096 ||/* 'page nominal size' case */ - os->lacing_fill>=255 || /* 'segment table full' case */ - (os->lacing_fill&&!os->b_o_s)){ /* 'initial header page' case */ - - return(ogg_stream_flush(os,og)); - } - - /* not enough data to construct a page and not end of stream */ - return 0; + (os->lacing_fill&&!os->b_o_s)) /* 'initial header page' case */ + force=1; + + return(ogg_stream_flush_i(os,og,force,4096)); +} + +/* Like the above, but an argument is provided to adjust the nominal +page size for applications which are smart enough to provide their +own delay based flushing */ + +int ogg_stream_pageout_fill(ogg_stream_state *os, ogg_page *og, int nfill){ + int force=0; + if(ogg_stream_check(os)) return 0; + + if((os->e_o_s&&os->lacing_fill) || /* 'were done, now flush' case */ + (os->lacing_fill&&!os->b_o_s)) /* 'initial header page' case */ + force=1; + + return(ogg_stream_flush_i(os,og,force,nfill)); } int ogg_stream_eos(ogg_stream_state *os){ @@ -984,17 +1022,17 @@ void ogg_packet_clear(ogg_packet *op) { ogg_stream_state os_en, os_de; ogg_sync_state oy; -void checkpacket(ogg_packet *op,int len, int no, int pos){ +void checkpacket(ogg_packet *op,long len, int no, long pos){ long j; static int sequence=0; static int lastno=0; if(op->bytes!=len){ - fprintf(stderr,"incorrect packet length!\n"); + fprintf(stderr,"incorrect packet length (%ld != %ld)!\n",op->bytes,len); exit(1); } if(op->granulepos!=pos){ - fprintf(stderr,"incorrect packet position!\n"); + fprintf(stderr,"incorrect packet granpos (%ld != %ld)!\n",(long)op->granulepos,pos); exit(1); } @@ -1161,18 +1199,73 @@ const int head1_4[] = {0x4f,0x67,0x67,0x53,0,0x02, const int head2_4[] = {0x4f,0x67,0x67,0x53,0,0x00, 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0x01,0x02,0x03,0x04,1,0,0,0, - 0x54,0x05,0x51,0xc8, - 17, + 0xf8,0x3c,0x19,0x79, + 255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255,255}; + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255}; const int head3_4[] = {0x4f,0x67,0x67,0x53,0,0x05, 0x07,0x0c,0x00,0x00,0x00,0x00,0x00,0x00, 0x01,0x02,0x03,0x04,2,0,0,0, - 0xc8,0xc3,0xcb,0xed, - 5, - 10,255,4,255,0}; - + 0x38,0xe6,0xb6,0x28, + 6, + 255,220,255,4,255,0}; + + +/* spill expansion test */ +const int head1_4b[] = {0x4f,0x67,0x67,0x53,0,0x02, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x01,0x02,0x03,0x04,0,0,0,0, + 0xff,0x7b,0x23,0x17, + 1, + 0}; + +const int head2_4b[] = {0x4f,0x67,0x67,0x53,0,0x00, + 0x07,0x20,0x00,0x00,0x00,0x00,0x00,0x00, + 0x01,0x02,0x03,0x04,1,0,0,0, + 0xe6,0x54,0xfe,0x7d, + 27, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,10,255,4,255,0,0,0,0,0,0}; + + +const int head3_4b[] = {0x4f,0x67,0x67,0x53,0,0x04, + 0x07,0x24,0x00,0x00,0x00,0x00,0x00,0x00, + 0x01,0x02,0x03,0x04,2,0,0,0, + 0x77,0x62,0xe0,0x12, + 1, + 0}; /* page with the 255 segment limit */ const int head1_5[] = {0x4f,0x67,0x67,0x53,0,0x02, @@ -1239,25 +1332,86 @@ const int head1_6[] = {0x4f,0x67,0x67,0x53,0,0x02, const int head2_6[] = {0x4f,0x67,0x67,0x53,0,0x00, 0x07,0x04,0x00,0x00,0x00,0x00,0x00,0x00, 0x01,0x02,0x03,0x04,1,0,0,0, - 0x3c,0xd9,0x4d,0x3f, - 17, - 100,255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255}; + 0x68,0x22,0x7c,0x3d, + 255, + 100, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255}; const int head3_6[] = {0x4f,0x67,0x67,0x53,0,0x01, 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0x01,0x02,0x03,0x04,2,0,0,0, - 0x01,0xd2,0xe5,0xe5, - 17, + 0xf4,0x87,0xba,0xf3, + 255, 255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255,255}; + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255}; const int head4_6[] = {0x4f,0x67,0x67,0x53,0,0x05, 0x07,0x10,0x00,0x00,0x00,0x00,0x00,0x00, 0x01,0x02,0x03,0x04,3,0,0,0, - 0xef,0xdd,0x88,0xde, - 7, - 255,255,75,255,4,255,0}; + 0xf7,0x2f,0x6c,0x60, + 5, + 254,255,4,255,0}; /* packet that overspans over an entire page */ const int head1_7[] = {0x4f,0x67,0x67,0x53,0,0x02, @@ -1270,18 +1424,50 @@ const int head1_7[] = {0x4f,0x67,0x67,0x53,0,0x02, const int head2_7[] = {0x4f,0x67,0x67,0x53,0,0x00, 0x07,0x04,0x00,0x00,0x00,0x00,0x00,0x00, 0x01,0x02,0x03,0x04,1,0,0,0, - 0x3c,0xd9,0x4d,0x3f, - 17, - 100,255,255,255,255,255,255,255,255, - 255,255,255,255,255,255,255,255}; + 0x68,0x22,0x7c,0x3d, + 255, + 100, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255,255,255, + 255,255,255,255,255,255}; const int head3_7[] = {0x4f,0x67,0x67,0x53,0,0x05, 0x07,0x08,0x00,0x00,0x00,0x00,0x00,0x00, 0x01,0x02,0x03,0x04,2,0,0,0, 0xd4,0xe0,0x60,0xe5, - 1,0}; + 1, + 0}; -void test_pack(const int *pl, const int **headers, int byteskip, +void test_pack(const int *pl, const int **headers, int byteskip, int pageskip, int packetskip){ unsigned char *data=_ogg_malloc(1024*1024); /* for scripted test cases only */ long inptr=0; @@ -1374,7 +1560,7 @@ void test_pack(const int *pl, const int **headers, int byteskip, if(ret<0)continue; /* got a page. Happy happy. Verify that it's good. */ - fprintf(stderr,"(%ld), ",pageout); + fprintf(stderr,"(%d), ",pageout); check_page(data+deptr,headers[pageout],&og_de); deptr+=og_de.body_len; @@ -1473,7 +1659,7 @@ int main(void){ /* 17 only */ const int packets[]={17, -1}; const int *headret[]={head1_0,NULL}; - + fprintf(stderr,"testing single page encoding... "); test_pack(packets,headret,0,0,0); } @@ -1482,7 +1668,7 @@ int main(void){ /* 17, 254, 255, 256, 500, 510, 600 byte, pad */ const int packets[]={17, 254, 255, 256, 500, 510, 600, -1}; const int *headret[]={head1_1,head2_1,NULL}; - + fprintf(stderr,"testing basic page encoding... "); test_pack(packets,headret,0,0,0); } @@ -1491,7 +1677,7 @@ int main(void){ /* nil packets; beginning,middle,end */ const int packets[]={0,17, 254, 255, 0, 256, 0, 500, 510, 600, 0, -1}; const int *headret[]={head1_2,head2_2,NULL}; - + fprintf(stderr,"testing basic nil packets... "); test_pack(packets,headret,0,0,0); } @@ -1500,20 +1686,30 @@ int main(void){ /* large initial packet */ const int packets[]={4345,259,255,-1}; const int *headret[]={head1_3,head2_3,NULL}; - + fprintf(stderr,"testing initial-packet lacing > 4k... "); test_pack(packets,headret,0,0,0); } { - /* continuing packet test */ - const int packets[]={0,4345,259,255,-1}; + /* continuing packet test; with page spill expansion, we have to + overflow the lacing table. */ + const int packets[]={0,65500,259,255,-1}; const int *headret[]={head1_4,head2_4,head3_4,NULL}; - + fprintf(stderr,"testing single packet page span... "); test_pack(packets,headret,0,0,0); } + { + /* spill expand packet test */ + const int packets[]={0,4345,259,255,0,0,0,0,0,0,-1}; + const int *headret[]={head1_4b,head2_4b,head3_4b,NULL}; + + fprintf(stderr,"testing page spill expansion... "); + test_pack(packets,headret,0,0,0); + } + /* page with the 255 segment limit */ { @@ -1557,7 +1753,7 @@ int main(void){ { /* packet that overspans over an entire page */ - const int packets[]={0,100,9000,259,255,-1}; + const int packets[]={0,100,130049,259,255,-1}; const int *headret[]={head1_6,head2_6,head3_6,head4_6,NULL}; fprintf(stderr,"testing very large packets... "); @@ -1567,7 +1763,7 @@ int main(void){ { /* test for the libogg 1.1.1 resync in large continuation bug found by Josh Coalson) */ - const int packets[]={0,100,9000,259,255,-1}; + const int packets[]={0,100,130049,259,255,-1}; const int *headret[]={head1_6,head2_6,head3_6,head4_6,NULL}; fprintf(stderr,"testing continuation resync in very large packets... "); @@ -1576,7 +1772,7 @@ int main(void){ { /* term only page. why not? */ - const int packets[]={0,100,4080,-1}; + const int packets[]={0,100,64770,-1}; const int *headret[]={head1_7,head2_7,head3_7,NULL}; fprintf(stderr,"testing zero data page (1 nil packet)... "); @@ -1588,7 +1784,7 @@ int main(void){ { /* build a bunch of pages for testing */ unsigned char *data=_ogg_malloc(1024*1024); - int pl[]={0,100,4079,2956,2057,76,34,912,0,234,1000,1000,1000,300,-1}; + int pl[]={0, 0,0,0,0,1,1,98,4079, 0,0,0,0,1,1,2954,2057, 0,0,0,0,76,34,912,0,234,1000,1000, 1000,300,-1}; int inptr=0,i,j; ogg_page og[5]; @@ -1649,17 +1845,37 @@ int main(void){ if(ogg_stream_packetout(&os_de,&test)!=1)error(); checkpacket(&test,0,0,0); if(ogg_stream_packetout(&os_de,&test)!=1)error(); - checkpacket(&test,100,1,-1); + checkpacket(&test,0,1,-1); + if(ogg_stream_packetout(&os_de,&test)!=1)error(); + checkpacket(&test,0,2,-1); + if(ogg_stream_packetout(&os_de,&test)!=1)error(); + checkpacket(&test,0,3,-1); + if(ogg_stream_packetout(&os_de,&test)!=1)error(); + checkpacket(&test,0,4,-1); + if(ogg_stream_packetout(&os_de,&test)!=1)error(); + checkpacket(&test,1,5,-1); + if(ogg_stream_packetout(&os_de,&test)!=1)error(); + checkpacket(&test,1,6,-1); if(ogg_stream_packetout(&os_de,&test)!=1)error(); - checkpacket(&test,4079,2,3000); + checkpacket(&test,98,7,-1); + if(ogg_stream_packetout(&os_de,&test)!=1)error(); + checkpacket(&test,4079,8,9000); if(ogg_stream_packetout(&os_de,&test)!=-1){ fprintf(stderr,"Error: loss of page did not return error\n"); exit(1); } if(ogg_stream_packetout(&os_de,&test)!=1)error(); - checkpacket(&test,76,5,-1); + checkpacket(&test,0,17,-1); + if(ogg_stream_packetout(&os_de,&test)!=1)error(); + checkpacket(&test,0,18,-1); + if(ogg_stream_packetout(&os_de,&test)!=1)error(); + checkpacket(&test,0,19,-1); + if(ogg_stream_packetout(&os_de,&test)!=1)error(); + checkpacket(&test,0,20,-1); if(ogg_stream_packetout(&os_de,&test)!=1)error(); - checkpacket(&test,34,6,-1); + checkpacket(&test,76,21,-1); + if(ogg_stream_packetout(&os_de,&test)!=1)error(); + checkpacket(&test,34,22,-1); fprintf(stderr,"ok.\n"); } @@ -1692,24 +1908,50 @@ int main(void){ ogg_stream_pagein(&os_de,&temp); /* do we get the expected results/packets? */ - + if(ogg_stream_packetout(&os_de,&test)!=1)error(); checkpacket(&test,0,0,0); if(ogg_stream_packetout(&os_de,&test)!=1)error(); - checkpacket(&test,100,1,-1); + checkpacket(&test,0,1,-1); if(ogg_stream_packetout(&os_de,&test)!=1)error(); - checkpacket(&test,4079,2,3000); + checkpacket(&test,0,2,-1); if(ogg_stream_packetout(&os_de,&test)!=1)error(); - checkpacket(&test,2956,3,4000); + checkpacket(&test,0,3,-1); + if(ogg_stream_packetout(&os_de,&test)!=1)error(); + checkpacket(&test,0,4,-1); + if(ogg_stream_packetout(&os_de,&test)!=1)error(); + checkpacket(&test,1,5,-1); + if(ogg_stream_packetout(&os_de,&test)!=1)error(); + checkpacket(&test,1,6,-1); + if(ogg_stream_packetout(&os_de,&test)!=1)error(); + checkpacket(&test,98,7,-1); + if(ogg_stream_packetout(&os_de,&test)!=1)error(); + checkpacket(&test,4079,8,9000); + if(ogg_stream_packetout(&os_de,&test)!=1)error(); + checkpacket(&test,0,9,-1); + if(ogg_stream_packetout(&os_de,&test)!=1)error(); + checkpacket(&test,0,10,-1); + if(ogg_stream_packetout(&os_de,&test)!=1)error(); + checkpacket(&test,0,11,-1); + if(ogg_stream_packetout(&os_de,&test)!=1)error(); + checkpacket(&test,0,12,-1); + if(ogg_stream_packetout(&os_de,&test)!=1)error(); + checkpacket(&test,1,13,-1); + if(ogg_stream_packetout(&os_de,&test)!=1)error(); + checkpacket(&test,1,14,-1); + if(ogg_stream_packetout(&os_de,&test)!=1)error(); + checkpacket(&test,2954,15,-1); + if(ogg_stream_packetout(&os_de,&test)!=1)error(); + checkpacket(&test,2057,16,17000); if(ogg_stream_packetout(&os_de,&test)!=-1){ fprintf(stderr,"Error: loss of page did not return error\n"); exit(1); } if(ogg_stream_packetout(&os_de,&test)!=1)error(); - checkpacket(&test,300,13,14000); + checkpacket(&test,300,29,30000); fprintf(stderr,"ok.\n"); } - + /* the rest only test sync */ { ogg_page og_de; @@ -1720,35 +1962,35 @@ int main(void){ 3); ogg_sync_wrote(&oy,3); if(ogg_sync_pageout(&oy,&og_de)>0)error(); - + /* Test fractional page inputs: incomplete fixed header */ memcpy(ogg_sync_buffer(&oy,og[1].header_len),og[1].header+3, 20); ogg_sync_wrote(&oy,20); if(ogg_sync_pageout(&oy,&og_de)>0)error(); - + /* Test fractional page inputs: incomplete header */ memcpy(ogg_sync_buffer(&oy,og[1].header_len),og[1].header+23, 5); ogg_sync_wrote(&oy,5); if(ogg_sync_pageout(&oy,&og_de)>0)error(); - + /* Test fractional page inputs: incomplete body */ - + memcpy(ogg_sync_buffer(&oy,og[1].header_len),og[1].header+28, og[1].header_len-28); ogg_sync_wrote(&oy,og[1].header_len-28); if(ogg_sync_pageout(&oy,&og_de)>0)error(); - + memcpy(ogg_sync_buffer(&oy,og[1].body_len),og[1].body,1000); ogg_sync_wrote(&oy,1000); if(ogg_sync_pageout(&oy,&og_de)>0)error(); - + memcpy(ogg_sync_buffer(&oy,og[1].body_len),og[1].body+1000, og[1].body_len-1000); ogg_sync_wrote(&oy,og[1].body_len-1000); if(ogg_sync_pageout(&oy,&og_de)<=0)error(); - + fprintf(stderr,"ok.\n"); } @@ -1756,7 +1998,7 @@ int main(void){ { ogg_page og_de; fprintf(stderr,"Testing sync on 1+partial inputs... "); - ogg_sync_reset(&oy); + ogg_sync_reset(&oy); memcpy(ogg_sync_buffer(&oy,og[1].header_len),og[1].header, og[1].header_len); diff --git a/src/filters/transform/MpaDecFilter/libvorbisidec/highlevel.h b/src/filters/transform/MpaDecFilter/libvorbisidec/highlevel.h index 2a856df25..07cba61f2 100644 --- a/src/filters/transform/MpaDecFilter/libvorbisidec/highlevel.h +++ b/src/filters/transform/MpaDecFilter/libvorbisidec/highlevel.h @@ -11,7 +11,7 @@ ******************************************************************** function: highlevel encoder setup struct seperated out for vorbisenc clarity - last mod: $Id: highlevel.h 16227 2009-07-08 06:58:46Z xiphmont $ + last mod: $Id: highlevel.h 16995 2010-03-23 03:44:44Z xiphmont $ ********************************************************************/ @@ -23,14 +23,14 @@ typedef struct highlevel_byblocktype { } highlevel_byblocktype; typedef struct highlevel_encode_setup { - const void *setup; int set_in_stone; - + const void *setup; double base_setting; - double long_setting; - double short_setting; + double impulse_noisetune; + /* bitrate management below all settable */ + float req; int managed; long bitrate_min; long bitrate_av; @@ -41,9 +41,11 @@ typedef struct highlevel_encode_setup { int impulse_block_p; int noise_normalize_p; + int coupling_p; double stereo_point_setting; double lowpass_kHz; + int lowpass_altered; double ath_floating_dB; double ath_absolute_dB; diff --git a/src/filters/transform/MpaDecFilter/libvorbisidec/info.c b/src/filters/transform/MpaDecFilter/libvorbisidec/info.c index 30088d8f0..e80915a12 100644 --- a/src/filters/transform/MpaDecFilter/libvorbisidec/info.c +++ b/src/filters/transform/MpaDecFilter/libvorbisidec/info.c @@ -5,13 +5,13 @@ * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * * * - * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2009 * + * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2010 * * by the Xiph.Org Foundation http://www.xiph.org/ * * * ******************************************************************** function: maintain the info structure, info <-> header packets - last mod: $Id: info.c 16243 2009-07-10 02:49:31Z xiphmont $ + last mod: $Id: info.c 17080 2010-03-26 06:59:58Z xiphmont $ ********************************************************************/ @@ -31,8 +31,8 @@ #include "misc.h" #include "os.h" -#define GENERAL_VENDOR_STRING "Xiph.Org libVorbis 1.2.3" -#define ENCODE_VENDOR_STRING "Xiph.Org libVorbis I 20090709" +#define GENERAL_VENDOR_STRING "Xiph.Org libVorbis 1.3.1" +#define ENCODE_VENDOR_STRING "Xiph.Org libVorbis I 20100325 (Everywhere)" /* helpers */ static int ilog2(unsigned int v){ @@ -278,8 +278,8 @@ static int _vorbis_unpack_books(vorbis_info *vi,oggpack_buffer *opb){ ci->books=oggpack_read(opb,8)+1; if(ci->books<=0)goto err_out; for(i=0;i<ci->books;i++){ - ci->book_param[i]=_ogg_calloc(1,sizeof(*ci->book_param[i])); - if(vorbis_staticbook_unpack(opb,ci->book_param[i]))goto err_out; + ci->book_param[i]=vorbis_staticbook_unpack(opb); + if(!ci->book_param[i])goto err_out; } /* time backend settings; hooks are unused */ @@ -628,12 +628,12 @@ int vorbis_analysis_headerout(vorbis_dsp_state *v, oggpack_writeclear(&opb); return(0); err_out: - oggpack_writeclear(&opb); memset(op,0,sizeof(*op)); memset(op_comm,0,sizeof(*op_comm)); memset(op_code,0,sizeof(*op_code)); if(b){ + oggpack_writeclear(&opb); if(b->header)_ogg_free(b->header); if(b->header1)_ogg_free(b->header1); if(b->header2)_ogg_free(b->header2); diff --git a/src/filters/transform/MpaDecFilter/libvorbisidec/libvorbisidec.vcproj b/src/filters/transform/MpaDecFilter/libvorbisidec/libvorbisidec.vcproj index b09a5358e..c43ee0c05 100644 --- a/src/filters/transform/MpaDecFilter/libvorbisidec/libvorbisidec.vcproj +++ b/src/filters/transform/MpaDecFilter/libvorbisidec/libvorbisidec.vcproj @@ -297,6 +297,14 @@ Filter="c"
>
<File
+ RelativePath=".\analysis.c"
+ >
+ </File>
+ <File
+ RelativePath=".\barkmel.c"
+ >
+ </File>
+ <File
RelativePath=".\bitrate.c"
>
</File>
@@ -377,6 +385,14 @@ >
</File>
<File
+ RelativePath=".\tone.c"
+ >
+ </File>
+ <File
+ RelativePath=".\vorbisfile.c"
+ >
+ </File>
+ <File
RelativePath=".\window.c"
>
</File>
@@ -462,6 +478,10 @@ >
</File>
<File
+ RelativePath=".\vorbis\vorbisfile.h"
+ >
+ </File>
+ <File
RelativePath=".\window.h"
>
</File>
diff --git a/src/filters/transform/MpaDecFilter/libvorbisidec/mapping0.c b/src/filters/transform/MpaDecFilter/libvorbisidec/mapping0.c index 716fa4cd8..7d279a857 100644 --- a/src/filters/transform/MpaDecFilter/libvorbisidec/mapping0.c +++ b/src/filters/transform/MpaDecFilter/libvorbisidec/mapping0.c @@ -5,13 +5,13 @@ * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * * * - * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2009 * + * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2010 * * by the Xiph.Org Foundation http://www.xiph.org/ * * * ******************************************************************** function: channel mapping 0 implementation - last mod: $Id: mapping0.c 16227 2009-07-08 06:58:46Z xiphmont $ + last mod: $Id: mapping0.c 17022 2010-03-25 03:45:42Z xiphmont $ ********************************************************************/ @@ -246,7 +246,7 @@ static int mapping0_forward(vorbis_block *vb){ int *nonzero = alloca(sizeof(*nonzero)*vi->channels); float **gmdct = _vorbis_block_alloc(vb,vi->channels*sizeof(*gmdct)); - int **ilogmaskch= _vorbis_block_alloc(vb,vi->channels*sizeof(*ilogmaskch)); + int **iwork = _vorbis_block_alloc(vb,vi->channels*sizeof(*iwork)); int ***floor_posts = _vorbis_block_alloc(vb,vi->channels*sizeof(*floor_posts)); float global_ampmax=vbi->ampmax; @@ -255,8 +255,7 @@ static int mapping0_forward(vorbis_block *vb){ int modenumber=vb->W; vorbis_info_mapping0 *info=ci->map_param[modenumber]; - vorbis_look_psy *psy_look= - b->psy+blocktype+(vb->W?2:0); + vorbis_look_psy *psy_look=b->psy+blocktype+(vb->W?2:0); vb->mode=modenumber; @@ -267,6 +266,7 @@ static int mapping0_forward(vorbis_block *vb){ float *pcm =vb->pcm[i]; float *logfft =pcm; + iwork[i]=_vorbis_block_alloc(vb,n/2*sizeof(**iwork)); gmdct[i]=_vorbis_block_alloc(vb,n/2*sizeof(**gmdct)); scale_dB=todB(&scale) + .345; /* + .345 is a hack; the original @@ -598,39 +598,8 @@ static int mapping0_forward(vorbis_block *vb){ /* iterate over the many masking curve fits we've created */ { - float **res_bundle=alloca(sizeof(*res_bundle)*vi->channels); - float **couple_bundle=alloca(sizeof(*couple_bundle)*vi->channels); + int **couple_bundle=alloca(sizeof(*couple_bundle)*vi->channels); int *zerobundle=alloca(sizeof(*zerobundle)*vi->channels); - int **sortindex=alloca(sizeof(*sortindex)*vi->channels); - float **mag_memo=NULL; - int **mag_sort=NULL; - - if(info->coupling_steps){ - mag_memo=_vp_quantize_couple_memo(vb, - &ci->psy_g_param, - psy_look, - info, - gmdct); - - mag_sort=_vp_quantize_couple_sort(vb, - psy_look, - info, - mag_memo); - - hf_reduction(&ci->psy_g_param, - psy_look, - info, - mag_memo); - } - - memset(sortindex,0,sizeof(*sortindex)*vi->channels); - if(psy_look->vi->normal_channel_p){ - for(i=0;i<vi->channels;i++){ - float *mdct =gmdct[i]; - sortindex[i]=alloca(sizeof(**sortindex)*n/2); - _vp_noise_normalize_sort(psy_look,mdct,sortindex[i]); - } - } for(k=(vorbis_bitrate_managed(vb)?0:PACKETBLOBS/2); k<=(vorbis_bitrate_managed(vb)?PACKETBLOBS-1:PACKETBLOBS/2); @@ -651,10 +620,7 @@ static int mapping0_forward(vorbis_block *vb){ /* encode floor, compute masking curve, sep out residue */ for(i=0;i<vi->channels;i++){ int submap=info->chmuxlist[i]; - float *mdct =gmdct[i]; - float *res =vb->pcm[i]; - int *ilogmask=ilogmaskch[i]= - _vorbis_block_alloc(vb,n/2*sizeof(**gmdct)); + int *ilogmask=iwork[i]; nonzero[i]=floor1_encode(opb,vb,b->flr[info->floorsubmap[submap]], floor_posts[i][k], @@ -665,30 +631,10 @@ static int mapping0_forward(vorbis_block *vb){ sprintf(buf,"maskI%c%d",i?'R':'L',k); float work[n/2]; for(j=0;j<n/2;j++) - work[j]=FLOOR1_fromdB_LOOKUP[ilogmask[j]]; + work[j]=FLOOR1_fromdB_LOOKUP[iwork[i][j]]; _analysis_output(buf,seq,work,n/2,1,1,0); } #endif - _vp_remove_floor(psy_look, - mdct, - ilogmask, - res, - ci->psy_g_param.sliding_lowpass[vb->W][k]); - - _vp_noise_normalize(psy_look,res,res+n/2,sortindex[i]); - - -#if 0 - { - char buf[80]; - float work[n/2]; - for(j=0;j<n/2;j++) - work[j]=FLOOR1_fromdB_LOOKUP[ilogmask[j]]*(res+n/2)[j]; - sprintf(buf,"resI%c%d",i?'R':'L',k); - _analysis_output(buf,seq,work,n/2,1,1,0); - - } -#endif } /* our iteration is now based on masking curve, not prequant and @@ -697,18 +643,26 @@ static int mapping0_forward(vorbis_block *vb){ /* quantize/couple */ /* incomplete implementation that assumes the tree is all depth one, or no tree at all */ - if(info->coupling_steps){ - _vp_couple(k, - &ci->psy_g_param, - psy_look, - info, - vb->pcm, - mag_memo, - mag_sort, - ilogmaskch, - nonzero, - ci->psy_g_param.sliding_lowpass[vb->W][k]); + _vp_couple_quantize_normalize(k, + &ci->psy_g_param, + psy_look, + info, + gmdct, + iwork, + nonzero, + ci->psy_g_param.sliding_lowpass[vb->W][k], + vi->channels); + +#if 0 + for(i=0;i<vi->channels;i++){ + char buf[80]; + sprintf(buf,"res%c%d",i?'R':'L',k); + float work[n/2]; + for(j=0;j<n/2;j++) + work[j]=iwork[i][j]; + _analysis_output(buf,seq,work,n/2,1,0,0); } +#endif /* classify and encode by submap */ for(i=0;i<info->submaps;i++){ @@ -720,25 +674,21 @@ static int mapping0_forward(vorbis_block *vb){ if(info->chmuxlist[j]==i){ zerobundle[ch_in_bundle]=0; if(nonzero[j])zerobundle[ch_in_bundle]=1; - res_bundle[ch_in_bundle]=vb->pcm[j]; - couple_bundle[ch_in_bundle++]=vb->pcm[j]+n/2; + couple_bundle[ch_in_bundle++]=iwork[j]; } } classifications=_residue_P[ci->residue_type[resnum]]-> class(vb,b->residue[resnum],couple_bundle,zerobundle,ch_in_bundle); - /* couple_bundle is destructively overwritten by - the class function if some but not all of the channels are - marked as silence; build a fresh copy */ ch_in_bundle=0; for(j=0;j<vi->channels;j++) if(info->chmuxlist[j]==i) - couple_bundle[ch_in_bundle++]=vb->pcm[j]+n/2; + couple_bundle[ch_in_bundle++]=iwork[j]; _residue_P[ci->residue_type[resnum]]-> forward(opb,vb,b->residue[resnum], - couple_bundle,NULL,zerobundle,ch_in_bundle,classifications); + couple_bundle,zerobundle,ch_in_bundle,classifications,i); } /* ok, done encoding. Next protopacket. */ diff --git a/src/filters/transform/MpaDecFilter/libvorbisidec/psy.c b/src/filters/transform/MpaDecFilter/libvorbisidec/psy.c index 3ba6c4454..4a8e81eb9 100644 --- a/src/filters/transform/MpaDecFilter/libvorbisidec/psy.c +++ b/src/filters/transform/MpaDecFilter/libvorbisidec/psy.c @@ -5,13 +5,13 @@ * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * * * - * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2009 * + * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2010 * * by the Xiph.Org Foundation http://www.xiph.org/ * * * ******************************************************************** function: psychoacoustics not including preecho - last mod: $Id: psy.c 16227 2009-07-08 06:58:46Z xiphmont $ + last mod: $Id: psy.c 17077 2010-03-26 06:22:19Z xiphmont $ ********************************************************************/ @@ -696,92 +696,6 @@ static void bark_noise_hybridmp(int n,const long *b, } } -static const float FLOOR1_fromdB_INV_LOOKUP[256]={ - 0.F, 8.81683e+06F, 8.27882e+06F, 7.77365e+06F, - 7.29930e+06F, 6.85389e+06F, 6.43567e+06F, 6.04296e+06F, - 5.67422e+06F, 5.32798e+06F, 5.00286e+06F, 4.69759e+06F, - 4.41094e+06F, 4.14178e+06F, 3.88905e+06F, 3.65174e+06F, - 3.42891e+06F, 3.21968e+06F, 3.02321e+06F, 2.83873e+06F, - 2.66551e+06F, 2.50286e+06F, 2.35014e+06F, 2.20673e+06F, - 2.07208e+06F, 1.94564e+06F, 1.82692e+06F, 1.71544e+06F, - 1.61076e+06F, 1.51247e+06F, 1.42018e+06F, 1.33352e+06F, - 1.25215e+06F, 1.17574e+06F, 1.10400e+06F, 1.03663e+06F, - 973377.F, 913981.F, 858210.F, 805842.F, - 756669.F, 710497.F, 667142.F, 626433.F, - 588208.F, 552316.F, 518613.F, 486967.F, - 457252.F, 429351.F, 403152.F, 378551.F, - 355452.F, 333762.F, 313396.F, 294273.F, - 276316.F, 259455.F, 243623.F, 228757.F, - 214798.F, 201691.F, 189384.F, 177828.F, - 166977.F, 156788.F, 147221.F, 138237.F, - 129802.F, 121881.F, 114444.F, 107461.F, - 100903.F, 94746.3F, 88964.9F, 83536.2F, - 78438.8F, 73652.5F, 69158.2F, 64938.1F, - 60975.6F, 57254.9F, 53761.2F, 50480.6F, - 47400.3F, 44507.9F, 41792.0F, 39241.9F, - 36847.3F, 34598.9F, 32487.7F, 30505.3F, - 28643.8F, 26896.0F, 25254.8F, 23713.7F, - 22266.7F, 20908.0F, 19632.2F, 18434.2F, - 17309.4F, 16253.1F, 15261.4F, 14330.1F, - 13455.7F, 12634.6F, 11863.7F, 11139.7F, - 10460.0F, 9821.72F, 9222.39F, 8659.64F, - 8131.23F, 7635.06F, 7169.17F, 6731.70F, - 6320.93F, 5935.23F, 5573.06F, 5232.99F, - 4913.67F, 4613.84F, 4332.30F, 4067.94F, - 3819.72F, 3586.64F, 3367.78F, 3162.28F, - 2969.31F, 2788.13F, 2617.99F, 2458.24F, - 2308.24F, 2167.39F, 2035.14F, 1910.95F, - 1794.35F, 1684.85F, 1582.04F, 1485.51F, - 1394.86F, 1309.75F, 1229.83F, 1154.78F, - 1084.32F, 1018.15F, 956.024F, 897.687F, - 842.910F, 791.475F, 743.179F, 697.830F, - 655.249F, 615.265F, 577.722F, 542.469F, - 509.367F, 478.286F, 449.101F, 421.696F, - 395.964F, 371.803F, 349.115F, 327.812F, - 307.809F, 289.026F, 271.390F, 254.830F, - 239.280F, 224.679F, 210.969F, 198.096F, - 186.008F, 174.658F, 164.000F, 153.993F, - 144.596F, 135.773F, 127.488F, 119.708F, - 112.404F, 105.545F, 99.1046F, 93.0572F, - 87.3788F, 82.0469F, 77.0404F, 72.3394F, - 67.9252F, 63.7804F, 59.8885F, 56.2341F, - 52.8027F, 49.5807F, 46.5553F, 43.7144F, - 41.0470F, 38.5423F, 36.1904F, 33.9821F, - 31.9085F, 29.9614F, 28.1332F, 26.4165F, - 24.8045F, 23.2910F, 21.8697F, 20.5352F, - 19.2822F, 18.1056F, 17.0008F, 15.9634F, - 14.9893F, 14.0746F, 13.2158F, 12.4094F, - 11.6522F, 10.9411F, 10.2735F, 9.64662F, - 9.05798F, 8.50526F, 7.98626F, 7.49894F, - 7.04135F, 6.61169F, 6.20824F, 5.82941F, - 5.47370F, 5.13970F, 4.82607F, 4.53158F, - 4.25507F, 3.99542F, 3.75162F, 3.52269F, - 3.30774F, 3.10590F, 2.91638F, 2.73842F, - 2.57132F, 2.41442F, 2.26709F, 2.12875F, - 1.99885F, 1.87688F, 1.76236F, 1.65482F, - 1.55384F, 1.45902F, 1.36999F, 1.28640F, - 1.20790F, 1.13419F, 1.06499F, 1.F -}; - -void _vp_remove_floor(vorbis_look_psy *p, - float *mdct, - int *codedflr, - float *residue, - int sliding_lowpass){ - - int i,n=p->n; - - if(sliding_lowpass>n)sliding_lowpass=n; - - for(i=0;i<sliding_lowpass;i++){ - residue[i]= - mdct[i]*FLOOR1_fromdB_INV_LOOKUP[codedflr[i]]; - } - - for(;i<n;i++) - residue[i]=0.; -} - void _vp_noisemask(vorbis_look_psy *p, float *logmdct, float *logmask){ @@ -926,305 +840,356 @@ float _vp_ampmax_decay(float amp,vorbis_dsp_state *vd){ return(amp); } -static void couple_lossless(float A, float B, - float *qA, float *qB){ - int test1=fabs(*qA)>fabs(*qB); - test1-= fabs(*qA)<fabs(*qB); - - if(!test1)test1=((fabs(A)>fabs(B))<<1)-1; - if(test1==1){ - *qB=(*qA>0.f?*qA-*qB:*qB-*qA); - }else{ - float temp=*qB; - *qB=(*qB>0.f?*qA-*qB:*qB-*qA); - *qA=temp; - } - - if(*qB>fabs(*qA)*1.9999f){ - *qB= -fabs(*qA)*2.f; - *qA= -*qA; - } -} - -static const float hypot_lookup[32]={ - -0.009935, -0.011245, -0.012726, -0.014397, - -0.016282, -0.018407, -0.020800, -0.023494, - -0.026522, -0.029923, -0.033737, -0.038010, - -0.042787, -0.048121, -0.054064, -0.060671, - -0.068000, -0.076109, -0.085054, -0.094892, - -0.105675, -0.117451, -0.130260, -0.144134, - -0.159093, -0.175146, -0.192286, -0.210490, - -0.229718, -0.249913, -0.271001, -0.292893}; - -static void precomputed_couple_point(float premag, - int floorA,int floorB, - float *mag, float *ang){ - - int test=(floorA>floorB)-1; - int offset=31-abs(floorA-floorB); - float floormag=hypot_lookup[((offset<0)-1)&offset]+1.f; - - floormag*=FLOOR1_fromdB_INV_LOOKUP[(floorB&test)|(floorA&(~test))]; - - *mag=premag*floormag; - *ang=0.f; -} - -/* just like below, this is currently set up to only do - single-step-depth coupling. Otherwise, we'd have to do more - copying (which will be inevitable later) */ - -/* doing the real circular magnitude calculation is audibly superior - to (A+B)/sqrt(2) */ -static float dipole_hypot(float a, float b){ - if(a>0.){ - if(b>0.)return sqrt(a*a+b*b); - if(a>-b)return sqrt(a*a-b*b); - return -sqrt(b*b-a*a); - } - if(b<0.)return -sqrt(a*a+b*b); - if(-a>b)return -sqrt(a*a-b*b); - return sqrt(b*b-a*a); -} -static float round_hypot(float a, float b){ - if(a>0.){ - if(b>0.)return sqrt(a*a+b*b); - if(a>-b)return sqrt(a*a+b*b); - return -sqrt(b*b+a*a); - } - if(b<0.)return -sqrt(a*a+b*b); - if(-a>b)return -sqrt(a*a+b*b); - return sqrt(b*b+a*a); -} - -/* revert to round hypot for now */ -float **_vp_quantize_couple_memo(vorbis_block *vb, - vorbis_info_psy_global *g, - vorbis_look_psy *p, - vorbis_info_mapping0 *vi, - float **mdct){ - - int i,j,n=p->n; - float **ret=_vorbis_block_alloc(vb,vi->coupling_steps*sizeof(*ret)); - int limit=g->coupling_pointlimit[p->vi->blockflag][PACKETBLOBS/2]; - - for(i=0;i<vi->coupling_steps;i++){ - float *mdctM=mdct[vi->coupling_mag[i]]; - float *mdctA=mdct[vi->coupling_ang[i]]; - ret[i]=_vorbis_block_alloc(vb,n*sizeof(**ret)); - for(j=0;j<limit;j++) - ret[i][j]=dipole_hypot(mdctM[j],mdctA[j]); - for(;j<n;j++) - ret[i][j]=round_hypot(mdctM[j],mdctA[j]); - } - - return(ret); -} +static float FLOOR1_fromdB_LOOKUP[256]={ + 1.0649863e-07F, 1.1341951e-07F, 1.2079015e-07F, 1.2863978e-07F, + 1.3699951e-07F, 1.4590251e-07F, 1.5538408e-07F, 1.6548181e-07F, + 1.7623575e-07F, 1.8768855e-07F, 1.9988561e-07F, 2.128753e-07F, + 2.2670913e-07F, 2.4144197e-07F, 2.5713223e-07F, 2.7384213e-07F, + 2.9163793e-07F, 3.1059021e-07F, 3.3077411e-07F, 3.5226968e-07F, + 3.7516214e-07F, 3.9954229e-07F, 4.2550680e-07F, 4.5315863e-07F, + 4.8260743e-07F, 5.1396998e-07F, 5.4737065e-07F, 5.8294187e-07F, + 6.2082472e-07F, 6.6116941e-07F, 7.0413592e-07F, 7.4989464e-07F, + 7.9862701e-07F, 8.5052630e-07F, 9.0579828e-07F, 9.6466216e-07F, + 1.0273513e-06F, 1.0941144e-06F, 1.1652161e-06F, 1.2409384e-06F, + 1.3215816e-06F, 1.4074654e-06F, 1.4989305e-06F, 1.5963394e-06F, + 1.7000785e-06F, 1.8105592e-06F, 1.9282195e-06F, 2.0535261e-06F, + 2.1869758e-06F, 2.3290978e-06F, 2.4804557e-06F, 2.6416497e-06F, + 2.8133190e-06F, 2.9961443e-06F, 3.1908506e-06F, 3.3982101e-06F, + 3.6190449e-06F, 3.8542308e-06F, 4.1047004e-06F, 4.3714470e-06F, + 4.6555282e-06F, 4.9580707e-06F, 5.2802740e-06F, 5.6234160e-06F, + 5.9888572e-06F, 6.3780469e-06F, 6.7925283e-06F, 7.2339451e-06F, + 7.7040476e-06F, 8.2047000e-06F, 8.7378876e-06F, 9.3057248e-06F, + 9.9104632e-06F, 1.0554501e-05F, 1.1240392e-05F, 1.1970856e-05F, + 1.2748789e-05F, 1.3577278e-05F, 1.4459606e-05F, 1.5399272e-05F, + 1.6400004e-05F, 1.7465768e-05F, 1.8600792e-05F, 1.9809576e-05F, + 2.1096914e-05F, 2.2467911e-05F, 2.3928002e-05F, 2.5482978e-05F, + 2.7139006e-05F, 2.8902651e-05F, 3.0780908e-05F, 3.2781225e-05F, + 3.4911534e-05F, 3.7180282e-05F, 3.9596466e-05F, 4.2169667e-05F, + 4.4910090e-05F, 4.7828601e-05F, 5.0936773e-05F, 5.4246931e-05F, + 5.7772202e-05F, 6.1526565e-05F, 6.5524908e-05F, 6.9783085e-05F, + 7.4317983e-05F, 7.9147585e-05F, 8.4291040e-05F, 8.9768747e-05F, + 9.5602426e-05F, 0.00010181521F, 0.00010843174F, 0.00011547824F, + 0.00012298267F, 0.00013097477F, 0.00013948625F, 0.00014855085F, + 0.00015820453F, 0.00016848555F, 0.00017943469F, 0.00019109536F, + 0.00020351382F, 0.00021673929F, 0.00023082423F, 0.00024582449F, + 0.00026179955F, 0.00027881276F, 0.00029693158F, 0.00031622787F, + 0.00033677814F, 0.00035866388F, 0.00038197188F, 0.00040679456F, + 0.00043323036F, 0.00046138411F, 0.00049136745F, 0.00052329927F, + 0.00055730621F, 0.00059352311F, 0.00063209358F, 0.00067317058F, + 0.00071691700F, 0.00076350630F, 0.00081312324F, 0.00086596457F, + 0.00092223983F, 0.00098217216F, 0.0010459992F, 0.0011139742F, + 0.0011863665F, 0.0012634633F, 0.0013455702F, 0.0014330129F, + 0.0015261382F, 0.0016253153F, 0.0017309374F, 0.0018434235F, + 0.0019632195F, 0.0020908006F, 0.0022266726F, 0.0023713743F, + 0.0025254795F, 0.0026895994F, 0.0028643847F, 0.0030505286F, + 0.0032487691F, 0.0034598925F, 0.0036847358F, 0.0039241906F, + 0.0041792066F, 0.0044507950F, 0.0047400328F, 0.0050480668F, + 0.0053761186F, 0.0057254891F, 0.0060975636F, 0.0064938176F, + 0.0069158225F, 0.0073652516F, 0.0078438871F, 0.0083536271F, + 0.0088964928F, 0.009474637F, 0.010090352F, 0.010746080F, + 0.011444421F, 0.012188144F, 0.012980198F, 0.013823725F, + 0.014722068F, 0.015678791F, 0.016697687F, 0.017782797F, + 0.018938423F, 0.020169149F, 0.021479854F, 0.022875735F, + 0.024362330F, 0.025945531F, 0.027631618F, 0.029427276F, + 0.031339626F, 0.033376252F, 0.035545228F, 0.037855157F, + 0.040315199F, 0.042935108F, 0.045725273F, 0.048696758F, + 0.051861348F, 0.055231591F, 0.058820850F, 0.062643361F, + 0.066714279F, 0.071049749F, 0.075666962F, 0.080584227F, + 0.085821044F, 0.091398179F, 0.097337747F, 0.10366330F, + 0.11039993F, 0.11757434F, 0.12521498F, 0.13335215F, + 0.14201813F, 0.15124727F, 0.16107617F, 0.17154380F, + 0.18269168F, 0.19456402F, 0.20720788F, 0.22067342F, + 0.23501402F, 0.25028656F, 0.26655159F, 0.28387361F, + 0.30232132F, 0.32196786F, 0.34289114F, 0.36517414F, + 0.38890521F, 0.41417847F, 0.44109412F, 0.46975890F, + 0.50028648F, 0.53279791F, 0.56742212F, 0.60429640F, + 0.64356699F, 0.68538959F, 0.72993007F, 0.77736504F, + 0.82788260F, 0.88168307F, 0.9389798F, 1.F, +}; /* this is for per-channel noise normalization */ static int apsort(const void *a, const void *b){ - float f1=fabs(**(float**)a); - float f2=fabs(**(float**)b); + float f1=**(float**)a; + float f2=**(float**)b; return (f1<f2)-(f1>f2); } -int **_vp_quantize_couple_sort(vorbis_block *vb, - vorbis_look_psy *p, - vorbis_info_mapping0 *vi, - float **mags){ +static void flag_lossless(int limit, float prepoint, float postpoint, float *mdct, + float *floor, int *flag, int i, int jn){ + int j; + for(j=0;j<jn;j++){ + float point = j>=limit-i ? postpoint : prepoint; + float r = fabs(mdct[j])/floor[j]; + if(r<point) + flag[j]=0; + else + flag[j]=1; + } +} +/* Overload/Side effect: On input, the *q vector holds either the + quantized energy (for elements with the flag set) or the absolute + values of the *r vector (for elements with flag unset). On output, + *q holds the quantized energy for all elements */ +static float noise_normalize(vorbis_look_psy *p, int limit, float *r, float *q, float *f, int *flags, float acc, int i, int n, int *out){ - if(p->vi->normal_point_p){ - int i,j,k,n=p->n; - int **ret=_vorbis_block_alloc(vb,vi->coupling_steps*sizeof(*ret)); - int partition=p->vi->normal_partition; - float **work=alloca(sizeof(*work)*partition); + vorbis_info_psy *vi=p->vi; + float **sort = alloca(n*sizeof(*sort)); + int j,count=0; + int start = (vi->normal_p ? vi->normal_start-i : n); + if(start>n)start=n; - for(i=0;i<vi->coupling_steps;i++){ - ret[i]=_vorbis_block_alloc(vb,n*sizeof(**ret)); + /* force classic behavior where only energy in the current band is considered */ + acc=0.f; + + /* still responsible for populating *out where noise norm not in + effect. There's no need to [re]populate *q in these areas */ + for(j=0;j<start;j++){ + if(!flags || !flags[j]){ /* lossless coupling already quantized. + Don't touch; requantizing based on + energy would be incorrect. */ + float ve = q[j]/f[j]; + if(r[j]<0) + out[j] = -rint(sqrt(ve)); + else + out[j] = rint(sqrt(ve)); + } + } - for(j=0;j<n;j+=partition){ - for(k=0;k<partition;k++)work[k]=mags[i]+k+j; - qsort(work,partition,sizeof(*work),apsort); - for(k=0;k<partition;k++)ret[i][k+j]=work[k]-mags[i]; + /* sort magnitudes for noise norm portion of partition */ + for(;j<n;j++){ + if(!flags || !flags[j]){ /* can't noise norm elements that have + already been loslessly coupled; we can + only account for their energy error */ + float ve = q[j]/f[j]; + /* Despite all the new, more capable coupling code, for now we + implement noise norm as it has been up to this point. Only + consider promotions to unit magnitude from 0. In addition + the only energy error counted is quantizations to zero. */ + /* also-- the original point code only applied noise norm at > pointlimit */ + if(ve<.25f && (!flags || j>=limit-i)){ + acc += ve; + sort[count++]=q+j; /* q is fabs(r) for unflagged element */ + }else{ + /* For now: no acc adjustment for nonzero quantization. populate *out and q as this value is final. */ + if(r[j]<0) + out[j] = -rint(sqrt(ve)); + else + out[j] = rint(sqrt(ve)); + q[j] = out[j]*out[j]*f[j]; } - } - return(ret); + }/* else{ + again, no energy adjustment for error in nonzero quant-- for now + }*/ } - return(NULL); -} -void _vp_noise_normalize_sort(vorbis_look_psy *p, - float *magnitudes,int *sortedindex){ - int i,j,n=p->n; - vorbis_info_psy *vi=p->vi; - int partition=vi->normal_partition; - float **work=alloca(sizeof(*work)*partition); - int start=vi->normal_start; - - for(j=start;j<n;j+=partition){ - if(j+partition>n)partition=n-j; - for(i=0;i<partition;i++)work[i]=magnitudes+i+j; - qsort(work,partition,sizeof(*work),apsort); - for(i=0;i<partition;i++){ - sortedindex[i+j-start]=work[i]-magnitudes; + if(count){ + /* noise norm to do */ + qsort(sort,count,sizeof(*sort),apsort); + for(j=0;j<count;j++){ + int k=sort[j]-q; + if(acc>=vi->normal_thresh){ + out[k]=unitnorm(r[k]); + acc-=1.f; + q[k]=f[k]; + }else{ + out[k]=0; + q[k]=0.f; + } } } + + return acc; } -void _vp_noise_normalize(vorbis_look_psy *p, - float *in,float *out,int *sortedindex){ - int flag=0,i,j=0,n=p->n; - vorbis_info_psy *vi=p->vi; - int partition=vi->normal_partition; - int start=vi->normal_start; +/* Noise normalization, quantization and coupling are not wholly + seperable processes in depth>1 coupling. */ +void _vp_couple_quantize_normalize(int blobno, + vorbis_info_psy_global *g, + vorbis_look_psy *p, + vorbis_info_mapping0 *vi, + float **mdct, + int **iwork, + int *nonzero, + int sliding_lowpass, + int ch){ - if(start>n)start=n; + int i; + int n = p->n; + int partition=(p->vi->normal_p ? p->vi->normal_partition : 16); + int limit = g->coupling_pointlimit[p->vi->blockflag][blobno]; + float prepoint=stereo_threshholds[g->coupling_prepointamp[blobno]]; + float postpoint=stereo_threshholds[g->coupling_postpointamp[blobno]]; + float de=0.1*p->m_val; /* a blend of the AoTuV M2 and M3 code here and below */ - if(vi->normal_channel_p){ - for(;j<start;j++) - out[j]=rint(in[j]); + /* mdct is our raw mdct output, floor not removed. */ + /* inout passes in the ifloor, passes back quantized result */ - for(;j+partition<=n;j+=partition){ - float acc=0.; - int k; + /* unquantized energy (negative indicates amplitude has negative sign) */ + float **raw = alloca(ch*sizeof(*raw)); - for(i=j;i<j+partition;i++) - acc+=in[i]*in[i]; + /* dual pupose; quantized energy (if flag set), othersize fabs(raw) */ + float **quant = alloca(ch*sizeof(*quant)); - for(i=0;i<partition;i++){ - k=sortedindex[i+j-start]; + /* floor energy */ + float **floor = alloca(ch*sizeof(*floor)); - if(in[k]*in[k]>=.25f){ - out[k]=rint(in[k]); - acc-=in[k]*in[k]; - flag=1; - }else{ - if(acc<vi->normal_thresh)break; - out[k]=unitnorm(in[k]); - acc-=1.; - } - } + /* flags indicating raw/quantized status of elements in raw vector */ + int **flag = alloca(ch*sizeof(*flag)); - for(;i<partition;i++){ - k=sortedindex[i+j-start]; - out[k]=0.; - } - } - } + /* non-zero flag working vector */ + int *nz = alloca(ch*sizeof(*nz)); - for(;j<n;j++) - out[j]=rint(in[j]); - -} + /* energy surplus/defecit tracking */ + float *acc = alloca((ch+vi->coupling_steps)*sizeof(*acc)); -void _vp_couple(int blobno, - vorbis_info_psy_global *g, - vorbis_look_psy *p, - vorbis_info_mapping0 *vi, - float **res, - float **mag_memo, - int **mag_sort, - int **ifloor, - int *nonzero, - int sliding_lowpass){ - - int i,j,k,n=p->n; - - /* perform any requested channel coupling */ - /* point stereo can only be used in a first stage (in this encoder) - because of the dependency on floor lookups */ - for(i=0;i<vi->coupling_steps;i++){ + /* The threshold of a stereo is changed with the size of n */ + if(n > 1000) + postpoint=stereo_threshholds_limited[g->coupling_postpointamp[blobno]]; - /* once we're doing multistage coupling in which a channel goes - through more than one coupling step, the floor vector - magnitudes will also have to be recalculated an propogated - along with PCM. Right now, we're not (that will wait until 5.1 - most likely), so the code isn't here yet. The memory management - here is all assuming single depth couplings anyway. */ + raw[0] = alloca(ch*partition*sizeof(**raw)); + quant[0] = alloca(ch*partition*sizeof(**quant)); + floor[0] = alloca(ch*partition*sizeof(**floor)); + flag[0] = alloca(ch*partition*sizeof(**flag)); - /* make sure coupling a zero and a nonzero channel results in two - nonzero channels. */ - if(nonzero[vi->coupling_mag[i]] || - nonzero[vi->coupling_ang[i]]){ + for(i=1;i<ch;i++){ + raw[i] = &raw[0][partition*i]; + quant[i] = &quant[0][partition*i]; + floor[i] = &floor[0][partition*i]; + flag[i] = &flag[0][partition*i]; + } + for(i=0;i<ch+vi->coupling_steps;i++) + acc[i]=0.f; + for(i=0;i<n;i+=partition){ + int k,j,jn = partition > n-i ? n-i : partition; + int step,track = 0; - float *rM=res[vi->coupling_mag[i]]; - float *rA=res[vi->coupling_ang[i]]; - float *qM=rM+n; - float *qA=rA+n; - int *floorM=ifloor[vi->coupling_mag[i]]; - int *floorA=ifloor[vi->coupling_ang[i]]; - float prepoint=stereo_threshholds[g->coupling_prepointamp[blobno]]; - float postpoint=stereo_threshholds[g->coupling_postpointamp[blobno]]; - int partition=(p->vi->normal_point_p?p->vi->normal_partition:p->n); - int limit=g->coupling_pointlimit[p->vi->blockflag][blobno]; - int pointlimit=limit; + memcpy(nz,nonzero,sizeof(*nz)*ch); - nonzero[vi->coupling_mag[i]]=1; - nonzero[vi->coupling_ang[i]]=1; + /* prefill */ + memset(flag[0],0,ch*partition*sizeof(**flag)); + for(k=0;k<ch;k++){ + int *iout = &iwork[k][i]; + if(nz[k]){ - /* The threshold of a stereo is changed with the size of n */ - if(n > 1000) - postpoint=stereo_threshholds_limited[g->coupling_postpointamp[blobno]]; + for(j=0;j<jn;j++) + floor[k][j] = FLOOR1_fromdB_LOOKUP[iout[j]]; - for(j=0;j<p->n;j+=partition){ - float acc=0.f; + flag_lossless(limit,prepoint,postpoint,&mdct[k][i],floor[k],flag[k],i,jn); - for(k=0;k<partition;k++){ - int l=k+j; + for(j=0;j<jn;j++){ + quant[k][j] = raw[k][j] = mdct[k][i+j]*mdct[k][i+j]; + if(mdct[k][i+j]<0.f) raw[k][j]*=-1.f; + floor[k][j]*=floor[k][j]; + } - if(l<sliding_lowpass){ - if((l>=limit && fabs(rM[l])<postpoint && fabs(rA[l])<postpoint) || - (fabs(rM[l])<prepoint && fabs(rA[l])<prepoint)){ + acc[track]=noise_normalize(p,limit,raw[k],quant[k],floor[k],NULL,acc[track],i,jn,iout); + }else{ + for(j=0;j<jn;j++){ + floor[k][j] = 1e-10f; + raw[k][j] = 0.f; + quant[k][j] = 0.f; + flag[k][j] = 0; + iout[j]=0; + } + acc[track]=0.f; + } + track++; + } - precomputed_couple_point(mag_memo[i][l], - floorM[l],floorA[l], - qM+l,qA+l); + /* coupling */ + for(step=0;step<vi->coupling_steps;step++){ + int Mi = vi->coupling_mag[step]; + int Ai = vi->coupling_ang[step]; + int *iM = &iwork[Mi][i]; + int *iA = &iwork[Ai][i]; + float *reM = raw[Mi]; + float *reA = raw[Ai]; + float *qeM = quant[Mi]; + float *qeA = quant[Ai]; + float *floorM = floor[Mi]; + float *floorA = floor[Ai]; + int *fM = flag[Mi]; + int *fA = flag[Ai]; + + if(nz[Mi] || nz[Ai]){ + nz[Mi] = nz[Ai] = 1; + + for(j=0;j<jn;j++){ + + if(j<sliding_lowpass-i){ + if(fM[j] || fA[j]){ + /* lossless coupling */ + + reM[j] = fabs(reM[j])+fabs(reA[j]); + qeM[j] = qeM[j]+qeA[j]; + fM[j]=fA[j]=1; + + /* couple iM/iA */ + { + int A = iM[j]; + int B = iA[j]; + + if(abs(A)>abs(B)){ + iA[j]=(A>0?A-B:B-A); + }else{ + iA[j]=(B>0?A-B:B-A); + iM[j]=B; + } + + /* collapse two equivalent tuples to one */ + if(iA[j]>=abs(iM[j])*2){ + iA[j]= -iA[j]; + iM[j]= -iM[j]; + } + + } - if(rint(qM[l])==0.f)acc+=qM[l]*qM[l]; }else{ - couple_lossless(rM[l],rA[l],qM+l,qA+l); - } - }else{ - qM[l]=0.; - qA[l]=0.; - } - } - - if(p->vi->normal_point_p){ - for(k=0;k<partition && acc>=p->vi->normal_thresh;k++){ - int l=mag_sort[i][j+k]; - if(l<sliding_lowpass && l>=pointlimit && rint(qM[l])==0.f){ - qM[l]=unitnorm(qM[l]); - acc-=1.f; + /* lossy (point) coupling */ + if(j<limit-i){ + /* dipole */ + reM[j] += reA[j]; + qeM[j] = fabs(reM[j]); + }else{ + /* AoTuV */ + /** @ M2 ** + The boost problem by the combination of noise normalization and point stereo is eased. + However, this is a temporary patch. + by Aoyumi @ 2004/04/18 + */ + float derate = (1.0 - de*((float)(j-limit+i) / (float)(n-limit))); + + /* elliptical */ + if(reM[j]+reA[j]<0){ + reM[j] = - (qeM[j] = (fabs(reM[j])+fabs(reA[j]))*derate*derate); + }else{ + reM[j] = (qeM[j] = (fabs(reM[j])+fabs(reA[j]))*derate*derate); + } + } + reA[j]=qeA[j]=0.f; + fA[j]=1; + iA[j]=0; } } + floorM[j]=floorA[j]=floorM[j]+floorA[j]; } + /* normalize the resulting mag vector */ + acc[track]=noise_normalize(p,limit,raw[Mi],quant[Mi],floor[Mi],flag[Mi],acc[track],i,jn,iM); + track++; } } } -} -/* AoTuV */ -/** @ M2 ** - The boost problem by the combination of noise normalization and point stereo is eased. - However, this is a temporary patch. - by Aoyumi @ 2004/04/18 -*/ - -void hf_reduction(vorbis_info_psy_global *g, - vorbis_look_psy *p, - vorbis_info_mapping0 *vi, - float **mdct){ - - int i,j,n=p->n, de=0.3*p->m_val; - int limit=g->coupling_pointlimit[p->vi->blockflag][PACKETBLOBS/2]; - - for(i=0; i<vi->coupling_steps; i++){ - /* for(j=start; j<limit; j++){} // ???*/ - for(j=limit; j<n; j++) - mdct[i][j] *= (1.0 - de*((float)(j-limit) / (float)(n-limit))); + for(i=0;i<vi->coupling_steps;i++){ + /* make sure coupling a zero and a nonzero channel results in two + nonzero channels. */ + if(nonzero[vi->coupling_mag[i]] || + nonzero[vi->coupling_ang[i]]){ + nonzero[vi->coupling_mag[i]]=1; + nonzero[vi->coupling_ang[i]]=1; + } } } diff --git a/src/filters/transform/MpaDecFilter/libvorbisidec/psy.h b/src/filters/transform/MpaDecFilter/libvorbisidec/psy.h index e5e8886a0..c1ea82440 100644 --- a/src/filters/transform/MpaDecFilter/libvorbisidec/psy.h +++ b/src/filters/transform/MpaDecFilter/libvorbisidec/psy.h @@ -11,7 +11,7 @@ ******************************************************************** function: random psychoacoustics (not including preecho) - last mod: $Id: psy.h 16227 2009-07-08 06:58:46Z xiphmont $ + last mod: $Id: psy.h 16946 2010-03-03 16:12:40Z xiphmont $ ********************************************************************/ @@ -57,8 +57,7 @@ typedef struct vorbis_info_psy{ float max_curve_dB; - int normal_channel_p; - int normal_point_p; + int normal_p; int normal_start; int normal_partition; double normal_thresh; @@ -122,12 +121,6 @@ extern void *_vi_psy_dup(void *source); extern void _vi_psy_free(vorbis_info_psy *i); extern vorbis_info_psy *_vi_psy_copy(vorbis_info_psy *i); -extern void _vp_remove_floor(vorbis_look_psy *p, - float *mdct, - int *icodedflr, - float *residue, - int sliding_lowpass); - extern void _vp_noisemask(vorbis_look_psy *p, float *logmdct, float *logmask); @@ -148,38 +141,14 @@ extern void _vp_offset_and_mix(vorbis_look_psy *p, extern float _vp_ampmax_decay(float amp,vorbis_dsp_state *vd); -extern float **_vp_quantize_couple_memo(vorbis_block *vb, - vorbis_info_psy_global *g, - vorbis_look_psy *p, - vorbis_info_mapping0 *vi, - float **mdct); - -extern void _vp_couple(int blobno, - vorbis_info_psy_global *g, - vorbis_look_psy *p, - vorbis_info_mapping0 *vi, - float **res, - float **mag_memo, - int **mag_sort, - int **ifloor, - int *nonzero, - int sliding_lowpass); - -extern void _vp_noise_normalize(vorbis_look_psy *p, - float *in,float *out,int *sortedindex); - -extern void _vp_noise_normalize_sort(vorbis_look_psy *p, - float *magnitudes,int *sortedindex); - -extern int **_vp_quantize_couple_sort(vorbis_block *vb, - vorbis_look_psy *p, - vorbis_info_mapping0 *vi, - float **mags); - -extern void hf_reduction(vorbis_info_psy_global *g, - vorbis_look_psy *p, - vorbis_info_mapping0 *vi, - float **mdct); - +extern void _vp_couple_quantize_normalize(int blobno, + vorbis_info_psy_global *g, + vorbis_look_psy *p, + vorbis_info_mapping0 *vi, + float **mdct, + int **iwork, + int *nonzero, + int sliding_lowpass, + int ch); #endif diff --git a/src/filters/transform/MpaDecFilter/libvorbisidec/res0.c b/src/filters/transform/MpaDecFilter/libvorbisidec/res0.c index 7a4b64ae8..87df7659f 100644 --- a/src/filters/transform/MpaDecFilter/libvorbisidec/res0.c +++ b/src/filters/transform/MpaDecFilter/libvorbisidec/res0.c @@ -5,13 +5,13 @@ * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * * * - * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2009 * + * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2010 * * by the Xiph.Org Foundation http://www.xiph.org/ * * * ******************************************************************** function: residue backend 0, 1 and 2 implementation - last mod: $Id: res0.c 16552 2009-09-12 02:09:04Z xiphmont $ + last mod: $Id: res0.c 16962 2010-03-11 07:30:34Z xiphmont $ ********************************************************************/ @@ -31,6 +31,9 @@ #include "misc.h" #include "os.h" +//#define TRAIN_RES 1 +//#define TRAIN_RESAUX 1 + #if defined(TRAIN_RES) || defined (TRAIN_RESAUX) #include <stdio.h> #endif @@ -58,6 +61,7 @@ typedef struct { float training_min[8][64]; float tmin; float tmax; + int submap; #endif } vorbis_look_residue0; @@ -88,7 +92,7 @@ void res0_free_look(vorbis_look_residue *i){ codebook *statebook=look->partbooks[j][k]; /* long and short into the same bucket by current convention */ - sprintf(buffer,"res_part%d_pass%d.vqd",j,k); + sprintf(buffer,"res_sub%d_part%d_pass%d.vqd",look->submap,j,k); of=fopen(buffer,"a"); for(l=0;l<statebook->entries;l++) @@ -321,65 +325,71 @@ vorbis_look_residue *res0_look(vorbis_dsp_state *vd, } /* break an abstraction and copy some code for performance purposes */ -static int local_book_besterror(codebook *book,float *a){ - int dim=book->dim,i,k,o; - int best=0; - encode_aux_threshmatch *tt=book->c->thresh_tree; - - /* find the quant val of each scalar */ - for(k=0,o=dim;k<dim;++k){ - float val=a[--o]; - i=tt->threshvals>>1; - - if(val<tt->quantthresh[i]){ - if(val<tt->quantthresh[i-1]){ - for(--i;i>0;--i) - if(val>=tt->quantthresh[i-1]) - break; - } - }else{ - - for(++i;i<tt->threshvals-1;++i) - if(val<tt->quantthresh[i])break; - +static int local_book_besterror(codebook *book,int *a){ + int dim=book->dim; + int i,j,o; + int minval=book->minval; + int del=book->delta; + int qv=book->quantvals; + int ze=(qv>>1); + int index=0; + /* assumes integer/centered encoder codebook maptype 1 no more than dim 8 */ + int p[8]={0,0,0,0,0,0,0,0}; + + if(del!=1){ + for(i=0,o=dim;i<dim;i++){ + int v = (a[--o]-minval+(del>>1))/del; + int m = (v<ze ? ((ze-v)<<1)-1 : ((v-ze)<<1)); + index = index*qv+ (m<0?0:(m>=qv?qv-1:m)); + p[o]=v*del+minval; + } + }else{ + for(i=0,o=dim;i<dim;i++){ + int v = a[--o]-minval; + int m = (v<ze ? ((ze-v)<<1)-1 : ((v-ze)<<1)); + index = index*qv+ (m<0?0:(m>=qv?qv-1:m)); + p[o]=v*del+minval; } - - best=(best*tt->quantvals)+tt->quantmap[i]; } - /* regular lattices are easy :-) */ - if(book->c->lengthlist[best]<=0){ + if(book->c->lengthlist[index]<=0){ const static_codebook *c=book->c; - int i,j; - float bestf=0.f; - float *e=book->valuelist; - best=-1; + int best=-1; + /* assumes integer/centered encoder codebook maptype 1 no more than dim 8 */ + int e[8]={0,0,0,0,0,0,0,0}; + int maxval = book->minval + book->delta*(book->quantvals-1); for(i=0;i<book->entries;i++){ if(c->lengthlist[i]>0){ - float this=0.f; + int this=0; for(j=0;j<dim;j++){ - float val=(e[j]-a[j]); + int val=(e[j]-a[j]); this+=val*val; } - if(best==-1 || this<bestf){ - bestf=this; - best=i; + if(best==-1 || this<best){ + memcpy(p,e,sizeof(p)); + best=this; + index=i; } } - e+=dim; + /* assumes the value patterning created by the tools in vq/ */ + j=0; + while(e[j]>=maxval) + e[j++]=0; + if(e[j]>=0) + e[j]+=book->delta; + e[j]= -e[j]; } } - if(best>-1){ - float *ptr=book->valuelist+best*dim; + if(index>-1){ for(i=0;i<dim;i++) - *a++ -= *ptr++; + *a++ -= p[i]; } - return(best); + return(index); } -static int _encodepart(oggpack_buffer *opb,float *vec, int n, +static int _encodepart(oggpack_buffer *opb,int *vec, int n, codebook *book,long *acc){ int i,bits=0; int dim=book->dim; @@ -389,7 +399,7 @@ static int _encodepart(oggpack_buffer *opb,float *vec, int n, int entry=local_book_besterror(book,vec+i*dim); #ifdef TRAIN_RES - if(entry>0) + if(entry>=0) acc[entry]++; #endif @@ -401,7 +411,7 @@ static int _encodepart(oggpack_buffer *opb,float *vec, int n, } static long **_01class(vorbis_block *vb,vorbis_look_residue *vl, - float **in,int ch){ + int **in,int ch){ long i,j,k; vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl; vorbis_info_residue0 *info=look->info; @@ -427,17 +437,17 @@ static long **_01class(vorbis_block *vb,vorbis_look_residue *vl, for(i=0;i<partvals;i++){ int offset=i*samples_per_partition+info->begin; for(j=0;j<ch;j++){ - float max=0.; - float ent=0.; + int max=0; + int ent=0; for(k=0;k<samples_per_partition;k++){ - if(fabs(in[j][offset+k])>max)max=fabs(in[j][offset+k]); - ent+=fabs(rint(in[j][offset+k])); + if(abs(in[j][offset+k])>max)max=abs(in[j][offset+k]); + ent+=abs(in[j][offset+k]); } ent*=scale; for(k=0;k<possible_partitions-1;k++) if(max<=info->classmetric1[k] && - (info->classmetric2[k]<0 || (int)ent<info->classmetric2[k])) + (info->classmetric2[k]<0 || ent<info->classmetric2[k])) break; partword[j][i]=k; @@ -467,7 +477,7 @@ static long **_01class(vorbis_block *vb,vorbis_look_residue *vl, /* designed for stereo or other modes where the partition size is an integer multiple of the number of channels encoded in the current submap */ -static long **_2class(vorbis_block *vb,vorbis_look_residue *vl,float **in, +static long **_2class(vorbis_block *vb,vorbis_look_residue *vl,int **in, int ch){ long i,j,k,l; vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl; @@ -486,17 +496,17 @@ static long **_2class(vorbis_block *vb,vorbis_look_residue *vl,float **in, char buffer[80]; #endif - partword[0]=_vorbis_block_alloc(vb,n*ch/samples_per_partition*sizeof(*partword[0])); - memset(partword[0],0,n*ch/samples_per_partition*sizeof(*partword[0])); + partword[0]=_vorbis_block_alloc(vb,partvals*sizeof(*partword[0])); + memset(partword[0],0,partvals*sizeof(*partword[0])); for(i=0,l=info->begin/ch;i<partvals;i++){ - float magmax=0.f; - float angmax=0.f; + int magmax=0; + int angmax=0; for(j=0;j<samples_per_partition;j+=ch){ - if(fabs(in[0][l])>magmax)magmax=fabs(in[0][l]); + if(abs(in[0][l])>magmax)magmax=abs(in[0][l]); for(k=1;k<ch;k++) - if(fabs(in[k][l])>angmax)angmax=fabs(in[k][l]); - l++; + if(abs(in[k][l])>angmax)angmax=abs(in[k][l]); + l++; } for(j=0;j<possible_partitions-1;j++) @@ -524,14 +534,19 @@ static long **_2class(vorbis_block *vb,vorbis_look_residue *vl,float **in, static int _01forward(oggpack_buffer *opb, vorbis_block *vb,vorbis_look_residue *vl, - float **in,int ch, + int **in,int ch, long **partword, - int (*encode)(oggpack_buffer *,float *,int, - codebook *,long *)){ + int (*encode)(oggpack_buffer *,int *,int, + codebook *,long *), + int submap){ long i,j,k,s; vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl; vorbis_info_residue0 *info=look->info; +#ifdef TRAIN_RES + look->submap=submap; +#endif + /* move all this setup out later */ int samples_per_partition=info->grouping; int possible_partitions=info->partitions; @@ -544,7 +559,7 @@ static int _01forward(oggpack_buffer *opb, #ifdef TRAIN_RES for(i=0;i<ch;i++) - for(j=info->begin;j<end;j++){ + for(j=info->begin;j<info->end;j++){ if(in[i][j]>look->tmax)look->tmax=in[i][j]; if(in[i][j]<look->tmin)look->tmin=in[i][j]; } @@ -599,7 +614,7 @@ static int _01forward(oggpack_buffer *opb, accumulator=look->training_data[s][partword[j][i]]; { int l; - float *samples=in[j]+offset; + int *samples=in[j]+offset; for(l=0;l<samples_per_partition;l++){ if(samples[l]<look->training_min[s][partword[j][i]]) look->training_min[s][partword[j][i]]=samples[l]; @@ -698,54 +713,6 @@ static int _01inverse(vorbis_block *vb,vorbis_look_residue *vl, return(0); } -#if 0 -/* residue 0 and 1 are just slight variants of one another. 0 is - interleaved, 1 is not */ -long **res0_class(vorbis_block *vb,vorbis_look_residue *vl, - float **in,int *nonzero,int ch){ - /* we encode only the nonzero parts of a bundle */ - int i,used=0; - for(i=0;i<ch;i++) - if(nonzero[i]) - in[used++]=in[i]; - if(used) - /*return(_01class(vb,vl,in,used,_interleaved_testhack));*/ - return(_01class(vb,vl,in,used)); - else - return(0); -} - -int res0_forward(vorbis_block *vb,vorbis_look_residue *vl, - float **in,float **out,int *nonzero,int ch, - long **partword){ - /* we encode only the nonzero parts of a bundle */ - int i,j,used=0,n=vb->pcmend/2; - for(i=0;i<ch;i++) - if(nonzero[i]){ - if(out) - for(j=0;j<n;j++) - out[i][j]+=in[i][j]; - in[used++]=in[i]; - } - if(used){ - int ret=_01forward(vb,vl,in,used,partword, - _interleaved_encodepart); - if(out){ - used=0; - for(i=0;i<ch;i++) - if(nonzero[i]){ - for(j=0;j<n;j++) - out[i][j]-=in[used][j]; - used++; - } - } - return(ret); - }else{ - return(0); - } -} -#endif - int res0_inverse(vorbis_block *vb,vorbis_look_residue *vl, float **in,int *nonzero,int ch){ int i,used=0; @@ -759,36 +726,21 @@ int res0_inverse(vorbis_block *vb,vorbis_look_residue *vl, } int res1_forward(oggpack_buffer *opb,vorbis_block *vb,vorbis_look_residue *vl, - float **in,float **out,int *nonzero,int ch, - long **partword){ - int i,j,used=0,n=vb->pcmend/2; + int **in,int *nonzero,int ch, long **partword, int submap){ + int i,used=0; for(i=0;i<ch;i++) - if(nonzero[i]){ - if(out) - for(j=0;j<n;j++) - out[i][j]+=in[i][j]; + if(nonzero[i]) in[used++]=in[i]; - } if(used){ - int ret=_01forward(opb,vb,vl,in,used,partword,_encodepart); - if(out){ - used=0; - for(i=0;i<ch;i++) - if(nonzero[i]){ - for(j=0;j<n;j++) - out[i][j]-=in[used][j]; - used++; - } - } - return(ret); + return _01forward(opb,vb,vl,in,used,partword,_encodepart,submap); }else{ return(0); } } long **res1_class(vorbis_block *vb,vorbis_look_residue *vl, - float **in,int *nonzero,int ch){ + int **in,int *nonzero,int ch){ int i,used=0; for(i=0;i<ch;i++) if(nonzero[i]) @@ -812,7 +764,7 @@ int res1_inverse(vorbis_block *vb,vorbis_look_residue *vl, } long **res2_class(vorbis_block *vb,vorbis_look_residue *vl, - float **in,int *nonzero,int ch){ + int **in,int *nonzero,int ch){ int i,used=0; for(i=0;i<ch;i++) if(nonzero[i])used++; @@ -827,34 +779,22 @@ long **res2_class(vorbis_block *vb,vorbis_look_residue *vl, int res2_forward(oggpack_buffer *opb, vorbis_block *vb,vorbis_look_residue *vl, - float **in,float **out,int *nonzero,int ch, - long **partword){ + int **in,int *nonzero,int ch, long **partword,int submap){ long i,j,k,n=vb->pcmend/2,used=0; /* don't duplicate the code; use a working vector hack for now and reshape ourselves into a single channel res1 */ /* ugly; reallocs for each coupling pass :-( */ - float *work=_vorbis_block_alloc(vb,ch*n*sizeof(*work)); + int *work=_vorbis_block_alloc(vb,ch*n*sizeof(*work)); for(i=0;i<ch;i++){ - float *pcm=in[i]; + int *pcm=in[i]; if(nonzero[i])used++; for(j=0,k=i;j<n;j++,k+=ch) work[k]=pcm[j]; } if(used){ - int ret=_01forward(opb,vb,vl,&work,1,partword,_encodepart); - /* update the sofar vector */ - if(out){ - for(i=0;i<ch;i++){ - float *pcm=in[i]; - float *sofar=out[i]; - for(j=0,k=i;j<n;j++,k+=ch) - sofar[j]+=pcm[j]-work[k]; - - } - } - return(ret); + return _01forward(opb,vb,vl,&work,1,partword,_encodepart,submap); }else{ return(0); } diff --git a/src/filters/transform/MpaDecFilter/libvorbisidec/sharedbook.c b/src/filters/transform/MpaDecFilter/libvorbisidec/sharedbook.c index 35dde586b..545e302f8 100644 --- a/src/filters/transform/MpaDecFilter/libvorbisidec/sharedbook.c +++ b/src/filters/transform/MpaDecFilter/libvorbisidec/sharedbook.c @@ -11,7 +11,7 @@ ******************************************************************** function: basic shared codebook operations - last mod: $Id: sharedbook.c 16227 2009-07-08 06:58:46Z xiphmont $ + last mod: $Id: sharedbook.c 17030 2010-03-25 06:52:55Z xiphmont $ ********************************************************************/ @@ -52,7 +52,7 @@ long _float32_pack(float val){ sign=0x80000000; val= -val; } - exp= floor(log(val)/log(2.f)); + exp= floor(log(val)/log(2.f)+.001); //+epsilon mant=rint(ldexp(val,(VQ_FMAN-1)-exp)); exp=(exp+VQ_FEXP_BIAS)<<VQ_FMAN; @@ -258,34 +258,13 @@ float *_book_unquantize(const static_codebook *b,int n,int *sparsemap){ return(NULL); } -void vorbis_staticbook_clear(static_codebook *b){ +void vorbis_staticbook_destroy(static_codebook *b){ if(b->allocedp){ if(b->quantlist)_ogg_free(b->quantlist); if(b->lengthlist)_ogg_free(b->lengthlist); - if(b->nearest_tree){ - _ogg_free(b->nearest_tree->ptr0); - _ogg_free(b->nearest_tree->ptr1); - _ogg_free(b->nearest_tree->p); - _ogg_free(b->nearest_tree->q); - memset(b->nearest_tree,0,sizeof(*b->nearest_tree)); - _ogg_free(b->nearest_tree); - } - if(b->thresh_tree){ - _ogg_free(b->thresh_tree->quantthresh); - _ogg_free(b->thresh_tree->quantmap); - memset(b->thresh_tree,0,sizeof(*b->thresh_tree)); - _ogg_free(b->thresh_tree); - } - memset(b,0,sizeof(*b)); - } -} - -void vorbis_staticbook_destroy(static_codebook *b){ - if(b->allocedp){ - vorbis_staticbook_clear(b); _ogg_free(b); - } + } /* otherwise, it is in static memory */ } void vorbis_book_clear(codebook *b){ @@ -309,7 +288,10 @@ int vorbis_book_init_encode(codebook *c,const static_codebook *s){ c->used_entries=s->entries; c->dim=s->dim; c->codelist=_make_words(s->lengthlist,s->entries,0); - c->valuelist=_book_unquantize(s,s->entries,NULL); + //c->valuelist=_book_unquantize(s,s->entries,NULL); + c->quantvals=_book_maptype1_quantvals(s); + c->minval=(int)rint(_float32_unpack(s->q_min)); + c->delta=(int)rint(_float32_unpack(s->q_delta)); return(0); } @@ -445,157 +427,6 @@ int vorbis_book_init_decode(codebook *c,const static_codebook *s){ return(-1); } -static float _dist(int el,float *ref, float *b,int step){ - int i; - float acc=0.f; - for(i=0;i<el;i++){ - float val=(ref[i]-b[i*step]); - acc+=val*val; - } - return(acc); -} - -int _best(codebook *book, float *a, int step){ - encode_aux_threshmatch *tt=book->c->thresh_tree; - -#if 0 - encode_aux_nearestmatch *nt=book->c->nearest_tree; - encode_aux_pigeonhole *pt=book->c->pigeon_tree; -#endif - int dim=book->dim; - int k,o; - /*int savebest=-1; - float saverr;*/ - - /* do we have a threshhold encode hint? */ - if(tt){ - int index=0,i; - /* find the quant val of each scalar */ - for(k=0,o=step*(dim-1);k<dim;k++,o-=step){ - - i=tt->threshvals>>1; - if(a[o]<tt->quantthresh[i]){ - - for(;i>0;i--) - if(a[o]>=tt->quantthresh[i-1]) - break; - - }else{ - - for(i++;i<tt->threshvals-1;i++) - if(a[o]<tt->quantthresh[i])break; - - } - - index=(index*tt->quantvals)+tt->quantmap[i]; - } - /* regular lattices are easy :-) */ - if(book->c->lengthlist[index]>0) /* is this unused? If so, we'll - use a decision tree after all - and fall through*/ - return(index); - } - -#if 0 - /* do we have a pigeonhole encode hint? */ - if(pt){ - const static_codebook *c=book->c; - int i,besti=-1; - float best=0.f; - int entry=0; - - /* dealing with sequentialness is a pain in the ass */ - if(c->q_sequencep){ - int pv; - long mul=1; - float qlast=0; - for(k=0,o=0;k<dim;k++,o+=step){ - pv=(int)((a[o]-qlast-pt->min)/pt->del); - if(pv<0 || pv>=pt->mapentries)break; - entry+=pt->pigeonmap[pv]*mul; - mul*=pt->quantvals; - qlast+=pv*pt->del+pt->min; - } - }else{ - for(k=0,o=step*(dim-1);k<dim;k++,o-=step){ - int pv=(int)((a[o]-pt->min)/pt->del); - if(pv<0 || pv>=pt->mapentries)break; - entry=entry*pt->quantvals+pt->pigeonmap[pv]; - } - } - - /* must be within the pigeonholable range; if we quant outside (or - in an entry that we define no list for), brute force it */ - if(k==dim && pt->fitlength[entry]){ - /* search the abbreviated list */ - long *list=pt->fitlist+pt->fitmap[entry]; - for(i=0;i<pt->fitlength[entry];i++){ - float this=_dist(dim,book->valuelist+list[i]*dim,a,step); - if(besti==-1 || this<best){ - best=this; - besti=list[i]; - } - } - - return(besti); - } - } - - if(nt){ - /* optimized using the decision tree */ - while(1){ - float c=0.f; - float *p=book->valuelist+nt->p[ptr]; - float *q=book->valuelist+nt->q[ptr]; - - for(k=0,o=0;k<dim;k++,o+=step) - c+=(p[k]-q[k])*(a[o]-(p[k]+q[k])*.5); - - if(c>0.f) /* in A */ - ptr= -nt->ptr0[ptr]; - else /* in B */ - ptr= -nt->ptr1[ptr]; - if(ptr<=0)break; - } - return(-ptr); - } -#endif - - /* brute force it! */ - { - const static_codebook *c=book->c; - int i,besti=-1; - float best=0.f; - float *e=book->valuelist; - for(i=0;i<book->entries;i++){ - if(c->lengthlist[i]>0){ - float this=_dist(dim,e,a,step); - if(besti==-1 || this<best){ - best=this; - besti=i; - } - } - e+=dim; - } - - /*if(savebest!=-1 && savebest!=besti){ - fprintf(stderr,"brute force/pigeonhole disagreement:\n" - "original:"); - for(i=0;i<dim*step;i+=step)fprintf(stderr,"%g,",a[i]); - fprintf(stderr,"\n" - "pigeonhole (entry %d, err %g):",savebest,saverr); - for(i=0;i<dim;i++)fprintf(stderr,"%g,", - (book->valuelist+savebest*dim)[i]); - fprintf(stderr,"\n" - "bruteforce (entry %d, err %g):",besti,best); - for(i=0;i<dim;i++)fprintf(stderr,"%g,", - (book->valuelist+besti*dim)[i]); - fprintf(stderr,"\n"); - }*/ - return(besti); - } -} - long vorbis_book_codeword(codebook *book,int entry){ if(book->c) /* only use with encode; decode optimizations are allowed to break this */ @@ -638,7 +469,6 @@ static_codebook test1={ 0, 0,0,0,0, NULL, - NULL,NULL,NULL, 0 }; static float *test1_result=NULL; @@ -650,7 +480,6 @@ static_codebook test2={ 2, -533200896,1611661312,4,0, full_quantlist1, - NULL,NULL,NULL, 0 }; static float test2_result[]={-3,-2,-1,0, 1,2,3,4, 5,0,3,-2}; @@ -662,7 +491,6 @@ static_codebook test3={ 2, -533200896,1611661312,4,1, full_quantlist1, - NULL,NULL,NULL, 0 }; static float test3_result[]={-3,-5,-6,-6, 1,3,6,10, 5,5,8,6}; @@ -674,7 +502,6 @@ static_codebook test4={ 1, -533200896,1611661312,4,0, partial_quantlist1, - NULL,NULL,NULL, 0 }; static float test4_result[]={-3,-3,-3, 4,-3,-3, -1,-3,-3, @@ -694,7 +521,6 @@ static_codebook test5={ 1, -533200896,1611661312,4,1, partial_quantlist1, - NULL,NULL,NULL, 0 }; static float test5_result[]={-3,-6,-9, 4, 1,-2, -1,-4,-7, diff --git a/src/filters/transform/MpaDecFilter/libvorbisidec/synthesis.c b/src/filters/transform/MpaDecFilter/libvorbisidec/synthesis.c index 7a0c74804..5aa1d884e 100644 --- a/src/filters/transform/MpaDecFilter/libvorbisidec/synthesis.c +++ b/src/filters/transform/MpaDecFilter/libvorbisidec/synthesis.c @@ -11,7 +11,7 @@ ******************************************************************** function: single-block PCM synthesis - last mod: $Id: synthesis.c 16227 2009-07-08 06:58:46Z xiphmont $ + last mod: $Id: synthesis.c 17027 2010-03-25 05:21:20Z xiphmont $ ********************************************************************/ @@ -24,13 +24,17 @@ #include "os.h" int vorbis_synthesis(vorbis_block *vb,ogg_packet *op){ - vorbis_dsp_state *vd=vb->vd; - private_state *b=vd->backend_state; - vorbis_info *vi=vd->vi; - codec_setup_info *ci=vi->codec_setup; - oggpack_buffer *opb=&vb->opb; + vorbis_dsp_state *vd= vb ? vb->vd : 0; + private_state *b= vd ? vd->backend_state : 0; + vorbis_info *vi= vd ? vd->vi : 0; + codec_setup_info *ci= vi ? vi->codec_setup : 0; + oggpack_buffer *opb=vb ? &vb->opb : 0; int type,mode,i; + if (!vd || !b || !vi || !ci || !opb) { + return OV_EBADPACKET; + } + /* first things first. Make sure decode is ready */ _vorbis_block_ripcord(vb); oggpack_readinit(opb,op->packet,op->bytes); @@ -43,9 +47,15 @@ int vorbis_synthesis(vorbis_block *vb,ogg_packet *op){ /* read our mode and pre/post windowsize */ mode=oggpack_read(opb,b->modebits); - if(mode==-1)return(OV_EBADPACKET); + if(mode==-1){ + return(OV_EBADPACKET); + } vb->mode=mode; + if(!ci->mode_param[mode]){ + return(OV_EBADPACKET); + } + vb->W=ci->mode_param[mode]->blockflag; if(vb->W){ @@ -53,7 +63,9 @@ int vorbis_synthesis(vorbis_block *vb,ogg_packet *op){ only for window selection */ vb->lW=oggpack_read(opb,1); vb->nW=oggpack_read(opb,1); - if(vb->nW==-1) return(OV_EBADPACKET); + if(vb->nW==-1){ + return(OV_EBADPACKET); + } }else{ vb->lW=0; vb->nW=0; diff --git a/src/filters/transform/MpaDecFilter/libvorbisidec/tone.c b/src/filters/transform/MpaDecFilter/libvorbisidec/tone.c new file mode 100644 index 000000000..0812aca4f --- /dev/null +++ b/src/filters/transform/MpaDecFilter/libvorbisidec/tone.c @@ -0,0 +1,55 @@ +#include <stdlib.h>
+#include <stdio.h>
+#define _USE_MATH_DEFINES
+#include <math.h>
+#include <string.h>
+
+void usage(){
+ fprintf(stderr,"tone <frequency_Hz>,[<amplitude>] [<frequency_Hz>,[<amplitude>]...]\n");
+ exit(1);
+}
+
+int main (int argc,char *argv[]){
+ int i,j;
+ double *f;
+ double *amp;
+
+ if(argc<2)usage();
+
+ f=alloca(sizeof(*f)*(argc-1));
+ amp=alloca(sizeof(*amp)*(argc-1));
+
+ i=0;
+ while(argv[i+1]){
+ char *pos=strchr(argv[i+1],',');
+
+ f[i]=atof(argv[i+1]);
+ if(pos)
+ amp[i]=atof(pos+1)*32767.f;
+ else
+ amp[i]=32767.f;
+
+ fprintf(stderr,"%g Hz, %g amp\n",f[i],amp[i]);
+
+ i++;
+ }
+
+ for(i=0;i<44100*10;i++){
+ float val=0;
+ int ival;
+ for(j=0;j<argc-1;j++)
+ val+=amp[j]*sin(i/44100.f*f[j]*2*M_PI);
+ ival=rint(val);
+
+ if(ival>32767.f)ival=32767.f;
+ if(ival<-32768.f)ival=-32768.f;
+
+ fprintf(stdout,"%c%c%c%c",
+ (char)(ival&0xff),
+ (char)((ival>>8)&0xff),
+ (char)(ival&0xff),
+ (char)((ival>>8)&0xff));
+ }
+ return(0);
+}
+
diff --git a/src/filters/transform/MpaDecFilter/libvorbisidec/vorbis/codec.h b/src/filters/transform/MpaDecFilter/libvorbisidec/vorbis/codec.h index 259798c61..999aa3351 100644 --- a/src/filters/transform/MpaDecFilter/libvorbisidec/vorbis/codec.h +++ b/src/filters/transform/MpaDecFilter/libvorbisidec/vorbis/codec.h @@ -11,7 +11,7 @@ ******************************************************************** function: libvorbis codec headers - last mod: $Id: codec.h 16037 2009-05-26 21:10:58Z xiphmont $ + last mod: $Id: codec.h 17021 2010-03-24 09:29:41Z xiphmont $ ********************************************************************/ @@ -121,7 +121,7 @@ typedef struct vorbis_block{ /* vorbis_block is a single block of data to be processed as part of the analysis/synthesis stream; it belongs to a specific logical -bitstream, but is independant from other vorbis_blocks belonging to +bitstream, but is independent from other vorbis_blocks belonging to that logical bitstream. *************************************************/ struct alloc_chain{ diff --git a/src/filters/transform/MpaDecFilter/libvorbisidec/vorbis/vorbisfile.h b/src/filters/transform/MpaDecFilter/libvorbisidec/vorbis/vorbisfile.h new file mode 100644 index 000000000..0d7e14a2b --- /dev/null +++ b/src/filters/transform/MpaDecFilter/libvorbisidec/vorbis/vorbisfile.h @@ -0,0 +1,206 @@ +/********************************************************************
+ * *
+ * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
+ * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
+ * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
+ * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
+ * *
+ * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2007 *
+ * by the Xiph.Org Foundation http://www.xiph.org/ *
+ * *
+ ********************************************************************
+
+ function: stdio-based convenience library for opening/seeking/decoding
+ last mod: $Id: vorbisfile.h 17021 2010-03-24 09:29:41Z xiphmont $
+
+ ********************************************************************/
+
+#ifndef _OV_FILE_H_
+#define _OV_FILE_H_
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif /* __cplusplus */
+
+#include <stdio.h>
+#include "codec.h"
+
+/* The function prototypes for the callbacks are basically the same as for
+ * the stdio functions fread, fseek, fclose, ftell.
+ * The one difference is that the FILE * arguments have been replaced with
+ * a void * - this is to be used as a pointer to whatever internal data these
+ * functions might need. In the stdio case, it's just a FILE * cast to a void *
+ *
+ * If you use other functions, check the docs for these functions and return
+ * the right values. For seek_func(), you *MUST* return -1 if the stream is
+ * unseekable
+ */
+typedef struct {
+ size_t (*read_func) (void *ptr, size_t size, size_t nmemb, void *datasource);
+ int (*seek_func) (void *datasource, ogg_int64_t offset, int whence);
+ int (*close_func) (void *datasource);
+ long (*tell_func) (void *datasource);
+} ov_callbacks;
+
+#ifndef OV_EXCLUDE_STATIC_CALLBACKS
+
+/* a few sets of convenient callbacks, especially for use under
+ * Windows where ov_open_callbacks() should always be used instead of
+ * ov_open() to avoid problems with incompatible crt.o version linking
+ * issues. */
+
+static int _ov_header_fseek_wrap(FILE *f,ogg_int64_t off,int whence){
+ if(f==NULL)return(-1);
+
+#ifdef __MINGW32__
+ return fseeko64(f,off,whence);
+#elif defined (_WIN32)
+ return _fseeki64(f,off,whence);
+#else
+ return fseek(f,off,whence);
+#endif
+}
+
+/* These structs below (OV_CALLBACKS_DEFAULT etc) are defined here as
+ * static data. That means that every file which includes this header
+ * will get its own copy of these structs whether it uses them or
+ * not unless it #defines OV_EXCLUDE_STATIC_CALLBACKS.
+ * These static symbols are essential on platforms such as Windows on
+ * which several different versions of stdio support may be linked to
+ * by different DLLs, and we need to be certain we know which one
+ * we're using (the same one as the main application).
+ */
+
+static ov_callbacks OV_CALLBACKS_DEFAULT = {
+ (size_t (*)(void *, size_t, size_t, void *)) fread,
+ (int (*)(void *, ogg_int64_t, int)) _ov_header_fseek_wrap,
+ (int (*)(void *)) fclose,
+ (long (*)(void *)) ftell
+};
+
+static ov_callbacks OV_CALLBACKS_NOCLOSE = {
+ (size_t (*)(void *, size_t, size_t, void *)) fread,
+ (int (*)(void *, ogg_int64_t, int)) _ov_header_fseek_wrap,
+ (int (*)(void *)) NULL,
+ (long (*)(void *)) ftell
+};
+
+static ov_callbacks OV_CALLBACKS_STREAMONLY = {
+ (size_t (*)(void *, size_t, size_t, void *)) fread,
+ (int (*)(void *, ogg_int64_t, int)) NULL,
+ (int (*)(void *)) fclose,
+ (long (*)(void *)) NULL
+};
+
+static ov_callbacks OV_CALLBACKS_STREAMONLY_NOCLOSE = {
+ (size_t (*)(void *, size_t, size_t, void *)) fread,
+ (int (*)(void *, ogg_int64_t, int)) NULL,
+ (int (*)(void *)) NULL,
+ (long (*)(void *)) NULL
+};
+
+#endif
+
+#define NOTOPEN 0
+#define PARTOPEN 1
+#define OPENED 2
+#define STREAMSET 3
+#define INITSET 4
+
+typedef struct OggVorbis_File {
+ void *datasource; /* Pointer to a FILE *, etc. */
+ int seekable;
+ ogg_int64_t offset;
+ ogg_int64_t end;
+ ogg_sync_state oy;
+
+ /* If the FILE handle isn't seekable (eg, a pipe), only the current
+ stream appears */
+ int links;
+ ogg_int64_t *offsets;
+ ogg_int64_t *dataoffsets;
+ long *serialnos;
+ ogg_int64_t *pcmlengths; /* overloaded to maintain binary
+ compatibility; x2 size, stores both
+ beginning and end values */
+ vorbis_info *vi;
+ vorbis_comment *vc;
+
+ /* Decoding working state local storage */
+ ogg_int64_t pcm_offset;
+ int ready_state;
+ long current_serialno;
+ int current_link;
+
+ double bittrack;
+ double samptrack;
+
+ ogg_stream_state os; /* take physical pages, weld into a logical
+ stream of packets */
+ vorbis_dsp_state vd; /* central working state for the packet->PCM decoder */
+ vorbis_block vb; /* local working space for packet->PCM decode */
+
+ ov_callbacks callbacks;
+
+} OggVorbis_File;
+
+
+extern int ov_clear(OggVorbis_File *vf);
+extern int ov_fopen(char *path,OggVorbis_File *vf);
+extern int ov_open(FILE *f,OggVorbis_File *vf,char *initial,long ibytes);
+extern int ov_open_callbacks(void *datasource, OggVorbis_File *vf,
+ char *initial, long ibytes, ov_callbacks callbacks);
+
+extern int ov_test(FILE *f,OggVorbis_File *vf,char *initial,long ibytes);
+extern int ov_test_callbacks(void *datasource, OggVorbis_File *vf,
+ char *initial, long ibytes, ov_callbacks callbacks);
+extern int ov_test_open(OggVorbis_File *vf);
+
+extern long ov_bitrate(OggVorbis_File *vf,int i);
+extern long ov_bitrate_instant(OggVorbis_File *vf);
+extern long ov_streams(OggVorbis_File *vf);
+extern long ov_seekable(OggVorbis_File *vf);
+extern long ov_serialnumber(OggVorbis_File *vf,int i);
+
+extern ogg_int64_t ov_raw_total(OggVorbis_File *vf,int i);
+extern ogg_int64_t ov_pcm_total(OggVorbis_File *vf,int i);
+extern double ov_time_total(OggVorbis_File *vf,int i);
+
+extern int ov_raw_seek(OggVorbis_File *vf,ogg_int64_t pos);
+extern int ov_pcm_seek(OggVorbis_File *vf,ogg_int64_t pos);
+extern int ov_pcm_seek_page(OggVorbis_File *vf,ogg_int64_t pos);
+extern int ov_time_seek(OggVorbis_File *vf,double pos);
+extern int ov_time_seek_page(OggVorbis_File *vf,double pos);
+
+extern int ov_raw_seek_lap(OggVorbis_File *vf,ogg_int64_t pos);
+extern int ov_pcm_seek_lap(OggVorbis_File *vf,ogg_int64_t pos);
+extern int ov_pcm_seek_page_lap(OggVorbis_File *vf,ogg_int64_t pos);
+extern int ov_time_seek_lap(OggVorbis_File *vf,double pos);
+extern int ov_time_seek_page_lap(OggVorbis_File *vf,double pos);
+
+extern ogg_int64_t ov_raw_tell(OggVorbis_File *vf);
+extern ogg_int64_t ov_pcm_tell(OggVorbis_File *vf);
+extern double ov_time_tell(OggVorbis_File *vf);
+
+extern vorbis_info *ov_info(OggVorbis_File *vf,int link);
+extern vorbis_comment *ov_comment(OggVorbis_File *vf,int link);
+
+extern long ov_read_float(OggVorbis_File *vf,float ***pcm_channels,int samples,
+ int *bitstream);
+extern long ov_read_filter(OggVorbis_File *vf,char *buffer,int length,
+ int bigendianp,int word,int sgned,int *bitstream,
+ void (*filter)(float **pcm,long channels,long samples,void *filter_param),void *filter_param);
+extern long ov_read(OggVorbis_File *vf,char *buffer,int length,
+ int bigendianp,int word,int sgned,int *bitstream);
+extern int ov_crosslap(OggVorbis_File *vf1,OggVorbis_File *vf2);
+
+extern int ov_halfrate(OggVorbis_File *vf,int flag);
+extern int ov_halfrate_p(OggVorbis_File *vf);
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
+#endif
+
diff --git a/src/filters/transform/MpaDecFilter/libvorbisidec/vorbisfile.c b/src/filters/transform/MpaDecFilter/libvorbisidec/vorbisfile.c new file mode 100644 index 000000000..edbcec764 --- /dev/null +++ b/src/filters/transform/MpaDecFilter/libvorbisidec/vorbisfile.c @@ -0,0 +1,2321 @@ +/********************************************************************
+ * *
+ * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
+ * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
+ * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
+ * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
+ * *
+ * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2009 *
+ * by the Xiph.Org Foundation http://www.xiph.org/ *
+ * *
+ ********************************************************************
+
+ function: stdio-based convenience library for opening/seeking/decoding
+ last mod: $Id: vorbisfile.c 17178 2010-04-26 14:25:54Z xiphmont $
+
+ ********************************************************************/
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <errno.h>
+#include <string.h>
+#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"
+#include "misc.h"
+
+/* A 'chained bitstream' is a Vorbis bitstream that contains more than
+ one logical bitstream arranged end to end (the only form of Ogg
+ multiplexing allowed in a Vorbis bitstream; grouping [parallel
+ multiplexing] is not allowed in Vorbis) */
+
+/* A Vorbis file can be played beginning to end (streamed) without
+ worrying ahead of time about chaining (see decoder_example.c). If
+ we have the whole file, however, and want random access
+ (seeking/scrubbing) or desire to know the total length/time of a
+ file, we need to account for the possibility of chaining. */
+
+/* We can handle things a number of ways; we can determine the entire
+ bitstream structure right off the bat, or find pieces on demand.
+ This example determines and caches structure for the entire
+ bitstream, but builds a virtual decoder on the fly when moving
+ between links in the chain. */
+
+/* There are also different ways to implement seeking. Enough
+ information exists in an Ogg bitstream to seek to
+ sample-granularity positions in the output. Or, one can seek by
+ picking some portion of the stream roughly in the desired area if
+ we only want coarse navigation through the stream. */
+
+/*************************************************************************
+ * Many, many internal helpers. The intention is not to be confusing;
+ * rampant duplication and monolithic function implementation would be
+ * harder to understand anyway. The high level functions are last. Begin
+ * grokking near the end of the file */
+
+/* read a little more data from the file/pipe into the ogg_sync framer
+*/
+#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,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);
+ }else
+ return(0);
+}
+
+/* save a tiny smidge of verbosity to make the code more readable */
+static int _seek_helper(OggVorbis_File *vf,ogg_int64_t offset){
+ if(vf->datasource){
+ if(!(vf->callbacks.seek_func)||
+ (vf->callbacks.seek_func)(vf->datasource, offset, SEEK_SET) == -1)
+ return OV_EREAD;
+ vf->offset=offset;
+ ogg_sync_reset(&vf->oy);
+ }else{
+ /* shouldn't happen unless someone writes a broken callback */
+ return OV_EFAULT;
+ }
+ return 0;
+}
+
+/* The read/seek functions track absolute position within the stream */
+
+/* from the head of the stream, get the next page. boundary specifies
+ if the function is allowed to fetch more data from the stream (and
+ how much) or only use internally buffered data.
+
+ boundary: -1) unbounded search
+ 0) read no additional data; use cached only
+ n) search for a new page beginning for n bytes
+
+ return: <0) did not find a page (OV_FALSE, OV_EOF, OV_EREAD)
+ n) found a page at absolute offset n */
+
+static ogg_int64_t _get_next_page(OggVorbis_File *vf,ogg_page *og,
+ ogg_int64_t boundary){
+ if(boundary>0)boundary+=vf->offset;
+ while(1){
+ long more;
+
+ if(boundary>0 && vf->offset>=boundary)return(OV_FALSE);
+ more=ogg_sync_pageseek(&vf->oy,og);
+
+ if(more<0){
+ /* skipped n bytes */
+ vf->offset-=more;
+ }else{
+ if(more==0){
+ /* send more paramedics */
+ if(!boundary)return(OV_FALSE);
+ {
+ long ret=_get_data(vf);
+ if(ret==0)return(OV_EOF);
+ if(ret<0)return(OV_EREAD);
+ }
+ }else{
+ /* got a page. Return the offset at the page beginning,
+ advance the internal offset past the page end */
+ ogg_int64_t ret=vf->offset;
+ vf->offset+=more;
+ return(ret);
+
+ }
+ }
+ }
+}
+
+/* find the latest page beginning before the current stream cursor
+ position. Much dirtier than the above as Ogg doesn't have any
+ backward search linkage. no 'readp' as it will certainly have to
+ read. */
+/* returns offset or OV_EREAD, OV_FAULT */
+static ogg_int64_t _get_prev_page(OggVorbis_File *vf,ogg_page *og){
+ ogg_int64_t begin=vf->offset;
+ ogg_int64_t end=begin;
+ ogg_int64_t ret;
+ ogg_int64_t offset=-1;
+
+ while(offset==-1){
+ begin-=CHUNKSIZE;
+ if(begin<0)
+ begin=0;
+
+ ret=_seek_helper(vf,begin);
+ if(ret)return(ret);
+
+ while(vf->offset<end){
+ memset(og,0,sizeof(*og));
+ ret=_get_next_page(vf,og,end-vf->offset);
+ if(ret==OV_EREAD)return(OV_EREAD);
+ if(ret<0){
+ break;
+ }else{
+ offset=ret;
+ }
+ }
+ }
+
+ /* In a fully compliant, non-multiplexed stream, we'll still be
+ holding the last page. In multiplexed (or noncompliant streams),
+ we will probably have to re-read the last page we saw */
+ if(og->header_len==0){
+ ret=_seek_helper(vf,offset);
+ if(ret)return(ret);
+
+ ret=_get_next_page(vf,og,CHUNKSIZE);
+ if(ret<0)
+ /* this shouldn't be possible */
+ return(OV_EFAULT);
+ }
+
+ return(offset);
+}
+
+static void _add_serialno(ogg_page *og,long **serialno_list, int *n){
+ long s = ogg_page_serialno(og);
+ (*n)++;
+
+ if(*serialno_list){
+ *serialno_list = _ogg_realloc(*serialno_list, sizeof(**serialno_list)*(*n));
+ }else{
+ *serialno_list = _ogg_malloc(sizeof(**serialno_list));
+ }
+
+ (*serialno_list)[(*n)-1] = s;
+}
+
+/* returns nonzero if found */
+static int _lookup_serialno(long s, long *serialno_list, int n){
+ if(serialno_list){
+ while(n--){
+ if(*serialno_list == s) return 1;
+ serialno_list++;
+ }
+ }
+ return 0;
+}
+
+static int _lookup_page_serialno(ogg_page *og, long *serialno_list, int n){
+ long s = ogg_page_serialno(og);
+ return _lookup_serialno(s,serialno_list,n);
+}
+
+/* performs the same search as _get_prev_page, but prefers pages of
+ the specified serial number. If a page of the specified serialno is
+ spotted during the seek-back-and-read-forward, it will return the
+ info of last page of the matching serial number instead of the very
+ last page. If no page of the specified serialno is seen, it will
+ return the info of last page and alter *serialno. */
+static ogg_int64_t _get_prev_page_serial(OggVorbis_File *vf,
+ long *serial_list, int serial_n,
+ int *serialno, ogg_int64_t *granpos){
+ ogg_page og;
+ ogg_int64_t begin=vf->offset;
+ ogg_int64_t end=begin;
+ ogg_int64_t ret;
+
+ ogg_int64_t prefoffset=-1;
+ ogg_int64_t offset=-1;
+ ogg_int64_t ret_serialno=-1;
+ ogg_int64_t ret_gran=-1;
+
+ while(offset==-1){
+ begin-=CHUNKSIZE;
+ if(begin<0)
+ begin=0;
+
+ ret=_seek_helper(vf,begin);
+ if(ret)return(ret);
+
+ while(vf->offset<end){
+ ret=_get_next_page(vf,&og,end-vf->offset);
+ if(ret==OV_EREAD)return(OV_EREAD);
+ if(ret<0){
+ break;
+ }else{
+ ret_serialno=ogg_page_serialno(&og);
+ ret_gran=ogg_page_granulepos(&og);
+ offset=ret;
+
+ if(ret_serialno == *serialno){
+ prefoffset=ret;
+ *granpos=ret_gran;
+ }
+
+ if(!_lookup_serialno(ret_serialno,serial_list,serial_n)){
+ /* we fell off the end of the link, which means we seeked
+ back too far and shouldn't have been looking in that link
+ to begin with. If we found the preferred serial number,
+ forget that we saw it. */
+ prefoffset=-1;
+ }
+ }
+ }
+ }
+
+ /* we're not interested in the page... just the serialno and granpos. */
+ if(prefoffset>=0)return(prefoffset);
+
+ *serialno = ret_serialno;
+ *granpos = ret_gran;
+ return(offset);
+
+}
+
+/* uses the local ogg_stream storage in vf; this is important for
+ non-streaming input sources */
+static int _fetch_headers(OggVorbis_File *vf,vorbis_info *vi,vorbis_comment *vc,
+ long **serialno_list, int *serialno_n,
+ ogg_page *og_ptr){
+ ogg_page og;
+ ogg_packet op;
+ int i,ret;
+ int allbos=0;
+
+ if(!og_ptr){
+ ogg_int64_t llret=_get_next_page(vf,&og,CHUNKSIZE);
+ if(llret==OV_EREAD)return(OV_EREAD);
+ if(llret<0)return(OV_ENOTVORBIS);
+ og_ptr=&og;
+ }
+
+ vorbis_info_init(vi);
+ vorbis_comment_init(vc);
+ vf->ready_state=OPENED;
+
+ /* extract the serialnos of all BOS pages + the first set of vorbis
+ headers we see in the link */
+
+ while(ogg_page_bos(og_ptr)){
+ if(serialno_list){
+ if(_lookup_page_serialno(og_ptr,*serialno_list,*serialno_n)){
+ /* a dupe serialnumber in an initial header packet set == invalid stream */
+ if(*serialno_list)_ogg_free(*serialno_list);
+ *serialno_list=0;
+ *serialno_n=0;
+ ret=OV_EBADHEADER;
+ goto bail_header;
+ }
+
+ _add_serialno(og_ptr,serialno_list,serialno_n);
+ }
+
+ if(vf->ready_state<STREAMSET){
+ /* we don't have a vorbis stream in this link yet, so begin
+ prospective stream setup. We need a stream to get packets */
+ ogg_stream_reset_serialno(&vf->os,ogg_page_serialno(og_ptr));
+ ogg_stream_pagein(&vf->os,og_ptr);
+
+ if(ogg_stream_packetout(&vf->os,&op) > 0 &&
+ vorbis_synthesis_idheader(&op)){
+ /* vorbis header; continue setup */
+ vf->ready_state=STREAMSET;
+ if((ret=vorbis_synthesis_headerin(vi,vc,&op))){
+ ret=OV_EBADHEADER;
+ goto bail_header;
+ }
+ }
+ }
+
+ /* get next page */
+ {
+ ogg_int64_t llret=_get_next_page(vf,og_ptr,CHUNKSIZE);
+ if(llret==OV_EREAD){
+ ret=OV_EREAD;
+ goto bail_header;
+ }
+ if(llret<0){
+ ret=OV_ENOTVORBIS;
+ goto bail_header;
+ }
+
+ /* if this page also belongs to our vorbis stream, submit it and break */
+ if(vf->ready_state==STREAMSET &&
+ vf->os.serialno == ogg_page_serialno(og_ptr)){
+ ogg_stream_pagein(&vf->os,og_ptr);
+ break;
+ }
+ }
+ }
+
+ if(vf->ready_state!=STREAMSET){
+ ret = OV_ENOTVORBIS;
+ goto bail_header;
+ }
+
+ while(1){
+
+ i=0;
+ while(i<2){ /* get a page loop */
+
+ while(i<2){ /* get a packet loop */
+
+ int result=ogg_stream_packetout(&vf->os,&op);
+ if(result==0)break;
+ if(result==-1){
+ ret=OV_EBADHEADER;
+ goto bail_header;
+ }
+
+ if((ret=vorbis_synthesis_headerin(vi,vc,&op)))
+ goto bail_header;
+
+ i++;
+ }
+
+ while(i<2){
+ if(_get_next_page(vf,og_ptr,CHUNKSIZE)<0){
+ ret=OV_EBADHEADER;
+ goto bail_header;
+ }
+
+ /* if this page belongs to the correct stream, go parse it */
+ if(vf->os.serialno == ogg_page_serialno(og_ptr)){
+ ogg_stream_pagein(&vf->os,og_ptr);
+ break;
+ }
+
+ /* if we never see the final vorbis headers before the link
+ ends, abort */
+ if(ogg_page_bos(og_ptr)){
+ if(allbos){
+ ret = OV_EBADHEADER;
+ goto bail_header;
+ }else
+ allbos=1;
+ }
+
+ /* otherwise, keep looking */
+ }
+ }
+
+ return 0;
+ }
+
+ bail_header:
+ vorbis_info_clear(vi);
+ vorbis_comment_clear(vc);
+ vf->ready_state=OPENED;
+
+ return ret;
+}
+
+/* Starting from current cursor position, get initial PCM offset of
+ next page. Consumes the page in the process without decoding
+ audio, however this is only called during stream parsing upon
+ seekable open. */
+static ogg_int64_t _initial_pcmoffset(OggVorbis_File *vf, vorbis_info *vi){
+ ogg_page og;
+ ogg_int64_t accumulated=0;
+ long lastblock=-1;
+ int result;
+ int serialno = vf->os.serialno;
+
+ while(1){
+ ogg_packet op;
+ if(_get_next_page(vf,&og,-1)<0)
+ break; /* should not be possible unless the file is truncated/mangled */
+
+ if(ogg_page_bos(&og)) break;
+ if(ogg_page_serialno(&og)!=serialno) continue;
+
+ /* count blocksizes of all frames in the page */
+ ogg_stream_pagein(&vf->os,&og);
+ while((result=ogg_stream_packetout(&vf->os,&op))){
+ if(result>0){ /* ignore holes */
+ long thisblock=vorbis_packet_blocksize(vi,&op);
+ if(lastblock!=-1)
+ accumulated+=(lastblock+thisblock)>>2;
+ lastblock=thisblock;
+ }
+ }
+
+ if(ogg_page_granulepos(&og)!=-1){
+ /* pcm offset of last packet on the first audio page */
+ accumulated= ogg_page_granulepos(&og)-accumulated;
+ break;
+ }
+ }
+
+ /* less than zero? This is a stream with samples trimmed off
+ the beginning, a normal occurrence; set the offset to zero */
+ if(accumulated<0)accumulated=0;
+
+ return accumulated;
+}
+
+/* finds each bitstream link one at a time using a bisection search
+ (has to begin by knowing the offset of the lb's initial page).
+ Recurses for each link so it can alloc the link storage after
+ finding them all, then unroll and fill the cache at the same time */
+static int _bisect_forward_serialno(OggVorbis_File *vf,
+ ogg_int64_t begin,
+ ogg_int64_t searched,
+ ogg_int64_t end,
+ ogg_int64_t endgran,
+ int endserial,
+ long *currentno_list,
+ int currentnos,
+ long m){
+ ogg_int64_t pcmoffset;
+ ogg_int64_t dataoffset=searched;
+ ogg_int64_t endsearched=end;
+ ogg_int64_t next=end;
+ ogg_int64_t searchgran=-1;
+ ogg_page og;
+ ogg_int64_t ret,last;
+ int serialno = vf->os.serialno;
+
+ /* invariants:
+ we have the headers and serialnos for the link beginning at 'begin'
+ we have the offset and granpos of the last page in the file (potentially
+ not a page we care about)
+ */
+
+ /* Is the last page in our list of current serialnumbers? */
+ if(_lookup_serialno(endserial,currentno_list,currentnos)){
+
+ /* last page is in the starting serialno list, so we've bisected
+ down to (or just started with) a single link. Now we need to
+ find the last vorbis page belonging to the first vorbis stream
+ for this link. */
+
+ while(endserial != serialno){
+ endserial = serialno;
+ vf->offset=_get_prev_page_serial(vf,currentno_list,currentnos,&endserial,&endgran);
+ }
+
+ vf->links=m+1;
+ if(vf->offsets)_ogg_free(vf->offsets);
+ if(vf->serialnos)_ogg_free(vf->serialnos);
+ if(vf->dataoffsets)_ogg_free(vf->dataoffsets);
+
+ vf->offsets=_ogg_malloc((vf->links+1)*sizeof(*vf->offsets));
+ vf->vi=_ogg_realloc(vf->vi,vf->links*sizeof(*vf->vi));
+ vf->vc=_ogg_realloc(vf->vc,vf->links*sizeof(*vf->vc));
+ vf->serialnos=_ogg_malloc(vf->links*sizeof(*vf->serialnos));
+ vf->dataoffsets=_ogg_malloc(vf->links*sizeof(*vf->dataoffsets));
+ vf->pcmlengths=_ogg_malloc(vf->links*2*sizeof(*vf->pcmlengths));
+
+ vf->offsets[m+1]=end;
+ vf->offsets[m]=begin;
+ vf->pcmlengths[m*2+1]=endgran;
+
+ }else{
+
+ long *next_serialno_list=NULL;
+ int next_serialnos=0;
+ vorbis_info vi;
+ vorbis_comment vc;
+
+ /* the below guards against garbage seperating the last and
+ first pages of two links. */
+ while(searched<endsearched){
+ ogg_int64_t bisect;
+
+ if(endsearched-searched<CHUNKSIZE){
+ bisect=searched;
+ }else{
+ bisect=(searched+endsearched)/2;
+ }
+
+ if(bisect != vf->offset){
+ ret=_seek_helper(vf,bisect);
+ if(ret)return(ret);
+ }
+
+ last=_get_next_page(vf,&og,-1);
+ if(last==OV_EREAD)return(OV_EREAD);
+ if(last<0 || !_lookup_page_serialno(&og,currentno_list,currentnos)){
+ endsearched=bisect;
+ if(last>=0)next=last;
+ }else{
+ searched=vf->offset;
+ }
+ }
+
+ /* Bisection point found */
+
+ /* for the time being, fetch end PCM offset the simple way */
+ {
+ int testserial = serialno+1;
+ vf->offset = next;
+ while(testserial != serialno){
+ testserial = serialno;
+ vf->offset=_get_prev_page_serial(vf,currentno_list,currentnos,&testserial,&searchgran);
+ }
+ }
+
+ if(vf->offset!=next){
+ ret=_seek_helper(vf,next);
+ if(ret)return(ret);
+ }
+
+ ret=_fetch_headers(vf,&vi,&vc,&next_serialno_list,&next_serialnos,NULL);
+ if(ret)return(ret);
+ serialno = vf->os.serialno;
+ dataoffset = vf->offset;
+
+ /* this will consume a page, however the next bistection always
+ starts with a raw seek */
+ pcmoffset = _initial_pcmoffset(vf,&vi);
+
+ ret=_bisect_forward_serialno(vf,next,vf->offset,end,endgran,endserial,
+ next_serialno_list,next_serialnos,m+1);
+ if(ret)return(ret);
+
+ if(next_serialno_list)_ogg_free(next_serialno_list);
+
+ vf->offsets[m+1]=next;
+ vf->serialnos[m+1]=serialno;
+ vf->dataoffsets[m+1]=dataoffset;
+
+ vf->vi[m+1]=vi;
+ vf->vc[m+1]=vc;
+
+ vf->pcmlengths[m*2+1]=searchgran;
+ vf->pcmlengths[m*2+2]=pcmoffset;
+ vf->pcmlengths[m*2+3]-=pcmoffset;
+
+ }
+ return(0);
+}
+
+static int _make_decode_ready(OggVorbis_File *vf){
+ if(vf->ready_state>STREAMSET)return 0;
+ if(vf->ready_state<STREAMSET)return OV_EFAULT;
+ if(vf->seekable){
+ if(vorbis_synthesis_init(&vf->vd,vf->vi+vf->current_link))
+ return OV_EBADLINK;
+ }else{
+ if(vorbis_synthesis_init(&vf->vd,vf->vi))
+ return OV_EBADLINK;
+ }
+ vorbis_block_init(&vf->vd,&vf->vb);
+ vf->ready_state=INITSET;
+ vf->bittrack=0.f;
+ vf->samptrack=0.f;
+ return 0;
+}
+
+static int _open_seekable2(OggVorbis_File *vf){
+ ogg_int64_t dataoffset=vf->dataoffsets[0],end,endgran=-1;
+ int endserial=vf->os.serialno;
+ int serialno=vf->os.serialno;
+
+ /* we're partially open and have a first link header state in
+ storage in vf */
+
+ /* fetch initial PCM offset */
+ ogg_int64_t pcmoffset = _initial_pcmoffset(vf,vf->vi);
+
+ /* we can seek, so set out learning all about this file */
+ if(vf->callbacks.seek_func && vf->callbacks.tell_func){
+ (vf->callbacks.seek_func)(vf->datasource,0,SEEK_END);
+ vf->offset=vf->end=(vf->callbacks.tell_func)(vf->datasource);
+ }else{
+ vf->offset=vf->end=-1;
+ }
+
+ /* If seek_func is implemented, tell_func must also be implemented */
+ if(vf->end==-1) return(OV_EINVAL);
+
+ /* Get the offset of the last page of the physical bitstream, or, if
+ we're lucky the last vorbis page of this link as most OggVorbis
+ files will contain a single logical bitstream */
+ end=_get_prev_page_serial(vf,vf->serialnos+2,vf->serialnos[1],&endserial,&endgran);
+ if(end<0)return(end);
+
+ /* now determine bitstream structure recursively */
+ if(_bisect_forward_serialno(vf,0,dataoffset,vf->offset,endgran,endserial,
+ vf->serialnos+2,vf->serialnos[1],0)<0)return(OV_EREAD);
+
+ vf->offsets[0]=0;
+ vf->serialnos[0]=serialno;
+ vf->dataoffsets[0]=dataoffset;
+ vf->pcmlengths[0]=pcmoffset;
+ vf->pcmlengths[1]-=pcmoffset;
+
+ return(ov_raw_seek(vf,dataoffset));
+}
+
+/* clear out the current logical bitstream decoder */
+static void _decode_clear(OggVorbis_File *vf){
+ vorbis_dsp_clear(&vf->vd);
+ vorbis_block_clear(&vf->vb);
+ vf->ready_state=OPENED;
+}
+
+/* fetch and process a packet. Handles the case where we're at a
+ bitstream boundary and dumps the decoding machine. If the decoding
+ machine is unloaded, it loads it. It also keeps pcm_offset up to
+ date (seek and read both use this. seek uses a special hack with
+ readp).
+
+ return: <0) error, OV_HOLE (lost packet) or OV_EOF
+ 0) need more data (only if readp==0)
+ 1) got a packet
+*/
+
+static int _fetch_and_process_packet(OggVorbis_File *vf,
+ ogg_packet *op_in,
+ int readp,
+ int spanp){
+ ogg_page og;
+
+ /* handle one packet. Try to fetch it from current stream state */
+ /* extract packets from page */
+ while(1){
+
+ if(vf->ready_state==STREAMSET){
+ int ret=_make_decode_ready(vf);
+ if(ret<0)return ret;
+ }
+
+ /* process a packet if we can. */
+
+ if(vf->ready_state==INITSET){
+ while(1) {
+ ogg_packet op;
+ ogg_packet *op_ptr=(op_in?op_in:&op);
+ int result=ogg_stream_packetout(&vf->os,op_ptr);
+ ogg_int64_t granulepos;
+
+ op_in=NULL;
+ if(result==-1)return(OV_HOLE); /* hole in the data. */
+ if(result>0){
+ /* got a packet. process it */
+ granulepos=op_ptr->granulepos;
+ if(!vorbis_synthesis(&vf->vb,op_ptr)){ /* lazy check for lazy
+ header handling. The
+ header packets aren't
+ audio, so if/when we
+ submit them,
+ vorbis_synthesis will
+ reject them */
+
+ /* suck in the synthesis data and track bitrate */
+ {
+ int oldsamples=vorbis_synthesis_pcmout(&vf->vd,NULL);
+ /* for proper use of libvorbis within libvorbisfile,
+ oldsamples will always be zero. */
+ if(oldsamples)return(OV_EFAULT);
+
+ vorbis_synthesis_blockin(&vf->vd,&vf->vb);
+ vf->samptrack+=vorbis_synthesis_pcmout(&vf->vd,NULL)-oldsamples;
+ vf->bittrack+=op_ptr->bytes*8;
+ }
+
+ /* update the pcm offset. */
+ if(granulepos!=-1 && !op_ptr->e_o_s){
+ int link=(vf->seekable?vf->current_link:0);
+ int i,samples;
+
+ /* this packet has a pcm_offset on it (the last packet
+ completed on a page carries the offset) After processing
+ (above), we know the pcm position of the *last* sample
+ ready to be returned. Find the offset of the *first*
+
+ As an aside, this trick is inaccurate if we begin
+ reading anew right at the last page; the end-of-stream
+ granulepos declares the last frame in the stream, and the
+ last packet of the last page may be a partial frame.
+ So, we need a previous granulepos from an in-sequence page
+ to have a reference point. Thus the !op_ptr->e_o_s clause
+ above */
+
+ if(vf->seekable && link>0)
+ granulepos-=vf->pcmlengths[link*2];
+ if(granulepos<0)granulepos=0; /* actually, this
+ shouldn't be possible
+ here unless the stream
+ is very broken */
+
+ samples=vorbis_synthesis_pcmout(&vf->vd,NULL);
+
+ granulepos-=samples;
+ for(i=0;i<link;i++)
+ granulepos+=vf->pcmlengths[i*2+1];
+ vf->pcm_offset=granulepos;
+ }
+ return(1);
+ }
+ }
+ else
+ break;
+ }
+ }
+
+ if(vf->ready_state>=OPENED){
+ ogg_int64_t ret;
+
+ while(1){
+ /* the loop is not strictly necessary, but there's no sense in
+ doing the extra checks of the larger loop for the common
+ case in a multiplexed bistream where the page is simply
+ part of a different logical bitstream; keep reading until
+ we get one with the correct serialno */
+
+ if(!readp)return(0);
+ if((ret=_get_next_page(vf,&og,-1))<0){
+ return(OV_EOF); /* eof. leave unitialized */
+ }
+
+ /* bitrate tracking; add the header's bytes here, the body bytes
+ are done by packet above */
+ vf->bittrack+=og.header_len*8;
+
+ if(vf->ready_state==INITSET){
+ if(vf->current_serialno!=ogg_page_serialno(&og)){
+
+ /* two possibilities:
+ 1) our decoding just traversed a bitstream boundary
+ 2) another stream is multiplexed into this logical section */
+
+ if(ogg_page_bos(&og)){
+ /* boundary case */
+ if(!spanp)
+ return(OV_EOF);
+
+ _decode_clear(vf);
+
+ if(!vf->seekable){
+ vorbis_info_clear(vf->vi);
+ vorbis_comment_clear(vf->vc);
+ }
+ break;
+
+ }else
+ continue; /* possibility #2 */
+ }
+ }
+
+ break;
+ }
+ }
+
+ /* Do we need to load a new machine before submitting the page? */
+ /* This is different in the seekable and non-seekable cases.
+
+ In the seekable case, we already have all the header
+ information loaded and cached; we just initialize the machine
+ with it and continue on our merry way.
+
+ In the non-seekable (streaming) case, we'll only be at a
+ boundary if we just left the previous logical bitstream and
+ we're now nominally at the header of the next bitstream
+ */
+
+ if(vf->ready_state!=INITSET){
+ int link;
+
+ if(vf->ready_state<STREAMSET){
+ if(vf->seekable){
+ long serialno = ogg_page_serialno(&og);
+
+ /* match the serialno to bitstream section. We use this rather than
+ offset positions to avoid problems near logical bitstream
+ boundaries */
+
+ for(link=0;link<vf->links;link++)
+ if(vf->serialnos[link]==serialno)break;
+
+ if(link==vf->links) continue; /* not the desired Vorbis
+ bitstream section; keep
+ trying */
+
+ vf->current_serialno=serialno;
+ vf->current_link=link;
+
+ ogg_stream_reset_serialno(&vf->os,vf->current_serialno);
+ vf->ready_state=STREAMSET;
+
+ }else{
+ /* we're streaming */
+ /* fetch the three header packets, build the info struct */
+
+ int ret=_fetch_headers(vf,vf->vi,vf->vc,NULL,NULL,&og);
+ if(ret)return(ret);
+ vf->current_serialno=vf->os.serialno;
+ vf->current_link++;
+ link=0;
+ }
+ }
+ }
+
+ /* the buffered page is the data we want, and we're ready for it;
+ add it to the stream state */
+ ogg_stream_pagein(&vf->os,&og);
+
+ }
+}
+
+/* if, eg, 64 bit stdio is configured by default, this will build with
+ fseek64 */
+static int _fseek64_wrap(FILE *f,ogg_int64_t off,int whence){
+ if(f==NULL)return(-1);
+ return fseek(f,off,whence);
+}
+
+static int _ov_open1(void *f,OggVorbis_File *vf,char *initial,
+ long ibytes, ov_callbacks callbacks){
+ int offsettest=((f && callbacks.seek_func)?callbacks.seek_func(f,0,SEEK_CUR):-1);
+ long *serialno_list=NULL;
+ int serialno_list_size=0;
+ int ret;
+
+ memset(vf,0,sizeof(*vf));
+ vf->datasource=f;
+ vf->callbacks = callbacks;
+
+ /* init the framing state */
+ ogg_sync_init(&vf->oy);
+
+ /* perhaps some data was previously read into a buffer for testing
+ against other stream types. Allow initialization from this
+ previously read data (especially as we may be reading from a
+ non-seekable stream) */
+ if(initial){
+ char *buffer=ogg_sync_buffer(&vf->oy,ibytes);
+ memcpy(buffer,initial,ibytes);
+ ogg_sync_wrote(&vf->oy,ibytes);
+ }
+
+ /* can we seek? Stevens suggests the seek test was portable */
+ if(offsettest!=-1)vf->seekable=1;
+
+ /* No seeking yet; Set up a 'single' (current) logical bitstream
+ entry for partial open */
+ vf->links=1;
+ vf->vi=_ogg_calloc(vf->links,sizeof(*vf->vi));
+ vf->vc=_ogg_calloc(vf->links,sizeof(*vf->vc));
+ ogg_stream_init(&vf->os,-1); /* fill in the serialno later */
+
+ /* Fetch all BOS pages, store the vorbis header and all seen serial
+ numbers, load subsequent vorbis setup headers */
+ if((ret=_fetch_headers(vf,vf->vi,vf->vc,&serialno_list,&serialno_list_size,NULL))<0){
+ vf->datasource=NULL;
+ ov_clear(vf);
+ }else{
+ /* serial number list for first link needs to be held somewhere
+ for second stage of seekable stream open; this saves having to
+ seek/reread first link's serialnumber data then. */
+ vf->serialnos=_ogg_calloc(serialno_list_size+2,sizeof(*vf->serialnos));
+ vf->serialnos[0]=vf->current_serialno;
+ vf->serialnos[1]=serialno_list_size;
+ memcpy(vf->serialnos+2,serialno_list,serialno_list_size*sizeof(*vf->serialnos));
+
+ vf->offsets=_ogg_calloc(1,sizeof(*vf->offsets));
+ vf->dataoffsets=_ogg_calloc(1,sizeof(*vf->dataoffsets));
+ vf->offsets[0]=0;
+ vf->dataoffsets[0]=vf->offset;
+ vf->current_serialno=vf->os.serialno;
+
+ vf->ready_state=PARTOPEN;
+ }
+ if(serialno_list)_ogg_free(serialno_list);
+ return(ret);
+}
+
+static int _ov_open2(OggVorbis_File *vf){
+ if(vf->ready_state != PARTOPEN) return OV_EINVAL;
+ vf->ready_state=OPENED;
+ if(vf->seekable){
+ int ret=_open_seekable2(vf);
+ if(ret){
+ vf->datasource=NULL;
+ ov_clear(vf);
+ }
+ return(ret);
+ }else
+ vf->ready_state=STREAMSET;
+
+ return 0;
+}
+
+
+/* clear out the OggVorbis_File struct */
+int ov_clear(OggVorbis_File *vf){
+ if(vf){
+ vorbis_block_clear(&vf->vb);
+ vorbis_dsp_clear(&vf->vd);
+ ogg_stream_clear(&vf->os);
+
+ if(vf->vi && vf->links){
+ int i;
+ for(i=0;i<vf->links;i++){
+ vorbis_info_clear(vf->vi+i);
+ vorbis_comment_clear(vf->vc+i);
+ }
+ _ogg_free(vf->vi);
+ _ogg_free(vf->vc);
+ }
+ if(vf->dataoffsets)_ogg_free(vf->dataoffsets);
+ if(vf->pcmlengths)_ogg_free(vf->pcmlengths);
+ if(vf->serialnos)_ogg_free(vf->serialnos);
+ if(vf->offsets)_ogg_free(vf->offsets);
+ ogg_sync_clear(&vf->oy);
+ if(vf->datasource && vf->callbacks.close_func)
+ (vf->callbacks.close_func)(vf->datasource);
+ memset(vf,0,sizeof(*vf));
+ }
+#ifdef DEBUG_LEAKS
+ _VDBG_dump();
+#endif
+ return(0);
+}
+
+/* inspects the OggVorbis file and finds/documents all the logical
+ bitstreams contained in it. Tries to be tolerant of logical
+ bitstream sections that are truncated/woogie.
+
+ return: -1) error
+ 0) OK
+*/
+
+int ov_open_callbacks(void *f,OggVorbis_File *vf,char *initial,long ibytes,
+ ov_callbacks callbacks){
+ int ret=_ov_open1(f,vf,initial,ibytes,callbacks);
+ if(ret)return ret;
+ return _ov_open2(vf);
+}
+
+int ov_open(FILE *f,OggVorbis_File *vf,char *initial,long ibytes){
+ ov_callbacks callbacks = {
+ (size_t (*)(void *, size_t, size_t, void *)) fread,
+ (int (*)(void *, ogg_int64_t, int)) _fseek64_wrap,
+ (int (*)(void *)) fclose,
+ (long (*)(void *)) ftell
+ };
+
+ return ov_open_callbacks((void *)f, vf, initial, ibytes, callbacks);
+}
+
+int ov_fopen(char *path,OggVorbis_File *vf){
+ int ret;
+ FILE *f = fopen(path,"rb");
+ if(!f) return -1;
+
+ ret = ov_open(f,vf,NULL,0);
+ if(ret) fclose(f);
+ return ret;
+}
+
+
+/* cheap hack for game usage where downsampling is desirable; there's
+ no need for SRC as we can just do it cheaply in libvorbis. */
+
+int ov_halfrate(OggVorbis_File *vf,int flag){
+ int i;
+ if(vf->vi==NULL)return OV_EINVAL;
+ if(!vf->seekable)return OV_EINVAL;
+ if(vf->ready_state>=STREAMSET)
+ _decode_clear(vf); /* clear out stream state; later on libvorbis
+ will be able to swap this on the fly, but
+ for now dumping the decode machine is needed
+ to reinit the MDCT lookups. 1.1 libvorbis
+ is planned to be able to switch on the fly */
+
+ for(i=0;i<vf->links;i++){
+ if(vorbis_synthesis_halfrate(vf->vi+i,flag)){
+ ov_halfrate(vf,0);
+ return OV_EINVAL;
+ }
+ }
+ return 0;
+}
+
+int ov_halfrate_p(OggVorbis_File *vf){
+ if(vf->vi==NULL)return OV_EINVAL;
+ return vorbis_synthesis_halfrate_p(vf->vi);
+}
+
+/* Only partially open the vorbis file; test for Vorbisness, and load
+ the headers for the first chain. Do not seek (although test for
+ seekability). Use ov_test_open to finish opening the file, else
+ ov_clear to close/free it. Same return codes as open. */
+
+int ov_test_callbacks(void *f,OggVorbis_File *vf,char *initial,long ibytes,
+ ov_callbacks callbacks)
+{
+ return _ov_open1(f,vf,initial,ibytes,callbacks);
+}
+
+int ov_test(FILE *f,OggVorbis_File *vf,char *initial,long ibytes){
+ ov_callbacks callbacks = {
+ (size_t (*)(void *, size_t, size_t, void *)) fread,
+ (int (*)(void *, ogg_int64_t, int)) _fseek64_wrap,
+ (int (*)(void *)) fclose,
+ (long (*)(void *)) ftell
+ };
+
+ return ov_test_callbacks((void *)f, vf, initial, ibytes, callbacks);
+}
+
+int ov_test_open(OggVorbis_File *vf){
+ if(vf->ready_state!=PARTOPEN)return(OV_EINVAL);
+ return _ov_open2(vf);
+}
+
+/* How many logical bitstreams in this physical bitstream? */
+long ov_streams(OggVorbis_File *vf){
+ return vf->links;
+}
+
+/* Is the FILE * associated with vf seekable? */
+long ov_seekable(OggVorbis_File *vf){
+ return vf->seekable;
+}
+
+/* returns the bitrate for a given logical bitstream or the entire
+ physical bitstream. If the file is open for random access, it will
+ find the *actual* average bitrate. If the file is streaming, it
+ returns the nominal bitrate (if set) else the average of the
+ upper/lower bounds (if set) else -1 (unset).
+
+ If you want the actual bitrate field settings, get them from the
+ vorbis_info structs */
+
+long ov_bitrate(OggVorbis_File *vf,int i){
+ if(vf->ready_state<OPENED)return(OV_EINVAL);
+ if(i>=vf->links)return(OV_EINVAL);
+ if(!vf->seekable && i!=0)return(ov_bitrate(vf,0));
+ if(i<0){
+ ogg_int64_t bits=0;
+ int i;
+ float br;
+ for(i=0;i<vf->links;i++)
+ bits+=(vf->offsets[i+1]-vf->dataoffsets[i])*8;
+ /* This once read: return(rint(bits/ov_time_total(vf,-1)));
+ * gcc 3.x on x86 miscompiled this at optimisation level 2 and above,
+ * so this is slightly transformed to make it work.
+ */
+ br = bits/ov_time_total(vf,-1);
+ return(rint(br));
+ }else{
+ if(vf->seekable){
+ /* return the actual bitrate */
+ return(rint((vf->offsets[i+1]-vf->dataoffsets[i])*8/ov_time_total(vf,i)));
+ }else{
+ /* return nominal if set */
+ if(vf->vi[i].bitrate_nominal>0){
+ return vf->vi[i].bitrate_nominal;
+ }else{
+ if(vf->vi[i].bitrate_upper>0){
+ if(vf->vi[i].bitrate_lower>0){
+ return (vf->vi[i].bitrate_upper+vf->vi[i].bitrate_lower)/2;
+ }else{
+ return vf->vi[i].bitrate_upper;
+ }
+ }
+ return(OV_FALSE);
+ }
+ }
+ }
+}
+
+/* returns the actual bitrate since last call. returns -1 if no
+ additional data to offer since last call (or at beginning of stream),
+ EINVAL if stream is only partially open
+*/
+long ov_bitrate_instant(OggVorbis_File *vf){
+ int link=(vf->seekable?vf->current_link:0);
+ long ret;
+ if(vf->ready_state<OPENED)return(OV_EINVAL);
+ if(vf->samptrack==0)return(OV_FALSE);
+ ret=vf->bittrack/vf->samptrack*vf->vi[link].rate+.5;
+ vf->bittrack=0.f;
+ vf->samptrack=0.f;
+ return(ret);
+}
+
+/* Guess */
+long ov_serialnumber(OggVorbis_File *vf,int i){
+ if(i>=vf->links)return(ov_serialnumber(vf,vf->links-1));
+ if(!vf->seekable && i>=0)return(ov_serialnumber(vf,-1));
+ if(i<0){
+ return(vf->current_serialno);
+ }else{
+ return(vf->serialnos[i]);
+ }
+}
+
+/* returns: total raw (compressed) length of content if i==-1
+ raw (compressed) length of that logical bitstream for i==0 to n
+ OV_EINVAL if the stream is not seekable (we can't know the length)
+ or if stream is only partially open
+*/
+ogg_int64_t ov_raw_total(OggVorbis_File *vf,int i){
+ if(vf->ready_state<OPENED)return(OV_EINVAL);
+ if(!vf->seekable || i>=vf->links)return(OV_EINVAL);
+ if(i<0){
+ ogg_int64_t acc=0;
+ int i;
+ for(i=0;i<vf->links;i++)
+ acc+=ov_raw_total(vf,i);
+ return(acc);
+ }else{
+ return(vf->offsets[i+1]-vf->offsets[i]);
+ }
+}
+
+/* returns: total PCM length (samples) of content if i==-1 PCM length
+ (samples) of that logical bitstream for i==0 to n
+ OV_EINVAL if the stream is not seekable (we can't know the
+ length) or only partially open
+*/
+ogg_int64_t ov_pcm_total(OggVorbis_File *vf,int i){
+ if(vf->ready_state<OPENED)return(OV_EINVAL);
+ if(!vf->seekable || i>=vf->links)return(OV_EINVAL);
+ if(i<0){
+ ogg_int64_t acc=0;
+ int i;
+ for(i=0;i<vf->links;i++)
+ acc+=ov_pcm_total(vf,i);
+ return(acc);
+ }else{
+ return(vf->pcmlengths[i*2+1]);
+ }
+}
+
+/* returns: total seconds of content if i==-1
+ seconds in that logical bitstream for i==0 to n
+ OV_EINVAL if the stream is not seekable (we can't know the
+ length) or only partially open
+*/
+double ov_time_total(OggVorbis_File *vf,int i){
+ if(vf->ready_state<OPENED)return(OV_EINVAL);
+ if(!vf->seekable || i>=vf->links)return(OV_EINVAL);
+ if(i<0){
+ double acc=0;
+ int i;
+ for(i=0;i<vf->links;i++)
+ acc+=ov_time_total(vf,i);
+ return(acc);
+ }else{
+ return((double)(vf->pcmlengths[i*2+1])/vf->vi[i].rate);
+ }
+}
+
+/* seek to an offset relative to the *compressed* data. This also
+ scans packets to update the PCM cursor. It will cross a logical
+ bitstream boundary, but only if it can't get any packets out of the
+ tail of the bitstream we seek to (so no surprises).
+
+ returns zero on success, nonzero on failure */
+
+int ov_raw_seek(OggVorbis_File *vf,ogg_int64_t pos){
+ ogg_stream_state work_os;
+ int ret;
+
+ if(vf->ready_state<OPENED)return(OV_EINVAL);
+ if(!vf->seekable)
+ return(OV_ENOSEEK); /* don't dump machine if we can't seek */
+
+ if(pos<0 || pos>vf->end)return(OV_EINVAL);
+
+ /* is the seek position outside our current link [if any]? */
+ if(vf->ready_state>=STREAMSET){
+ if(pos<vf->offsets[vf->current_link] || pos>=vf->offsets[vf->current_link+1])
+ _decode_clear(vf); /* clear out stream state */
+ }
+
+ /* don't yet clear out decoding machine (if it's initialized), in
+ the case we're in the same link. Restart the decode lapping, and
+ let _fetch_and_process_packet deal with a potential bitstream
+ boundary */
+ vf->pcm_offset=-1;
+ ogg_stream_reset_serialno(&vf->os,
+ vf->current_serialno); /* must set serialno */
+ vorbis_synthesis_restart(&vf->vd);
+
+ ret=_seek_helper(vf,pos);
+ if(ret)goto seek_error;
+
+ /* we need to make sure the pcm_offset is set, but we don't want to
+ advance the raw cursor past good packets just to get to the first
+ with a granulepos. That's not equivalent behavior to beginning
+ decoding as immediately after the seek position as possible.
+
+ So, a hack. We use two stream states; a local scratch state and
+ the shared vf->os stream state. We use the local state to
+ scan, and the shared state as a buffer for later decode.
+
+ Unfortuantely, on the last page we still advance to last packet
+ because the granulepos on the last page is not necessarily on a
+ packet boundary, and we need to make sure the granpos is
+ correct.
+ */
+
+ {
+ ogg_page og;
+ ogg_packet op;
+ int lastblock=0;
+ int accblock=0;
+ int thisblock=0;
+ int lastflag=0;
+ int firstflag=0;
+ ogg_int64_t pagepos=-1;
+
+ ogg_stream_init(&work_os,vf->current_serialno); /* get the memory ready */
+ ogg_stream_reset(&work_os); /* eliminate the spurious OV_HOLE
+ return from not necessarily
+ starting from the beginning */
+
+ while(1){
+ if(vf->ready_state>=STREAMSET){
+ /* snarf/scan a packet if we can */
+ int result=ogg_stream_packetout(&work_os,&op);
+
+ if(result>0){
+
+ if(vf->vi[vf->current_link].codec_setup){
+ thisblock=vorbis_packet_blocksize(vf->vi+vf->current_link,&op);
+ if(thisblock<0){
+ ogg_stream_packetout(&vf->os,NULL);
+ thisblock=0;
+ }else{
+
+ /* We can't get a guaranteed correct pcm position out of the
+ last page in a stream because it might have a 'short'
+ granpos, which can only be detected in the presence of a
+ preceeding page. However, if the last page is also the first
+ page, the granpos rules of a first page take precedence. Not
+ only that, but for first==last, the EOS page must be treated
+ as if its a normal first page for the stream to open/play. */
+ if(lastflag && !firstflag)
+ ogg_stream_packetout(&vf->os,NULL);
+ else
+ if(lastblock)accblock+=(lastblock+thisblock)>>2;
+ }
+
+ if(op.granulepos!=-1){
+ int i,link=vf->current_link;
+ ogg_int64_t granulepos=op.granulepos-vf->pcmlengths[link*2];
+ if(granulepos<0)granulepos=0;
+
+ for(i=0;i<link;i++)
+ granulepos+=vf->pcmlengths[i*2+1];
+ vf->pcm_offset=granulepos-accblock;
+ if(vf->pcm_offset<0)vf->pcm_offset=0;
+ break;
+ }
+ lastblock=thisblock;
+ continue;
+ }else
+ ogg_stream_packetout(&vf->os,NULL);
+ }
+ }
+
+ if(!lastblock){
+ pagepos=_get_next_page(vf,&og,-1);
+ if(pagepos<0){
+ vf->pcm_offset=ov_pcm_total(vf,-1);
+ break;
+ }
+ }else{
+ /* huh? Bogus stream with packets but no granulepos */
+ vf->pcm_offset=-1;
+ break;
+ }
+
+ /* has our decoding just traversed a bitstream boundary? */
+ if(vf->ready_state>=STREAMSET){
+ if(vf->current_serialno!=ogg_page_serialno(&og)){
+
+ /* two possibilities:
+ 1) our decoding just traversed a bitstream boundary
+ 2) another stream is multiplexed into this logical section? */
+
+ if(ogg_page_bos(&og)){
+ /* we traversed */
+ _decode_clear(vf); /* clear out stream state */
+ ogg_stream_clear(&work_os);
+ } /* else, do nothing; next loop will scoop another page */
+ }
+ }
+
+ if(vf->ready_state<STREAMSET){
+ int link;
+ long serialno = ogg_page_serialno(&og);
+
+ for(link=0;link<vf->links;link++)
+ if(vf->serialnos[link]==serialno)break;
+
+ if(link==vf->links) continue; /* not the desired Vorbis
+ bitstream section; keep
+ trying */
+ vf->current_link=link;
+ vf->current_serialno=serialno;
+ ogg_stream_reset_serialno(&vf->os,serialno);
+ ogg_stream_reset_serialno(&work_os,serialno);
+ vf->ready_state=STREAMSET;
+ firstflag=(pagepos<=vf->dataoffsets[link]);
+ }
+
+ ogg_stream_pagein(&vf->os,&og);
+ ogg_stream_pagein(&work_os,&og);
+ lastflag=ogg_page_eos(&og);
+
+ }
+ }
+
+ ogg_stream_clear(&work_os);
+ vf->bittrack=0.f;
+ vf->samptrack=0.f;
+ return(0);
+
+ seek_error:
+ /* dump the machine so we're in a known state */
+ vf->pcm_offset=-1;
+ ogg_stream_clear(&work_os);
+ _decode_clear(vf);
+ return OV_EBADLINK;
+}
+
+/* Page granularity seek (faster than sample granularity because we
+ don't do the last bit of decode to find a specific sample).
+
+ Seek to the last [granule marked] page preceeding the specified pos
+ location, such that decoding past the returned point will quickly
+ arrive at the requested position. */
+int ov_pcm_seek_page(OggVorbis_File *vf,ogg_int64_t pos){
+ int link=-1;
+ ogg_int64_t result=0;
+ ogg_int64_t total=ov_pcm_total(vf,-1);
+
+ if(vf->ready_state<OPENED)return(OV_EINVAL);
+ if(!vf->seekable)return(OV_ENOSEEK);
+
+ if(pos<0 || pos>total)return(OV_EINVAL);
+
+ /* which bitstream section does this pcm offset occur in? */
+ for(link=vf->links-1;link>=0;link--){
+ total-=vf->pcmlengths[link*2+1];
+ if(pos>=total)break;
+ }
+
+ /* search within the logical bitstream for the page with the highest
+ pcm_pos preceeding (or equal to) pos. There is a danger here;
+ missing pages or incorrect frame number information in the
+ bitstream could make our task impossible. Account for that (it
+ would be an error condition) */
+
+ /* new search algorithm by HB (Nicholas Vinen) */
+ {
+ ogg_int64_t end=vf->offsets[link+1];
+ ogg_int64_t begin=vf->offsets[link];
+ ogg_int64_t begintime = vf->pcmlengths[link*2];
+ ogg_int64_t endtime = vf->pcmlengths[link*2+1]+begintime;
+ ogg_int64_t target=pos-total+begintime;
+ ogg_int64_t best=begin;
+
+ ogg_page og;
+ while(begin<end){
+ ogg_int64_t bisect;
+
+ if(end-begin<CHUNKSIZE){
+ bisect=begin;
+ }else{
+ /* take a (pretty decent) guess. */
+ bisect=begin +
+ (ogg_int64_t)((double)(target-begintime)*(end-begin)/(endtime-begintime))
+ - CHUNKSIZE;
+ if(bisect<begin+CHUNKSIZE)
+ bisect=begin;
+ }
+
+ if(bisect!=vf->offset){
+ 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;
+ 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;
+ }
+ }
+ }
+ }
+ }
+ }
+
+ /* found our page. seek to it, update pcm offset. Easier case than
+ raw_seek, don't keep packets preceeding granulepos. */
+ {
+ ogg_page og;
+ ogg_packet op;
+
+ /* seek */
+ result=_seek_helper(vf,best);
+ vf->pcm_offset=-1;
+ if(result) goto seek_error;
+ result=_get_next_page(vf,&og,-1);
+ if(result<0) goto seek_error;
+
+ if(link!=vf->current_link){
+ /* Different link; dump entire decode machine */
+ _decode_clear(vf);
+
+ vf->current_link=link;
+ vf->current_serialno=vf->serialnos[link];
+ vf->ready_state=STREAMSET;
+
+ }else{
+ vorbis_synthesis_restart(&vf->vd);
+ }
+
+ ogg_stream_reset_serialno(&vf->os,vf->current_serialno);
+ ogg_stream_pagein(&vf->os,&og);
+
+ /* pull out all but last packet; the one with granulepos */
+ while(1){
+ result=ogg_stream_packetpeek(&vf->os,&op);
+ if(result==0){
+ /* !!! the packet finishing this page originated on a
+ preceeding page. Keep fetching previous pages until we
+ get one with a granulepos or without the 'continued' flag
+ set. Then just use raw_seek for simplicity. */
+
+ result=_seek_helper(vf,best);
+ if(result<0) goto seek_error;
+
+ while(1){
+ result=_get_prev_page(vf,&og);
+ if(result<0) goto seek_error;
+ if(ogg_page_serialno(&og)==vf->current_serialno &&
+ (ogg_page_granulepos(&og)>-1 ||
+ !ogg_page_continued(&og))){
+ return ov_raw_seek(vf,result);
+ }
+ vf->offset=result;
+ }
+ }
+ if(result<0){
+ result = OV_EBADPACKET;
+ goto seek_error;
+ }
+ if(op.granulepos!=-1){
+ vf->pcm_offset=op.granulepos-vf->pcmlengths[vf->current_link*2];
+ if(vf->pcm_offset<0)vf->pcm_offset=0;
+ vf->pcm_offset+=total;
+ break;
+ }else
+ result=ogg_stream_packetout(&vf->os,NULL);
+ }
+ }
+ }
+
+ /* verify result */
+ if(vf->pcm_offset>pos || pos>ov_pcm_total(vf,-1)){
+ result=OV_EFAULT;
+ goto seek_error;
+ }
+ vf->bittrack=0.f;
+ vf->samptrack=0.f;
+ return(0);
+
+ seek_error:
+ /* dump machine so we're in a known state */
+ vf->pcm_offset=-1;
+ _decode_clear(vf);
+ return (int)result;
+}
+
+/* seek to a sample offset relative to the decompressed pcm stream
+ returns zero on success, nonzero on failure */
+
+int ov_pcm_seek(OggVorbis_File *vf,ogg_int64_t pos){
+ int thisblock,lastblock=0;
+ int ret=ov_pcm_seek_page(vf,pos);
+ if(ret<0)return(ret);
+ if((ret=_make_decode_ready(vf)))return ret;
+
+ /* discard leading packets we don't need for the lapping of the
+ position we want; don't decode them */
+
+ while(1){
+ ogg_packet op;
+ ogg_page og;
+
+ int ret=ogg_stream_packetpeek(&vf->os,&op);
+ if(ret>0){
+ thisblock=vorbis_packet_blocksize(vf->vi+vf->current_link,&op);
+ if(thisblock<0){
+ ogg_stream_packetout(&vf->os,NULL);
+ continue; /* non audio packet */
+ }
+ if(lastblock)vf->pcm_offset+=(lastblock+thisblock)>>2;
+
+ if(vf->pcm_offset+((thisblock+
+ vorbis_info_blocksize(vf->vi,1))>>2)>=pos)break;
+
+ /* remove the packet from packet queue and track its granulepos */
+ ogg_stream_packetout(&vf->os,NULL);
+ vorbis_synthesis_trackonly(&vf->vb,&op); /* set up a vb with
+ only tracking, no
+ pcm_decode */
+ vorbis_synthesis_blockin(&vf->vd,&vf->vb);
+
+ /* end of logical stream case is hard, especially with exact
+ length positioning. */
+
+ if(op.granulepos>-1){
+ int i;
+ /* always believe the stream markers */
+ vf->pcm_offset=op.granulepos-vf->pcmlengths[vf->current_link*2];
+ if(vf->pcm_offset<0)vf->pcm_offset=0;
+ for(i=0;i<vf->current_link;i++)
+ vf->pcm_offset+=vf->pcmlengths[i*2+1];
+ }
+
+ lastblock=thisblock;
+
+ }else{
+ if(ret<0 && ret!=OV_HOLE)break;
+
+ /* suck in a new page */
+ if(_get_next_page(vf,&og,-1)<0)break;
+ if(ogg_page_bos(&og))_decode_clear(vf);
+
+ if(vf->ready_state<STREAMSET){
+ long serialno=ogg_page_serialno(&og);
+ int link;
+
+ for(link=0;link<vf->links;link++)
+ if(vf->serialnos[link]==serialno)break;
+ if(link==vf->links) continue;
+ vf->current_link=link;
+
+ vf->ready_state=STREAMSET;
+ vf->current_serialno=ogg_page_serialno(&og);
+ ogg_stream_reset_serialno(&vf->os,serialno);
+ ret=_make_decode_ready(vf);
+ if(ret)return ret;
+ lastblock=0;
+ }
+
+ ogg_stream_pagein(&vf->os,&og);
+ }
+ }
+
+ vf->bittrack=0.f;
+ vf->samptrack=0.f;
+ /* discard samples until we reach the desired position. Crossing a
+ logical bitstream boundary with abandon is OK. */
+ while(vf->pcm_offset<pos){
+ ogg_int64_t target=pos-vf->pcm_offset;
+ long samples=vorbis_synthesis_pcmout(&vf->vd,NULL);
+
+ if(samples>target)samples=target;
+ vorbis_synthesis_read(&vf->vd,samples);
+ vf->pcm_offset+=samples;
+
+ if(samples<target)
+ if(_fetch_and_process_packet(vf,NULL,1,1)<=0)
+ vf->pcm_offset=ov_pcm_total(vf,-1); /* eof */
+ }
+ return 0;
+}
+
+/* seek to a playback time relative to the decompressed pcm stream
+ returns zero on success, nonzero on failure */
+int ov_time_seek(OggVorbis_File *vf,double seconds){
+ /* translate time to PCM position and call ov_pcm_seek */
+
+ int link=-1;
+ ogg_int64_t pcm_total=0;
+ double time_total=0.;
+
+ if(vf->ready_state<OPENED)return(OV_EINVAL);
+ if(!vf->seekable)return(OV_ENOSEEK);
+ if(seconds<0)return(OV_EINVAL);
+
+ /* which bitstream section does this time offset occur in? */
+ for(link=0;link<vf->links;link++){
+ double addsec = ov_time_total(vf,link);
+ if(seconds<time_total+addsec)break;
+ time_total+=addsec;
+ pcm_total+=vf->pcmlengths[link*2+1];
+ }
+
+ if(link==vf->links)return(OV_EINVAL);
+
+ /* enough information to convert time offset to pcm offset */
+ {
+ ogg_int64_t target=pcm_total+(seconds-time_total)*vf->vi[link].rate;
+ return(ov_pcm_seek(vf,target));
+ }
+}
+
+/* page-granularity version of ov_time_seek
+ returns zero on success, nonzero on failure */
+int ov_time_seek_page(OggVorbis_File *vf,double seconds){
+ /* translate time to PCM position and call ov_pcm_seek */
+
+ int link=-1;
+ ogg_int64_t pcm_total=0;
+ double time_total=0.;
+
+ if(vf->ready_state<OPENED)return(OV_EINVAL);
+ if(!vf->seekable)return(OV_ENOSEEK);
+ if(seconds<0)return(OV_EINVAL);
+
+ /* which bitstream section does this time offset occur in? */
+ for(link=0;link<vf->links;link++){
+ double addsec = ov_time_total(vf,link);
+ if(seconds<time_total+addsec)break;
+ time_total+=addsec;
+ pcm_total+=vf->pcmlengths[link*2+1];
+ }
+
+ if(link==vf->links)return(OV_EINVAL);
+
+ /* enough information to convert time offset to pcm offset */
+ {
+ ogg_int64_t target=pcm_total+(seconds-time_total)*vf->vi[link].rate;
+ return(ov_pcm_seek_page(vf,target));
+ }
+}
+
+/* tell the current stream offset cursor. Note that seek followed by
+ tell will likely not give the set offset due to caching */
+ogg_int64_t ov_raw_tell(OggVorbis_File *vf){
+ if(vf->ready_state<OPENED)return(OV_EINVAL);
+ return(vf->offset);
+}
+
+/* return PCM offset (sample) of next PCM sample to be read */
+ogg_int64_t ov_pcm_tell(OggVorbis_File *vf){
+ if(vf->ready_state<OPENED)return(OV_EINVAL);
+ return(vf->pcm_offset);
+}
+
+/* return time offset (seconds) of next PCM sample to be read */
+double ov_time_tell(OggVorbis_File *vf){
+ int link=0;
+ ogg_int64_t pcm_total=0;
+ double time_total=0.f;
+
+ if(vf->ready_state<OPENED)return(OV_EINVAL);
+ if(vf->seekable){
+ pcm_total=ov_pcm_total(vf,-1);
+ time_total=ov_time_total(vf,-1);
+
+ /* which bitstream section does this time offset occur in? */
+ for(link=vf->links-1;link>=0;link--){
+ pcm_total-=vf->pcmlengths[link*2+1];
+ time_total-=ov_time_total(vf,link);
+ if(vf->pcm_offset>=pcm_total)break;
+ }
+ }
+
+ return((double)time_total+(double)(vf->pcm_offset-pcm_total)/vf->vi[link].rate);
+}
+
+/* link: -1) return the vorbis_info struct for the bitstream section
+ currently being decoded
+ 0-n) to request information for a specific bitstream section
+
+ In the case of a non-seekable bitstream, any call returns the
+ current bitstream. NULL in the case that the machine is not
+ initialized */
+
+vorbis_info *ov_info(OggVorbis_File *vf,int link){
+ if(vf->seekable){
+ if(link<0)
+ if(vf->ready_state>=STREAMSET)
+ return vf->vi+vf->current_link;
+ else
+ return vf->vi;
+ else
+ if(link>=vf->links)
+ return NULL;
+ else
+ return vf->vi+link;
+ }else{
+ return vf->vi;
+ }
+}
+
+/* grr, strong typing, grr, no templates/inheritence, grr */
+vorbis_comment *ov_comment(OggVorbis_File *vf,int link){
+ if(vf->seekable){
+ if(link<0)
+ if(vf->ready_state>=STREAMSET)
+ return vf->vc+vf->current_link;
+ else
+ return vf->vc;
+ else
+ if(link>=vf->links)
+ return NULL;
+ else
+ return vf->vc+link;
+ }else{
+ return vf->vc;
+ }
+}
+
+static int host_is_big_endian() {
+ ogg_int32_t pattern = 0xfeedface; /* deadbeef */
+ unsigned char *bytewise = (unsigned char *)&pattern;
+ if (bytewise[0] == 0xfe) return 1;
+ return 0;
+}
+
+/* up to this point, everything could more or less hide the multiple
+ logical bitstream nature of chaining from the toplevel application
+ if the toplevel application didn't particularly care. However, at
+ the point that we actually read audio back, the multiple-section
+ nature must surface: Multiple bitstream sections do not necessarily
+ have to have the same number of channels or sampling rate.
+
+ ov_read returns the sequential logical bitstream number currently
+ being decoded along with the PCM data in order that the toplevel
+ application can take action on channel/sample rate changes. This
+ number will be incremented even for streamed (non-seekable) streams
+ (for seekable streams, it represents the actual logical bitstream
+ index within the physical bitstream. Note that the accessor
+ functions above are aware of this dichotomy).
+
+ ov_read_filter is exactly the same as ov_read except that it processes
+ the decoded audio data through a filter before packing it into the
+ requested format. This gives greater accuracy than applying a filter
+ after the audio has been converted into integral PCM.
+
+ input values: buffer) a buffer to hold packed PCM data for return
+ length) the byte length requested to be placed into buffer
+ bigendianp) should the data be packed LSB first (0) or
+ MSB first (1)
+ word) word size for output. currently 1 (byte) or
+ 2 (16 bit short)
+
+ return values: <0) error/hole in data (OV_HOLE), partial open (OV_EINVAL)
+ 0) EOF
+ n) number of bytes of PCM actually returned. The
+ below works on a packet-by-packet basis, so the
+ return length is not related to the 'length' passed
+ in, just guaranteed to fit.
+
+ *section) set to the logical bitstream number */
+
+long ov_read_filter(OggVorbis_File *vf,char *buffer,int length,
+ int bigendianp,int word,int sgned,int *bitstream,
+ void (*filter)(float **pcm,long channels,long samples,void *filter_param),void *filter_param){
+ int i,j;
+ int host_endian = host_is_big_endian();
+
+ float **pcm;
+ long samples;
+
+ if(vf->ready_state<OPENED)return(OV_EINVAL);
+
+ while(1){
+ if(vf->ready_state==INITSET){
+ samples=vorbis_synthesis_pcmout(&vf->vd,&pcm);
+ if(samples)break;
+ }
+
+ /* suck in another packet */
+ {
+ int ret=_fetch_and_process_packet(vf,NULL,1,1);
+ if(ret==OV_EOF)
+ return(0);
+ if(ret<=0)
+ return(ret);
+ }
+
+ }
+
+ if(samples>0){
+
+ /* yay! proceed to pack data into the byte buffer */
+
+ long channels=ov_info(vf,-1)->channels;
+ long bytespersample=word * channels;
+ vorbis_fpu_control fpu;
+ if(samples>length/bytespersample)samples=length/bytespersample;
+
+ if(samples <= 0)
+ return OV_EINVAL;
+
+ /* Here. */
+ if(filter)
+ filter(pcm,channels,samples,filter_param);
+
+ /* a tight loop to pack each size */
+ {
+ int val;
+ if(word==1){
+ int off=(sgned?0:128);
+ vorbis_fpu_setround(&fpu);
+ for(j=0;j<samples;j++)
+ for(i=0;i<channels;i++){
+ val=vorbis_ftoi(pcm[i][j]*128.f);
+ if(val>127)val=127;
+ else if(val<-128)val=-128;
+ *buffer++=val+off;
+ }
+ vorbis_fpu_restore(fpu);
+ }else{
+ int off=(sgned?0:32768);
+
+ if(host_endian==bigendianp){
+ if(sgned){
+
+ vorbis_fpu_setround(&fpu);
+ for(i=0;i<channels;i++) { /* It's faster in this order */
+ float *src=pcm[i];
+ short *dest=((short *)buffer)+i;
+ for(j=0;j<samples;j++) {
+ val=vorbis_ftoi(src[j]*32768.f);
+ if(val>32767)val=32767;
+ else if(val<-32768)val=-32768;
+ *dest=val;
+ dest+=channels;
+ }
+ }
+ vorbis_fpu_restore(fpu);
+
+ }else{
+
+ vorbis_fpu_setround(&fpu);
+ for(i=0;i<channels;i++) {
+ float *src=pcm[i];
+ short *dest=((short *)buffer)+i;
+ for(j=0;j<samples;j++) {
+ val=vorbis_ftoi(src[j]*32768.f);
+ if(val>32767)val=32767;
+ else if(val<-32768)val=-32768;
+ *dest=val+off;
+ dest+=channels;
+ }
+ }
+ vorbis_fpu_restore(fpu);
+
+ }
+ }else if(bigendianp){
+
+ vorbis_fpu_setround(&fpu);
+ for(j=0;j<samples;j++)
+ for(i=0;i<channels;i++){
+ val=vorbis_ftoi(pcm[i][j]*32768.f);
+ if(val>32767)val=32767;
+ else if(val<-32768)val=-32768;
+ val+=off;
+ *buffer++=(val>>8);
+ *buffer++=(val&0xff);
+ }
+ vorbis_fpu_restore(fpu);
+
+ }else{
+ int val;
+ vorbis_fpu_setround(&fpu);
+ for(j=0;j<samples;j++)
+ for(i=0;i<channels;i++){
+ val=vorbis_ftoi(pcm[i][j]*32768.f);
+ if(val>32767)val=32767;
+ else if(val<-32768)val=-32768;
+ val+=off;
+ *buffer++=(val&0xff);
+ *buffer++=(val>>8);
+ }
+ vorbis_fpu_restore(fpu);
+
+ }
+ }
+ }
+
+ vorbis_synthesis_read(&vf->vd,samples);
+ vf->pcm_offset+=samples;
+ if(bitstream)*bitstream=vf->current_link;
+ return(samples*bytespersample);
+ }else{
+ return(samples);
+ }
+}
+
+long ov_read(OggVorbis_File *vf,char *buffer,int length,
+ int bigendianp,int word,int sgned,int *bitstream){
+ return ov_read_filter(vf, buffer, length, bigendianp, word, sgned, bitstream, NULL, NULL);
+}
+
+/* input values: pcm_channels) a float vector per channel of output
+ length) the sample length being read by the app
+
+ return values: <0) error/hole in data (OV_HOLE), partial open (OV_EINVAL)
+ 0) EOF
+ n) number of samples of PCM actually returned. The
+ below works on a packet-by-packet basis, so the
+ return length is not related to the 'length' passed
+ in, just guaranteed to fit.
+
+ *section) set to the logical bitstream number */
+
+
+
+long ov_read_float(OggVorbis_File *vf,float ***pcm_channels,int length,
+ int *bitstream){
+
+ if(vf->ready_state<OPENED)return(OV_EINVAL);
+
+ while(1){
+ if(vf->ready_state==INITSET){
+ float **pcm;
+ long samples=vorbis_synthesis_pcmout(&vf->vd,&pcm);
+ if(samples){
+ if(pcm_channels)*pcm_channels=pcm;
+ if(samples>length)samples=length;
+ vorbis_synthesis_read(&vf->vd,samples);
+ vf->pcm_offset+=samples;
+ if(bitstream)*bitstream=vf->current_link;
+ return samples;
+
+ }
+ }
+
+ /* suck in another packet */
+ {
+ int ret=_fetch_and_process_packet(vf,NULL,1,1);
+ if(ret==OV_EOF)return(0);
+ if(ret<=0)return(ret);
+ }
+
+ }
+}
+
+extern float *vorbis_window(vorbis_dsp_state *v,int W);
+
+static void _ov_splice(float **pcm,float **lappcm,
+ int n1, int n2,
+ int ch1, int ch2,
+ float *w1, float *w2){
+ int i,j;
+ float *w=w1;
+ int n=n1;
+
+ if(n1>n2){
+ n=n2;
+ w=w2;
+ }
+
+ /* splice */
+ for(j=0;j<ch1 && j<ch2;j++){
+ float *s=lappcm[j];
+ float *d=pcm[j];
+
+ for(i=0;i<n;i++){
+ float wd=w[i]*w[i];
+ float ws=1.-wd;
+ d[i]=d[i]*wd + s[i]*ws;
+ }
+ }
+ /* window from zero */
+ for(;j<ch2;j++){
+ float *d=pcm[j];
+ for(i=0;i<n;i++){
+ float wd=w[i]*w[i];
+ d[i]=d[i]*wd;
+ }
+ }
+
+}
+
+/* make sure vf is INITSET */
+static int _ov_initset(OggVorbis_File *vf){
+ while(1){
+ if(vf->ready_state==INITSET)break;
+ /* suck in another packet */
+ {
+ int ret=_fetch_and_process_packet(vf,NULL,1,0);
+ if(ret<0 && ret!=OV_HOLE)return(ret);
+ }
+ }
+ return 0;
+}
+
+/* make sure vf is INITSET and that we have a primed buffer; if
+ we're crosslapping at a stream section boundary, this also makes
+ sure we're sanity checking against the right stream information */
+static int _ov_initprime(OggVorbis_File *vf){
+ vorbis_dsp_state *vd=&vf->vd;
+ while(1){
+ if(vf->ready_state==INITSET)
+ if(vorbis_synthesis_pcmout(vd,NULL))break;
+
+ /* suck in another packet */
+ {
+ int ret=_fetch_and_process_packet(vf,NULL,1,0);
+ if(ret<0 && ret!=OV_HOLE)return(ret);
+ }
+ }
+ return 0;
+}
+
+/* grab enough data for lapping from vf; this may be in the form of
+ unreturned, already-decoded pcm, remaining PCM we will need to
+ decode, or synthetic postextrapolation from last packets. */
+static void _ov_getlap(OggVorbis_File *vf,vorbis_info *vi,vorbis_dsp_state *vd,
+ float **lappcm,int lapsize){
+ int lapcount=0,i;
+ float **pcm;
+
+ /* try first to decode the lapping data */
+ while(lapcount<lapsize){
+ int samples=vorbis_synthesis_pcmout(vd,&pcm);
+ if(samples){
+ if(samples>lapsize-lapcount)samples=lapsize-lapcount;
+ for(i=0;i<vi->channels;i++)
+ memcpy(lappcm[i]+lapcount,pcm[i],sizeof(**pcm)*samples);
+ lapcount+=samples;
+ vorbis_synthesis_read(vd,samples);
+ }else{
+ /* suck in another packet */
+ int ret=_fetch_and_process_packet(vf,NULL,1,0); /* do *not* span */
+ if(ret==OV_EOF)break;
+ }
+ }
+ if(lapcount<lapsize){
+ /* failed to get lapping data from normal decode; pry it from the
+ postextrapolation buffering, or the second half of the MDCT
+ from the last packet */
+ int samples=vorbis_synthesis_lapout(&vf->vd,&pcm);
+ if(samples==0){
+ for(i=0;i<vi->channels;i++)
+ memset(lappcm[i]+lapcount,0,sizeof(**pcm)*lapsize-lapcount);
+ lapcount=lapsize;
+ }else{
+ if(samples>lapsize-lapcount)samples=lapsize-lapcount;
+ for(i=0;i<vi->channels;i++)
+ memcpy(lappcm[i]+lapcount,pcm[i],sizeof(**pcm)*samples);
+ lapcount+=samples;
+ }
+ }
+}
+
+/* this sets up crosslapping of a sample by using trailing data from
+ sample 1 and lapping it into the windowing buffer of sample 2 */
+int ov_crosslap(OggVorbis_File *vf1, OggVorbis_File *vf2){
+ vorbis_info *vi1,*vi2;
+ float **lappcm;
+ float **pcm;
+ float *w1,*w2;
+ int n1,n2,i,ret,hs1,hs2;
+
+ if(vf1==vf2)return(0); /* degenerate case */
+ if(vf1->ready_state<OPENED)return(OV_EINVAL);
+ if(vf2->ready_state<OPENED)return(OV_EINVAL);
+
+ /* the relevant overlap buffers must be pre-checked and pre-primed
+ before looking at settings in the event that priming would cross
+ a bitstream boundary. So, do it now */
+
+ ret=_ov_initset(vf1);
+ if(ret)return(ret);
+ ret=_ov_initprime(vf2);
+ if(ret)return(ret);
+
+ vi1=ov_info(vf1,-1);
+ vi2=ov_info(vf2,-1);
+ hs1=ov_halfrate_p(vf1);
+ hs2=ov_halfrate_p(vf2);
+
+ lappcm=alloca(sizeof(*lappcm)*vi1->channels);
+ n1=vorbis_info_blocksize(vi1,0)>>(1+hs1);
+ n2=vorbis_info_blocksize(vi2,0)>>(1+hs2);
+ w1=vorbis_window(&vf1->vd,0);
+ w2=vorbis_window(&vf2->vd,0);
+
+ for(i=0;i<vi1->channels;i++)
+ lappcm[i]=alloca(sizeof(**lappcm)*n1);
+
+ _ov_getlap(vf1,vi1,&vf1->vd,lappcm,n1);
+
+ /* have a lapping buffer from vf1; now to splice it into the lapping
+ buffer of vf2 */
+ /* consolidate and expose the buffer. */
+ vorbis_synthesis_lapout(&vf2->vd,&pcm);
+
+#if 0
+ _analysis_output_always("pcmL",0,pcm[0],n1*2,0,0,0);
+ _analysis_output_always("pcmR",0,pcm[1],n1*2,0,0,0);
+#endif
+
+ /* splice */
+ _ov_splice(pcm,lappcm,n1,n2,vi1->channels,vi2->channels,w1,w2);
+
+ /* done */
+ return(0);
+}
+
+static int _ov_64_seek_lap(OggVorbis_File *vf,ogg_int64_t pos,
+ int (*localseek)(OggVorbis_File *,ogg_int64_t)){
+ vorbis_info *vi;
+ float **lappcm;
+ float **pcm;
+ float *w1,*w2;
+ int n1,n2,ch1,ch2,hs;
+ int i,ret;
+
+ if(vf->ready_state<OPENED)return(OV_EINVAL);
+ ret=_ov_initset(vf);
+ if(ret)return(ret);
+ vi=ov_info(vf,-1);
+ hs=ov_halfrate_p(vf);
+
+ ch1=vi->channels;
+ n1=vorbis_info_blocksize(vi,0)>>(1+hs);
+ w1=vorbis_window(&vf->vd,0); /* window arrays from libvorbis are
+ persistent; even if the decode state
+ from this link gets dumped, this
+ window array continues to exist */
+
+ lappcm=alloca(sizeof(*lappcm)*ch1);
+ for(i=0;i<ch1;i++)
+ lappcm[i]=alloca(sizeof(**lappcm)*n1);
+ _ov_getlap(vf,vi,&vf->vd,lappcm,n1);
+
+ /* have lapping data; seek and prime the buffer */
+ ret=localseek(vf,pos);
+ if(ret)return ret;
+ ret=_ov_initprime(vf);
+ if(ret)return(ret);
+
+ /* Guard against cross-link changes; they're perfectly legal */
+ vi=ov_info(vf,-1);
+ ch2=vi->channels;
+ n2=vorbis_info_blocksize(vi,0)>>(1+hs);
+ w2=vorbis_window(&vf->vd,0);
+
+ /* consolidate and expose the buffer. */
+ vorbis_synthesis_lapout(&vf->vd,&pcm);
+
+ /* splice */
+ _ov_splice(pcm,lappcm,n1,n2,ch1,ch2,w1,w2);
+
+ /* done */
+ return(0);
+}
+
+int ov_raw_seek_lap(OggVorbis_File *vf,ogg_int64_t pos){
+ return _ov_64_seek_lap(vf,pos,ov_raw_seek);
+}
+
+int ov_pcm_seek_lap(OggVorbis_File *vf,ogg_int64_t pos){
+ return _ov_64_seek_lap(vf,pos,ov_pcm_seek);
+}
+
+int ov_pcm_seek_page_lap(OggVorbis_File *vf,ogg_int64_t pos){
+ return _ov_64_seek_lap(vf,pos,ov_pcm_seek_page);
+}
+
+static int _ov_d_seek_lap(OggVorbis_File *vf,double pos,
+ int (*localseek)(OggVorbis_File *,double)){
+ vorbis_info *vi;
+ float **lappcm;
+ float **pcm;
+ float *w1,*w2;
+ int n1,n2,ch1,ch2,hs;
+ int i,ret;
+
+ if(vf->ready_state<OPENED)return(OV_EINVAL);
+ ret=_ov_initset(vf);
+ if(ret)return(ret);
+ vi=ov_info(vf,-1);
+ hs=ov_halfrate_p(vf);
+
+ ch1=vi->channels;
+ n1=vorbis_info_blocksize(vi,0)>>(1+hs);
+ w1=vorbis_window(&vf->vd,0); /* window arrays from libvorbis are
+ persistent; even if the decode state
+ from this link gets dumped, this
+ window array continues to exist */
+
+ lappcm=alloca(sizeof(*lappcm)*ch1);
+ for(i=0;i<ch1;i++)
+ lappcm[i]=alloca(sizeof(**lappcm)*n1);
+ _ov_getlap(vf,vi,&vf->vd,lappcm,n1);
+
+ /* have lapping data; seek and prime the buffer */
+ ret=localseek(vf,pos);
+ if(ret)return ret;
+ ret=_ov_initprime(vf);
+ if(ret)return(ret);
+
+ /* Guard against cross-link changes; they're perfectly legal */
+ vi=ov_info(vf,-1);
+ ch2=vi->channels;
+ n2=vorbis_info_blocksize(vi,0)>>(1+hs);
+ w2=vorbis_window(&vf->vd,0);
+
+ /* consolidate and expose the buffer. */
+ vorbis_synthesis_lapout(&vf->vd,&pcm);
+
+ /* splice */
+ _ov_splice(pcm,lappcm,n1,n2,ch1,ch2,w1,w2);
+
+ /* done */
+ return(0);
+}
+
+int ov_time_seek_lap(OggVorbis_File *vf,double pos){
+ return _ov_d_seek_lap(vf,pos,ov_time_seek);
+}
+
+int ov_time_seek_page_lap(OggVorbis_File *vf,double pos){
+ return _ov_d_seek_lap(vf,pos,ov_time_seek_page);
+}
|