lsp.c 12 KB

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