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