/********************************************************************1* *2* THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *3* USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *4* GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *5* IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *6* *7* THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2009 *8* by the Xiph.Org Foundation https://xiph.org/ *9* *10********************************************************************1112function: LSP (also called LSF) conversion routines1314The LSP generation code is taken (with minimal modification and a15few bugfixes) from "On the Computation of the LSP Frequencies" by16Joseph Rothweiler (see http://www.rothweiler.us for contact info).1718The paper is available at:1920https://web.archive.org/web/20110810174000/http://home.myfairpoint.net/vzenxj75/myown1/joe/lsf/index.html2122********************************************************************/2324/* Note that the lpc-lsp conversion finds the roots of polynomial with25an iterative root polisher (CACM algorithm 283). It *is* possible26to confuse this algorithm into not converging; that should only27happen with absurdly closely spaced roots (very sharp peaks in the28LPC f response) which in turn should be impossible in our use of29the code. If this *does* happen anyway, it's a bug in the floor30finder; find the cause of the confusion (probably a single bin31spike or accidental near-float-limit resolution problems) and32correct it. */3334#include <math.h>35#include <string.h>36#include <stdlib.h>37#include "lsp.h"38#include "os.h"39#include "misc.h"40#include "lookup.h"41#include "scales.h"4243/* three possible LSP to f curve functions; the exact computation44(float), a lookup based float implementation, and an integer45implementation. The float lookup is likely the optimal choice on46any machine with an FPU. The integer implementation is *not* fixed47point (due to the need for a large dynamic range and thus a48separately tracked exponent) and thus much more complex than the49relatively simple float implementations. It's mostly for future50work on a fully fixed point implementation for processors like the51ARM family. */5253/* define either of these (preferably FLOAT_LOOKUP) to have faster54but less precise implementation. */55#undef FLOAT_LOOKUP56#undef INT_LOOKUP5758#ifdef FLOAT_LOOKUP59#include "lookup.c" /* catch this in the build system; we #include for60compilers (like gcc) that can't inline across61modules */6263/* side effect: changes *lsp to cosines of lsp */64void vorbis_lsp_to_curve(float *curve,int *map,int n,int ln,float *lsp,int m,65float amp,float ampoffset){66int i;67float wdel=M_PI/ln;68vorbis_fpu_control fpu;6970vorbis_fpu_setround(&fpu);71for(i=0;i<m;i++)lsp[i]=vorbis_coslook(lsp[i]);7273i=0;74while(i<n){75int k=map[i];76int qexp;77float p=.7071067812f;78float q=.7071067812f;79float w=vorbis_coslook(wdel*k);80float *ftmp=lsp;81int c=m>>1;8283while(c--){84q*=ftmp[0]-w;85p*=ftmp[1]-w;86ftmp+=2;87}8889if(m&1){90/* odd order filter; slightly assymetric */91/* the last coefficient */92q*=ftmp[0]-w;93q*=q;94p*=p*(1.f-w*w);95}else{96/* even order filter; still symmetric */97q*=q*(1.f+w);98p*=p*(1.f-w);99}100101q=frexp(p+q,&qexp);102q=vorbis_fromdBlook(amp*103vorbis_invsqlook(q)*104vorbis_invsq2explook(qexp+m)-105ampoffset);106107do{108curve[i++]*=q;109}while(map[i]==k);110}111vorbis_fpu_restore(fpu);112}113114#else115116#ifdef INT_LOOKUP117#include "lookup.c" /* catch this in the build system; we #include for118compilers (like gcc) that can't inline across119modules */120121static const int MLOOP_1[64]={1220,10,11,11, 12,12,12,12, 13,13,13,13, 13,13,13,13,12314,14,14,14, 14,14,14,14, 14,14,14,14, 14,14,14,14,12415,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,12515,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,126};127128static const int MLOOP_2[64]={1290,4,5,5, 6,6,6,6, 7,7,7,7, 7,7,7,7,1308,8,8,8, 8,8,8,8, 8,8,8,8, 8,8,8,8,1319,9,9,9, 9,9,9,9, 9,9,9,9, 9,9,9,9,1329,9,9,9, 9,9,9,9, 9,9,9,9, 9,9,9,9,133};134135static const int MLOOP_3[8]={0,1,2,2,3,3,3,3};136137138/* side effect: changes *lsp to cosines of lsp */139void vorbis_lsp_to_curve(float *curve,int *map,int n,int ln,float *lsp,int m,140float amp,float ampoffset){141142/* 0 <= m < 256 */143144/* set up for using all int later */145int i;146int ampoffseti=rint(ampoffset*4096.f);147int ampi=rint(amp*16.f);148long *ilsp=alloca(m*sizeof(*ilsp));149for(i=0;i<m;i++)ilsp[i]=vorbis_coslook_i(lsp[i]/M_PI*65536.f+.5f);150151i=0;152while(i<n){153int j,k=map[i];154unsigned long pi=46341; /* 2**-.5 in 0.16 */155unsigned long qi=46341;156int qexp=0,shift;157long wi=vorbis_coslook_i(k*65536/ln);158159qi*=labs(ilsp[0]-wi);160pi*=labs(ilsp[1]-wi);161162for(j=3;j<m;j+=2){163if(!(shift=MLOOP_1[(pi|qi)>>25]))164if(!(shift=MLOOP_2[(pi|qi)>>19]))165shift=MLOOP_3[(pi|qi)>>16];166qi=(qi>>shift)*labs(ilsp[j-1]-wi);167pi=(pi>>shift)*labs(ilsp[j]-wi);168qexp+=shift;169}170if(!(shift=MLOOP_1[(pi|qi)>>25]))171if(!(shift=MLOOP_2[(pi|qi)>>19]))172shift=MLOOP_3[(pi|qi)>>16];173174/* pi,qi normalized collectively, both tracked using qexp */175176if(m&1){177/* odd order filter; slightly assymetric */178/* the last coefficient */179qi=(qi>>shift)*labs(ilsp[j-1]-wi);180pi=(pi>>shift)<<14;181qexp+=shift;182183if(!(shift=MLOOP_1[(pi|qi)>>25]))184if(!(shift=MLOOP_2[(pi|qi)>>19]))185shift=MLOOP_3[(pi|qi)>>16];186187pi>>=shift;188qi>>=shift;189qexp+=shift-14*((m+1)>>1);190191pi=((pi*pi)>>16);192qi=((qi*qi)>>16);193qexp=qexp*2+m;194195pi*=(1<<14)-((wi*wi)>>14);196qi+=pi>>14;197198}else{199/* even order filter; still symmetric */200201/* p*=p(1-w), q*=q(1+w), let normalization drift because it isn't202worth tracking step by step */203204pi>>=shift;205qi>>=shift;206qexp+=shift-7*m;207208pi=((pi*pi)>>16);209qi=((qi*qi)>>16);210qexp=qexp*2+m;211212pi*=(1<<14)-wi;213qi*=(1<<14)+wi;214qi=(qi+pi)>>14;215216}217218219/* we've let the normalization drift because it wasn't important;220however, for the lookup, things must be normalized again. We221need at most one right shift or a number of left shifts */222223if(qi&0xffff0000){ /* checks for 1.xxxxxxxxxxxxxxxx */224qi>>=1; qexp++;225}else226while(qi && !(qi&0x8000)){ /* checks for 0.0xxxxxxxxxxxxxxx or less*/227qi<<=1; qexp--;228}229230amp=vorbis_fromdBlook_i(ampi* /* n.4 */231vorbis_invsqlook_i(qi,qexp)-232/* m.8, m+n<=8 */233ampoffseti); /* 8.12[0] */234235curve[i]*=amp;236while(map[++i]==k)curve[i]*=amp;237}238}239240#else241242/* old, nonoptimized but simple version for any poor sap who needs to243figure out what the hell this code does, or wants the other244fraction of a dB precision */245246/* side effect: changes *lsp to cosines of lsp */247void vorbis_lsp_to_curve(float *curve,int *map,int n,int ln,float *lsp,int m,248float amp,float ampoffset){249int i;250float wdel=M_PI/ln;251for(i=0;i<m;i++)lsp[i]=2.f*cos(lsp[i]);252253i=0;254while(i<n){255int j,k=map[i];256float p=.5f;257float q=.5f;258float w=2.f*cos(wdel*k);259for(j=1;j<m;j+=2){260q *= w-lsp[j-1];261p *= w-lsp[j];262}263if(j==m){264/* odd order filter; slightly assymetric */265/* the last coefficient */266q*=w-lsp[j-1];267p*=p*(4.f-w*w);268q*=q;269}else{270/* even order filter; still symmetric */271p*=p*(2.f-w);272q*=q*(2.f+w);273}274275q=fromdB(amp/sqrt(p+q)-ampoffset);276277curve[i]*=q;278while(map[++i]==k)curve[i]*=q;279}280}281282#endif283#endif284285static void cheby(float *g, int ord) {286int i, j;287288g[0] *= .5f;289for(i=2; i<= ord; i++) {290for(j=ord; j >= i; j--) {291g[j-2] -= g[j];292g[j] += g[j];293}294}295}296297static int comp(const void *a,const void *b){298return (*(float *)a<*(float *)b)-(*(float *)a>*(float *)b);299}300301/* Newton-Raphson-Maehly actually functioned as a decent root finder,302but there are root sets for which it gets into limit cycles303(exacerbated by zero suppression) and fails. We can't afford to304fail, even if the failure is 1 in 100,000,000, so we now use305Laguerre and later polish with Newton-Raphson (which can then306afford to fail) */307308#define EPSILON 10e-7309static int Laguerre_With_Deflation(float *a,int ord,float *r){310int i,m;311double *defl=alloca(sizeof(*defl)*(ord+1));312for(i=0;i<=ord;i++)defl[i]=a[i];313314for(m=ord;m>0;m--){315double new=0.f,delta;316317/* iterate a root */318while(1){319double p=defl[m],pp=0.f,ppp=0.f,denom;320321/* eval the polynomial and its first two derivatives */322for(i=m;i>0;i--){323ppp = new*ppp + pp;324pp = new*pp + p;325p = new*p + defl[i-1];326}327328/* Laguerre's method */329denom=(m-1) * ((m-1)*pp*pp - m*p*ppp);330if(denom<0)331return(-1); /* complex root! The LPC generator handed us a bad filter */332333if(pp>0){334denom = pp + sqrt(denom);335if(denom<EPSILON)denom=EPSILON;336}else{337denom = pp - sqrt(denom);338if(denom>-(EPSILON))denom=-(EPSILON);339}340341delta = m*p/denom;342new -= delta;343344if(delta<0.f)delta*=-1;345346if(fabs(delta/new)<10e-12)break;347}348349r[m-1]=new;350351/* forward deflation */352353for(i=m;i>0;i--)354defl[i-1]+=new*defl[i];355defl++;356357}358return(0);359}360361362/* for spit-and-polish only */363static int Newton_Raphson(float *a,int ord,float *r){364int i, k, count=0;365double error=1.f;366double *root=alloca(ord*sizeof(*root));367368for(i=0; i<ord;i++) root[i] = r[i];369370while(error>1e-20){371error=0;372373for(i=0; i<ord; i++) { /* Update each point. */374double pp=0.,delta;375double rooti=root[i];376double p=a[ord];377for(k=ord-1; k>= 0; k--) {378379pp= pp* rooti + p;380p = p * rooti + a[k];381}382383delta = p/pp;384root[i] -= delta;385error+= delta*delta;386}387388if(count>40)return(-1);389390count++;391}392393/* Replaced the original bubble sort with a real sort. With your394help, we can eliminate the bubble sort in our lifetime. --Monty */395396for(i=0; i<ord;i++) r[i] = root[i];397return(0);398}399400401/* Convert lpc coefficients to lsp coefficients */402int vorbis_lpc_to_lsp(float *lpc,float *lsp,int m){403int order2=(m+1)>>1;404int g1_order,g2_order;405float *g1=alloca(sizeof(*g1)*(order2+1));406float *g2=alloca(sizeof(*g2)*(order2+1));407float *g1r=alloca(sizeof(*g1r)*(order2+1));408float *g2r=alloca(sizeof(*g2r)*(order2+1));409int i;410411/* even and odd are slightly different base cases */412g1_order=(m+1)>>1;413g2_order=(m) >>1;414415/* Compute the lengths of the x polynomials. */416/* Compute the first half of K & R F1 & F2 polynomials. */417/* Compute half of the symmetric and antisymmetric polynomials. */418/* Remove the roots at +1 and -1. */419420g1[g1_order] = 1.f;421for(i=1;i<=g1_order;i++) g1[g1_order-i] = lpc[i-1]+lpc[m-i];422g2[g2_order] = 1.f;423for(i=1;i<=g2_order;i++) g2[g2_order-i] = lpc[i-1]-lpc[m-i];424425if(g1_order>g2_order){426for(i=2; i<=g2_order;i++) g2[g2_order-i] += g2[g2_order-i+2];427}else{428for(i=1; i<=g1_order;i++) g1[g1_order-i] -= g1[g1_order-i+1];429for(i=1; i<=g2_order;i++) g2[g2_order-i] += g2[g2_order-i+1];430}431432/* Convert into polynomials in cos(alpha) */433cheby(g1,g1_order);434cheby(g2,g2_order);435436/* Find the roots of the 2 even polynomials.*/437if(Laguerre_With_Deflation(g1,g1_order,g1r) ||438Laguerre_With_Deflation(g2,g2_order,g2r))439return(-1);440441Newton_Raphson(g1,g1_order,g1r); /* if it fails, it leaves g1r alone */442Newton_Raphson(g2,g2_order,g2r); /* if it fails, it leaves g2r alone */443444qsort(g1r,g1_order,sizeof(*g1r),comp);445qsort(g2r,g2_order,sizeof(*g2r),comp);446447for(i=0;i<g1_order;i++)448lsp[i*2] = acos(g1r[i]);449450for(i=0;i<g2_order;i++)451lsp[i*2+1] = acos(g2r[i]);452return(0);453}454455456