lsp.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. /********************************************************************
  2. * *
  3. * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
  4. * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
  5. * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
  6. * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
  7. * *
  8. * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2001 *
  9. * by the XIPHOPHORUS Company http://www.xiph.org/ *
  10. ********************************************************************
  11. function: LSP (also called LSF) conversion routines
  12. last mod: $Id: lsp.c,v 1.17.2.1 2001/03/28 03:08:14 segher Exp $
  13. The LSP generation code is taken (with minimal modification and a
  14. few bugfixes) from "On the Computation of the LSP Frequencies" by
  15. Joseph Rothweiler <rothwlr@altavista.net>, available at:
  16. http://www2.xtdl.com/~rothwlr/lsfpaper/lsfpage.html
  17. ********************************************************************/
  18. /* Note that the lpc-lsp conversion finds the roots of polynomial with
  19. an iterative root polisher (CACM algorithm 283). It *is* possible
  20. to confuse this algorithm into not converging; that should only
  21. happen with absurdly closely spaced roots (very sharp peaks in the
  22. LPC f response) which in turn should be impossible in our use of
  23. the code. If this *does* happen anyway, it's a bug in the floor
  24. finder; find the cause of the confusion (probably a single bin
  25. spike or accidental near-float-limit resolution problems) and
  26. correct it. */
  27. #include <math.h>
  28. #include <string.h>
  29. #include <stdlib.h>
  30. #include "lsp.h"
  31. #include "os.h"
  32. #include "misc.h"
  33. #include "lookup.h"
  34. #include "scales.h"
  35. #define LDBTAB 12
  36. #define LCOSTAB 12
  37. #define NDBTAB (1<<LDBTAB)
  38. #define NCOSTAB (1<<LCOSTAB)
  39. #define fromdB_t1(x) (dbtab1[(*(int *)&(x) >> (32-LDBTAB)) & (NDBTAB-1)])
  40. #define fromdB_t2(x) (dbtab2[(*(int *)&(x) >> (32-LDBTAB)) & (NDBTAB-1)])
  41. #define tcos_t(x) (tcostab[(*(int *)&(x) >> (23-LCOSTAB)) & (NCOSTAB-1)])
  42. static float dbtab1[NDBTAB];
  43. static float dbtab2[NDBTAB];
  44. static float tcostab[NCOSTAB];
  45. static void initdbtab()
  46. {
  47. int i;
  48. float t;
  49. for (i = 0; i < NDBTAB; i++) {
  50. *(int *)&t = (i << (32-LDBTAB)) | (1 << (31-LDBTAB));
  51. dbtab1[i] = fromdB(1.0f/sqrt(t));
  52. dbtab2[i] = fromdB(-t);
  53. //fprintf(stderr, "%4d: %08x %12.6f %12.6f\n", i, (i << (32-LDBTAB)) | (1 << (31-LDBTAB)), dbtab1[i], dbtab2[i]);
  54. }
  55. for (i = 0; i < NCOSTAB; i++) {
  56. *(int *)&t = 0x40800000 | (i << (23-LCOSTAB)) | (1 << (22-LCOSTAB));
  57. //fprintf(stderr, "xxx %f\n", t);
  58. tcostab[i] = 2.f*cos(t-4.0f);
  59. //fprintf(stderr, "%4d: %08x %12.6f\n", i, 0x40800000 | (i << (23-LCOSTAB)) | (1 << (22-LCOSTAB)), tcostab[i]);
  60. }
  61. }
  62. /* three possible LSP to f curve functions; the exact computation
  63. (float), a lookup based float implementation, and an integer
  64. implementation. The float lookup is likely the optimal choice on
  65. any machine with an FPU. The integer implementation is *not* fixed
  66. point (due to the need for a large dynamic range and thus a
  67. seperately tracked exponent) and thus much more complex than the
  68. relatively simple float implementations. It's mostly for future
  69. work on a fully fixed point implementation for processors like the
  70. ARM family. */
  71. /* undefine both for the 'old' but more precise implementation */
  72. #undef FLOAT_LOOKUP
  73. #undef INT_LOOKUP
  74. #ifdef FLOAT_LOOKUP
  75. #include "lookup.c" /* catch this in the build system; we #include for
  76. compilers (like gcc) that can't inline across
  77. modules */
  78. /* side effect: changes *lsp to cosines of lsp */
  79. void vorbis_lsp_to_curve(float *curve,int *map,int n,int ln,float *lsp,int m,
  80. float amp,float ampoffset){
  81. int i;
  82. float wdel=M_PI/ln;
  83. vorbis_fpu_control fpu;
  84. vorbis_fpu_setround(&fpu);
  85. for(i=0;i<m;i++)lsp[i]=vorbis_coslook(lsp[i]);
  86. i=0;
  87. while(i<n){
  88. int k=map[i];
  89. int qexp;
  90. float p=.7071067812f;
  91. float q=.7071067812f;
  92. float w=vorbis_coslook(wdel*k);
  93. float *ftmp=lsp;
  94. int c=m>>1;
  95. do{
  96. q*=ftmp[0]-w;
  97. p*=ftmp[1]-w;
  98. ftmp+=2;
  99. }while(--c);
  100. if(m&1){
  101. /* odd order filter; slightly assymetric */
  102. /* the last coefficient */
  103. q*=ftmp[0]-w;
  104. q*=q;
  105. p*=p*(1.f-w*w);
  106. }else{
  107. /* even order filter; still symmetric */
  108. q*=q*(1.f+w);
  109. p*=p*(1.f-w);
  110. }
  111. q=frexp(p+q,&qexp);
  112. q=vorbis_fromdBlook(amp*
  113. vorbis_invsqlook(q)*
  114. vorbis_invsq2explook(qexp+m)-
  115. ampoffset);
  116. do{
  117. curve[i++]=q;
  118. }while(map[i]==k);
  119. }
  120. vorbis_fpu_restore(fpu);
  121. }
  122. #else
  123. #ifdef INT_LOOKUP
  124. #include "lookup.c" /* catch this in the build system; we #include for
  125. compilers (like gcc) that can't inline across
  126. modules */
  127. static int MLOOP_1[64]={
  128. 0,10,11,11, 12,12,12,12, 13,13,13,13, 13,13,13,13,
  129. 14,14,14,14, 14,14,14,14, 14,14,14,14, 14,14,14,14,
  130. 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
  131. 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
  132. };
  133. static int MLOOP_2[64]={
  134. 0,4,5,5, 6,6,6,6, 7,7,7,7, 7,7,7,7,
  135. 8,8,8,8, 8,8,8,8, 8,8,8,8, 8,8,8,8,
  136. 9,9,9,9, 9,9,9,9, 9,9,9,9, 9,9,9,9,
  137. 9,9,9,9, 9,9,9,9, 9,9,9,9, 9,9,9,9,
  138. };
  139. static int MLOOP_3[8]={0,1,2,2,3,3,3,3};
  140. /* side effect: changes *lsp to cosines of lsp */
  141. void vorbis_lsp_to_curve(float *curve,int *map,int n,int ln,float *lsp,int m,
  142. float amp,float ampoffset){
  143. /* 0 <= m < 256 */
  144. /* set up for using all int later */
  145. int i;
  146. int ampoffseti=rint(ampoffset*4096.f);
  147. int ampi=rint(amp*16.f);
  148. long *ilsp=alloca(m*sizeof(long));
  149. for(i=0;i<m;i++)ilsp[i]=vorbis_coslook_i(lsp[i]/M_PI*65536.f+.5f);
  150. i=0;
  151. while(i<n){
  152. int j,k=map[i];
  153. unsigned long pi=46341; /* 2**-.5 in 0.16 */
  154. unsigned long qi=46341;
  155. int qexp=0,shift;
  156. long wi=vorbis_coslook_i(k*65536/ln);
  157. qi*=labs(ilsp[0]-wi);
  158. pi*=labs(ilsp[1]-wi);
  159. for(j=3;j<m;j+=2){
  160. if(!(shift=MLOOP_1[(pi|qi)>>25]))
  161. if(!(shift=MLOOP_2[(pi|qi)>>19]))
  162. shift=MLOOP_3[(pi|qi)>>16];
  163. qi=(qi>>shift)*labs(ilsp[j-1]-wi);
  164. pi=(pi>>shift)*labs(ilsp[j]-wi);
  165. qexp+=shift;
  166. }
  167. if(!(shift=MLOOP_1[(pi|qi)>>25]))
  168. if(!(shift=MLOOP_2[(pi|qi)>>19]))
  169. shift=MLOOP_3[(pi|qi)>>16];
  170. /* pi,qi normalized collectively, both tracked using qexp */
  171. if(m&1){
  172. /* odd order filter; slightly assymetric */
  173. /* the last coefficient */
  174. qi=(qi>>shift)*labs(ilsp[j-1]-wi);
  175. pi=(pi>>shift)<<14;
  176. qexp+=shift;
  177. if(!(shift=MLOOP_1[(pi|qi)>>25]))
  178. if(!(shift=MLOOP_2[(pi|qi)>>19]))
  179. shift=MLOOP_3[(pi|qi)>>16];
  180. pi>>=shift;
  181. qi>>=shift;
  182. qexp+=shift-14*((m+1)>>1);
  183. pi=((pi*pi)>>16);
  184. qi=((qi*qi)>>16);
  185. qexp=qexp*2+m;
  186. pi*=(1<<14)-((wi*wi)>>14);
  187. qi+=pi>>14;
  188. }else{
  189. /* even order filter; still symmetric */
  190. /* p*=p(1-w), q*=q(1+w), let normalization drift because it isn't
  191. worth tracking step by step */
  192. pi>>=shift;
  193. qi>>=shift;
  194. qexp+=shift-7*m;
  195. pi=((pi*pi)>>16);
  196. qi=((qi*qi)>>16);
  197. qexp=qexp*2+m;
  198. pi*=(1<<14)-wi;
  199. qi*=(1<<14)+wi;
  200. qi=(qi+pi)>>14;
  201. }
  202. /* we've let the normalization drift because it wasn't important;
  203. however, for the lookup, things must be normalized again. We
  204. need at most one right shift or a number of left shifts */
  205. if(qi&0xffff0000){ /* checks for 1.xxxxxxxxxxxxxxxx */
  206. qi>>=1; qexp++;
  207. }else
  208. while(qi && !(qi&0x8000)){ /* checks for 0.0xxxxxxxxxxxxxxx or less*/
  209. qi<<=1; qexp--;
  210. }
  211. amp=vorbis_fromdBlook_i(ampi* /* n.4 */
  212. vorbis_invsqlook_i(qi,qexp)-
  213. /* m.8, m+n<=8 */
  214. ampoffseti); /* 8.12[0] */
  215. curve[i]=amp;
  216. while(map[++i]==k)curve[i]=amp;
  217. }
  218. }
  219. #else
  220. /* old, nonoptimized but simple version for any poor sap who needs to
  221. figure out what the hell this code does, or wants the other
  222. fraction of a dB precision */
  223. /* side effect: changes *lsp to cosines of lsp */
  224. void vorbis_lsp_to_curve(float *curve,int *map,int n,int ln,float *lsp,int m,
  225. float amp,float ampoffset){
  226. int i;
  227. float wdel=M_PI/ln;
  228. float ampm2 = 1.f/(amp*amp);
  229. float mamp = fromdB_t2(ampoffset);
  230. {static int needinit=1;if(needinit){needinit=0;initdbtab();}}
  231. for(i=0;i<m;i++)lsp[i]+=4.f;
  232. for(i=0;i<m;i++)lsp[i]=tcos_t(lsp[i]);
  233. i=0;
  234. while(i<n){
  235. int j,k=map[i];
  236. float p=.5f;
  237. float q=.5f;
  238. float p1=1.f;
  239. float q1=1.f;
  240. float w=4.f+wdel*k;
  241. w=tcos_t(w);
  242. for(j=1;j<m-2;j+=4){
  243. q *= w-lsp[j-1];
  244. p *= w-lsp[j];
  245. q1 *= w-lsp[j+1];
  246. p1 *= w-lsp[j+2];
  247. }
  248. for(;j<m;j+=2){
  249. q *= w-lsp[j-1];
  250. p *= w-lsp[j];
  251. }
  252. q *= q1;
  253. p *= p1;
  254. if(j==m){
  255. /* odd order filter; slightly assymetric */
  256. /* the last coefficient */
  257. q*=w-lsp[j-1];
  258. p*=p*(4.f-w*w);
  259. q*=q;
  260. }else{
  261. /* even order filter; still symmetric */
  262. p*=p*(2.f-w);
  263. q*=q*(2.f+w);
  264. }
  265. q=(p+q)*ampm2;
  266. q=mamp*fromdB_t1(q);
  267. curve[i]=q;
  268. while(map[++i]==k)curve[i]=q;
  269. }
  270. }
  271. #endif
  272. #endif
  273. static void cheby(float *g, int ord) {
  274. int i, j;
  275. g[0] *= .5f;
  276. for(i=2; i<= ord; i++) {
  277. for(j=ord; j >= i; j--) {
  278. g[j-2] -= g[j];
  279. g[j] += g[j];
  280. }
  281. }
  282. }
  283. static int comp(const void *a,const void *b){
  284. if(*(float *)a<*(float *)b)
  285. return(1);
  286. else
  287. return(-1);
  288. }
  289. /* Newton-Raphson-Maehly actually functioned as a decent root finder,
  290. but there are root sets for which it gets into limit cycles
  291. (exacerbated by zero suppression) and fails. We can't afford to
  292. fail, even if the failure is 1 in 100,000,000, so we now use
  293. Laguerre and later polish with Newton-Raphson (which can then
  294. afford to fail) */
  295. #define EPSILON 10e-7
  296. static int Laguerre_With_Deflation(float *a,int ord,float *r){
  297. int i,m;
  298. double lastdelta=0.f;
  299. double *defl=alloca(sizeof(double)*(ord+1));
  300. for(i=0;i<=ord;i++)defl[i]=a[i];
  301. for(m=ord;m>0;m--){
  302. double new=0.f,delta;
  303. /* iterate a root */
  304. while(1){
  305. double p=defl[m],pp=0.f,ppp=0.f,denom;
  306. /* eval the polynomial and its first two derivatives */
  307. for(i=m;i>0;i--){
  308. ppp = new*ppp + pp;
  309. pp = new*pp + p;
  310. p = new*p + defl[i-1];
  311. }
  312. /* Laguerre's method */
  313. denom=(m-1) * ((m-1)*pp*pp - m*p*ppp);
  314. if(denom<0)
  315. return(-1); /* complex root! The LPC generator handed us a bad filter */
  316. if(pp>0){
  317. denom = pp + sqrt(denom);
  318. if(denom<EPSILON)denom=EPSILON;
  319. }else{
  320. denom = pp - sqrt(denom);
  321. if(denom>-(EPSILON))denom=-(EPSILON);
  322. }
  323. delta = m*p/denom;
  324. new -= delta;
  325. if(delta<0.f)delta*=-1;
  326. if(fabs(delta/new)<10e-12)break;
  327. lastdelta=delta;
  328. }
  329. r[m-1]=new;
  330. /* forward deflation */
  331. for(i=m;i>0;i--)
  332. defl[i-1]+=new*defl[i];
  333. defl++;
  334. }
  335. return(0);
  336. }
  337. /* for spit-and-polish only */
  338. static int Newton_Raphson(float *a,int ord,float *r){
  339. int i, k, count=0;
  340. double error=1.f;
  341. double *root=alloca(ord*sizeof(double));
  342. for(i=0; i<ord;i++) root[i] = r[i];
  343. while(error>1e-20){
  344. error=0;
  345. for(i=0; i<ord; i++) { /* Update each point. */
  346. double pp=0.,delta;
  347. double rooti=root[i];
  348. double p=a[ord];
  349. for(k=ord-1; k>= 0; k--) {
  350. pp= pp* rooti + p;
  351. p = p * rooti + a[k];
  352. }
  353. delta = p/pp;
  354. root[i] -= delta;
  355. error+= delta*delta;
  356. }
  357. if(count>40)return(-1);
  358. count++;
  359. }
  360. /* Replaced the original bubble sort with a real sort. With your
  361. help, we can eliminate the bubble sort in our lifetime. --Monty */
  362. for(i=0; i<ord;i++) r[i] = root[i];
  363. return(0);
  364. }
  365. /* Convert lpc coefficients to lsp coefficients */
  366. int vorbis_lpc_to_lsp(float *lpc,float *lsp,int m){
  367. int order2=(m+1)>>1;
  368. int g1_order,g2_order;
  369. float *g1=alloca(sizeof(float)*(order2+1));
  370. float *g2=alloca(sizeof(float)*(order2+1));
  371. float *g1r=alloca(sizeof(float)*(order2+1));
  372. float *g2r=alloca(sizeof(float)*(order2+1));
  373. int i;
  374. /* even and odd are slightly different base cases */
  375. g1_order=(m+1)>>1;
  376. g2_order=(m) >>1;
  377. /* Compute the lengths of the x polynomials. */
  378. /* Compute the first half of K & R F1 & F2 polynomials. */
  379. /* Compute half of the symmetric and antisymmetric polynomials. */
  380. /* Remove the roots at +1 and -1. */
  381. g1[g1_order] = 1.f;
  382. for(i=1;i<=g1_order;i++) g1[g1_order-i] = lpc[i-1]+lpc[m-i];
  383. g2[g2_order] = 1.f;
  384. for(i=1;i<=g2_order;i++) g2[g2_order-i] = lpc[i-1]-lpc[m-i];
  385. if(g1_order>g2_order){
  386. for(i=2; i<=g2_order;i++) g2[g2_order-i] += g2[g2_order-i+2];
  387. }else{
  388. for(i=1; i<=g1_order;i++) g1[g1_order-i] -= g1[g1_order-i+1];
  389. for(i=1; i<=g2_order;i++) g2[g2_order-i] += g2[g2_order-i+1];
  390. }
  391. /* Convert into polynomials in cos(alpha) */
  392. cheby(g1,g1_order);
  393. cheby(g2,g2_order);
  394. /* Find the roots of the 2 even polynomials.*/
  395. if(Laguerre_With_Deflation(g1,g1_order,g1r) ||
  396. Laguerre_With_Deflation(g2,g2_order,g2r))
  397. return(-1);
  398. Newton_Raphson(g1,g1_order,g1r); /* if it fails, it leaves g1r alone */
  399. Newton_Raphson(g2,g2_order,g2r); /* if it fails, it leaves g2r alone */
  400. qsort(g1r,g1_order,sizeof(float),comp);
  401. qsort(g2r,g2_order,sizeof(float),comp);
  402. for(i=0;i<g1_order;i++)
  403. lsp[i*2] = acos(g1r[i]);
  404. for(i=0;i<g2_order;i++)
  405. lsp[i*2+1] = acos(g2r[i]);
  406. return(0);
  407. }