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