nuclear@0: /******************************************************************** nuclear@0: * * nuclear@0: * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. * nuclear@0: * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * nuclear@0: * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * nuclear@0: * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * nuclear@0: * * nuclear@0: * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2009 * nuclear@0: * by the Xiph.Org Foundation http://www.xiph.org/ * nuclear@0: * * nuclear@0: ******************************************************************** nuclear@0: nuclear@0: function: LSP (also called LSF) conversion routines nuclear@0: last mod: $Id: lsp.c 17538 2010-10-15 02:52:29Z tterribe $ nuclear@0: nuclear@0: The LSP generation code is taken (with minimal modification and a nuclear@0: few bugfixes) from "On the Computation of the LSP Frequencies" by nuclear@0: Joseph Rothweiler (see http://www.rothweiler.us for contact info). nuclear@0: The paper is available at: nuclear@0: nuclear@0: http://www.myown1.com/joe/lsf nuclear@0: nuclear@0: ********************************************************************/ nuclear@0: nuclear@0: /* Note that the lpc-lsp conversion finds the roots of polynomial with nuclear@0: an iterative root polisher (CACM algorithm 283). It *is* possible nuclear@0: to confuse this algorithm into not converging; that should only nuclear@0: happen with absurdly closely spaced roots (very sharp peaks in the nuclear@0: LPC f response) which in turn should be impossible in our use of nuclear@0: the code. If this *does* happen anyway, it's a bug in the floor nuclear@0: finder; find the cause of the confusion (probably a single bin nuclear@0: spike or accidental near-float-limit resolution problems) and nuclear@0: correct it. */ nuclear@0: nuclear@0: #include nuclear@0: #include nuclear@0: #include nuclear@0: #include "lsp.h" nuclear@0: #include "os.h" nuclear@0: #include "misc.h" nuclear@0: #include "lookup.h" nuclear@0: #include "scales.h" nuclear@0: nuclear@0: /* three possible LSP to f curve functions; the exact computation nuclear@0: (float), a lookup based float implementation, and an integer nuclear@0: implementation. The float lookup is likely the optimal choice on nuclear@0: any machine with an FPU. The integer implementation is *not* fixed nuclear@0: point (due to the need for a large dynamic range and thus a nuclear@0: separately tracked exponent) and thus much more complex than the nuclear@0: relatively simple float implementations. It's mostly for future nuclear@0: work on a fully fixed point implementation for processors like the nuclear@0: ARM family. */ nuclear@0: nuclear@0: /* define either of these (preferably FLOAT_LOOKUP) to have faster nuclear@0: but less precise implementation. */ nuclear@0: #undef FLOAT_LOOKUP nuclear@0: #undef INT_LOOKUP nuclear@0: nuclear@0: #ifdef FLOAT_LOOKUP nuclear@0: #include "lookup.c" /* catch this in the build system; we #include for nuclear@0: compilers (like gcc) that can't inline across nuclear@0: modules */ nuclear@0: nuclear@0: /* side effect: changes *lsp to cosines of lsp */ nuclear@0: void vorbis_lsp_to_curve(float *curve,int *map,int n,int ln,float *lsp,int m, nuclear@0: float amp,float ampoffset){ nuclear@0: int i; nuclear@0: float wdel=M_PI/ln; nuclear@0: vorbis_fpu_control fpu; nuclear@0: nuclear@0: vorbis_fpu_setround(&fpu); nuclear@0: for(i=0;i>1; nuclear@0: nuclear@0: while(c--){ nuclear@0: q*=ftmp[0]-w; nuclear@0: p*=ftmp[1]-w; nuclear@0: ftmp+=2; nuclear@0: } nuclear@0: nuclear@0: if(m&1){ nuclear@0: /* odd order filter; slightly assymetric */ nuclear@0: /* the last coefficient */ nuclear@0: q*=ftmp[0]-w; nuclear@0: q*=q; nuclear@0: p*=p*(1.f-w*w); nuclear@0: }else{ nuclear@0: /* even order filter; still symmetric */ nuclear@0: q*=q*(1.f+w); nuclear@0: p*=p*(1.f-w); nuclear@0: } nuclear@0: nuclear@0: q=frexp(p+q,&qexp); nuclear@0: q=vorbis_fromdBlook(amp* nuclear@0: vorbis_invsqlook(q)* nuclear@0: vorbis_invsq2explook(qexp+m)- nuclear@0: ampoffset); nuclear@0: nuclear@0: do{ nuclear@0: curve[i++]*=q; nuclear@0: }while(map[i]==k); nuclear@0: } nuclear@0: vorbis_fpu_restore(fpu); nuclear@0: } nuclear@0: nuclear@0: #else nuclear@0: nuclear@0: #ifdef INT_LOOKUP nuclear@0: #include "lookup.c" /* catch this in the build system; we #include for nuclear@0: compilers (like gcc) that can't inline across nuclear@0: modules */ nuclear@0: nuclear@0: static const int MLOOP_1[64]={ nuclear@0: 0,10,11,11, 12,12,12,12, 13,13,13,13, 13,13,13,13, nuclear@0: 14,14,14,14, 14,14,14,14, 14,14,14,14, 14,14,14,14, nuclear@0: 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15, nuclear@0: 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15, nuclear@0: }; nuclear@0: nuclear@0: static const int MLOOP_2[64]={ nuclear@0: 0,4,5,5, 6,6,6,6, 7,7,7,7, 7,7,7,7, nuclear@0: 8,8,8,8, 8,8,8,8, 8,8,8,8, 8,8,8,8, nuclear@0: 9,9,9,9, 9,9,9,9, 9,9,9,9, 9,9,9,9, nuclear@0: 9,9,9,9, 9,9,9,9, 9,9,9,9, 9,9,9,9, nuclear@0: }; nuclear@0: nuclear@0: static const int MLOOP_3[8]={0,1,2,2,3,3,3,3}; nuclear@0: nuclear@0: nuclear@0: /* side effect: changes *lsp to cosines of lsp */ nuclear@0: void vorbis_lsp_to_curve(float *curve,int *map,int n,int ln,float *lsp,int m, nuclear@0: float amp,float ampoffset){ nuclear@0: nuclear@0: /* 0 <= m < 256 */ nuclear@0: nuclear@0: /* set up for using all int later */ nuclear@0: int i; nuclear@0: int ampoffseti=rint(ampoffset*4096.f); nuclear@0: int ampi=rint(amp*16.f); nuclear@0: long *ilsp=alloca(m*sizeof(*ilsp)); nuclear@0: for(i=0;i>25])) nuclear@0: if(!(shift=MLOOP_2[(pi|qi)>>19])) nuclear@0: shift=MLOOP_3[(pi|qi)>>16]; nuclear@0: qi=(qi>>shift)*labs(ilsp[j-1]-wi); nuclear@0: pi=(pi>>shift)*labs(ilsp[j]-wi); nuclear@0: qexp+=shift; nuclear@0: } nuclear@0: if(!(shift=MLOOP_1[(pi|qi)>>25])) nuclear@0: if(!(shift=MLOOP_2[(pi|qi)>>19])) nuclear@0: shift=MLOOP_3[(pi|qi)>>16]; nuclear@0: nuclear@0: /* pi,qi normalized collectively, both tracked using qexp */ nuclear@0: nuclear@0: if(m&1){ nuclear@0: /* odd order filter; slightly assymetric */ nuclear@0: /* the last coefficient */ nuclear@0: qi=(qi>>shift)*labs(ilsp[j-1]-wi); nuclear@0: pi=(pi>>shift)<<14; nuclear@0: qexp+=shift; nuclear@0: nuclear@0: if(!(shift=MLOOP_1[(pi|qi)>>25])) nuclear@0: if(!(shift=MLOOP_2[(pi|qi)>>19])) nuclear@0: shift=MLOOP_3[(pi|qi)>>16]; nuclear@0: nuclear@0: pi>>=shift; nuclear@0: qi>>=shift; nuclear@0: qexp+=shift-14*((m+1)>>1); nuclear@0: nuclear@0: pi=((pi*pi)>>16); nuclear@0: qi=((qi*qi)>>16); nuclear@0: qexp=qexp*2+m; nuclear@0: nuclear@0: pi*=(1<<14)-((wi*wi)>>14); nuclear@0: qi+=pi>>14; nuclear@0: nuclear@0: }else{ nuclear@0: /* even order filter; still symmetric */ nuclear@0: nuclear@0: /* p*=p(1-w), q*=q(1+w), let normalization drift because it isn't nuclear@0: worth tracking step by step */ nuclear@0: nuclear@0: pi>>=shift; nuclear@0: qi>>=shift; nuclear@0: qexp+=shift-7*m; nuclear@0: nuclear@0: pi=((pi*pi)>>16); nuclear@0: qi=((qi*qi)>>16); nuclear@0: qexp=qexp*2+m; nuclear@0: nuclear@0: pi*=(1<<14)-wi; nuclear@0: qi*=(1<<14)+wi; nuclear@0: qi=(qi+pi)>>14; nuclear@0: nuclear@0: } nuclear@0: nuclear@0: nuclear@0: /* we've let the normalization drift because it wasn't important; nuclear@0: however, for the lookup, things must be normalized again. We nuclear@0: need at most one right shift or a number of left shifts */ nuclear@0: nuclear@0: if(qi&0xffff0000){ /* checks for 1.xxxxxxxxxxxxxxxx */ nuclear@0: qi>>=1; qexp++; nuclear@0: }else nuclear@0: while(qi && !(qi&0x8000)){ /* checks for 0.0xxxxxxxxxxxxxxx or less*/ nuclear@0: qi<<=1; qexp--; nuclear@0: } nuclear@0: nuclear@0: amp=vorbis_fromdBlook_i(ampi* /* n.4 */ nuclear@0: vorbis_invsqlook_i(qi,qexp)- nuclear@0: /* m.8, m+n<=8 */ nuclear@0: ampoffseti); /* 8.12[0] */ nuclear@0: nuclear@0: curve[i]*=amp; nuclear@0: while(map[++i]==k)curve[i]*=amp; nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: #else nuclear@0: nuclear@0: /* old, nonoptimized but simple version for any poor sap who needs to nuclear@0: figure out what the hell this code does, or wants the other nuclear@0: fraction of a dB precision */ nuclear@0: nuclear@0: /* side effect: changes *lsp to cosines of lsp */ nuclear@0: void vorbis_lsp_to_curve(float *curve,int *map,int n,int ln,float *lsp,int m, nuclear@0: float amp,float ampoffset){ nuclear@0: int i; nuclear@0: float wdel=M_PI/ln; nuclear@0: for(i=0;i= i; j--) { nuclear@0: g[j-2] -= g[j]; nuclear@0: g[j] += g[j]; nuclear@0: } nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: static int comp(const void *a,const void *b){ nuclear@0: return (*(float *)a<*(float *)b)-(*(float *)a>*(float *)b); nuclear@0: } nuclear@0: nuclear@0: /* Newton-Raphson-Maehly actually functioned as a decent root finder, nuclear@0: but there are root sets for which it gets into limit cycles nuclear@0: (exacerbated by zero suppression) and fails. We can't afford to nuclear@0: fail, even if the failure is 1 in 100,000,000, so we now use nuclear@0: Laguerre and later polish with Newton-Raphson (which can then nuclear@0: afford to fail) */ nuclear@0: nuclear@0: #define EPSILON 10e-7 nuclear@0: static int Laguerre_With_Deflation(float *a,int ord,float *r){ nuclear@0: int i,m; nuclear@0: double *defl=alloca(sizeof(*defl)*(ord+1)); nuclear@0: for(i=0;i<=ord;i++)defl[i]=a[i]; nuclear@0: nuclear@0: for(m=ord;m>0;m--){ nuclear@0: double new=0.f,delta; nuclear@0: nuclear@0: /* iterate a root */ nuclear@0: while(1){ nuclear@0: double p=defl[m],pp=0.f,ppp=0.f,denom; nuclear@0: nuclear@0: /* eval the polynomial and its first two derivatives */ nuclear@0: for(i=m;i>0;i--){ nuclear@0: ppp = new*ppp + pp; nuclear@0: pp = new*pp + p; nuclear@0: p = new*p + defl[i-1]; nuclear@0: } nuclear@0: nuclear@0: /* Laguerre's method */ nuclear@0: denom=(m-1) * ((m-1)*pp*pp - m*p*ppp); nuclear@0: if(denom<0) nuclear@0: return(-1); /* complex root! The LPC generator handed us a bad filter */ nuclear@0: nuclear@0: if(pp>0){ nuclear@0: denom = pp + sqrt(denom); nuclear@0: if(denom-(EPSILON))denom=-(EPSILON); nuclear@0: } nuclear@0: nuclear@0: delta = m*p/denom; nuclear@0: new -= delta; nuclear@0: nuclear@0: if(delta<0.f)delta*=-1; nuclear@0: nuclear@0: if(fabs(delta/new)<10e-12)break; nuclear@0: } nuclear@0: nuclear@0: r[m-1]=new; nuclear@0: nuclear@0: /* forward deflation */ nuclear@0: nuclear@0: for(i=m;i>0;i--) nuclear@0: defl[i-1]+=new*defl[i]; nuclear@0: defl++; nuclear@0: nuclear@0: } nuclear@0: return(0); nuclear@0: } nuclear@0: nuclear@0: nuclear@0: /* for spit-and-polish only */ nuclear@0: static int Newton_Raphson(float *a,int ord,float *r){ nuclear@0: int i, k, count=0; nuclear@0: double error=1.f; nuclear@0: double *root=alloca(ord*sizeof(*root)); nuclear@0: nuclear@0: for(i=0; i1e-20){ nuclear@0: error=0; nuclear@0: nuclear@0: for(i=0; i= 0; k--) { nuclear@0: nuclear@0: pp= pp* rooti + p; nuclear@0: p = p * rooti + a[k]; nuclear@0: } nuclear@0: nuclear@0: delta = p/pp; nuclear@0: root[i] -= delta; nuclear@0: error+= delta*delta; nuclear@0: } nuclear@0: nuclear@0: if(count>40)return(-1); nuclear@0: nuclear@0: count++; nuclear@0: } nuclear@0: nuclear@0: /* Replaced the original bubble sort with a real sort. With your nuclear@0: help, we can eliminate the bubble sort in our lifetime. --Monty */ nuclear@0: nuclear@0: for(i=0; i>1; nuclear@0: int g1_order,g2_order; nuclear@0: float *g1=alloca(sizeof(*g1)*(order2+1)); nuclear@0: float *g2=alloca(sizeof(*g2)*(order2+1)); nuclear@0: float *g1r=alloca(sizeof(*g1r)*(order2+1)); nuclear@0: float *g2r=alloca(sizeof(*g2r)*(order2+1)); nuclear@0: int i; nuclear@0: nuclear@0: /* even and odd are slightly different base cases */ nuclear@0: g1_order=(m+1)>>1; nuclear@0: g2_order=(m) >>1; nuclear@0: nuclear@0: /* Compute the lengths of the x polynomials. */ nuclear@0: /* Compute the first half of K & R F1 & F2 polynomials. */ nuclear@0: /* Compute half of the symmetric and antisymmetric polynomials. */ nuclear@0: /* Remove the roots at +1 and -1. */ nuclear@0: nuclear@0: g1[g1_order] = 1.f; nuclear@0: for(i=1;i<=g1_order;i++) g1[g1_order-i] = lpc[i-1]+lpc[m-i]; nuclear@0: g2[g2_order] = 1.f; nuclear@0: for(i=1;i<=g2_order;i++) g2[g2_order-i] = lpc[i-1]-lpc[m-i]; nuclear@0: nuclear@0: if(g1_order>g2_order){ nuclear@0: for(i=2; i<=g2_order;i++) g2[g2_order-i] += g2[g2_order-i+2]; nuclear@0: }else{ nuclear@0: for(i=1; i<=g1_order;i++) g1[g1_order-i] -= g1[g1_order-i+1]; nuclear@0: for(i=1; i<=g2_order;i++) g2[g2_order-i] += g2[g2_order-i+1]; nuclear@0: } nuclear@0: nuclear@0: /* Convert into polynomials in cos(alpha) */ nuclear@0: cheby(g1,g1_order); nuclear@0: cheby(g2,g2_order); nuclear@0: nuclear@0: /* Find the roots of the 2 even polynomials.*/ nuclear@0: if(Laguerre_With_Deflation(g1,g1_order,g1r) || nuclear@0: Laguerre_With_Deflation(g2,g2_order,g2r)) nuclear@0: return(-1); nuclear@0: nuclear@0: Newton_Raphson(g1,g1_order,g1r); /* if it fails, it leaves g1r alone */ nuclear@0: Newton_Raphson(g2,g2_order,g2r); /* if it fails, it leaves g2r alone */ nuclear@0: nuclear@0: qsort(g1r,g1_order,sizeof(*g1r),comp); nuclear@0: qsort(g2r,g2_order,sizeof(*g2r),comp); nuclear@0: nuclear@0: for(i=0;i