lsp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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 19453 2015-03-02 22:35:34Z xiphmont $
  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 *defl=alloca(sizeof(*defl)*(ord+1));
  256. for(i=0;i<=ord;i++)defl[i]=a[i];
  257. for(m=ord;m>0;m--){
  258. double new=0.f,delta;
  259. /* iterate a root */
  260. while(1){
  261. double p=defl[m],pp=0.f,ppp=0.f,denom;
  262. /* eval the polynomial and its first two derivatives */
  263. for(i=m;i>0;i--){
  264. ppp = new*ppp + pp;
  265. pp = new*pp + p;
  266. p = new*p + defl[i-1];
  267. }
  268. /* Laguerre's method */
  269. denom=(m-1) * ((m-1)*pp*pp - m*p*ppp);
  270. if(denom<0)
  271. return(-1); /* complex root! The LPC generator handed us a bad filter */
  272. if(pp>0){
  273. denom = pp + sqrt(denom);
  274. if(denom<EPSILON)denom=EPSILON;
  275. }else{
  276. denom = pp - sqrt(denom);
  277. if(denom>-(EPSILON))denom=-(EPSILON);
  278. }
  279. delta = m*p/denom;
  280. new -= delta;
  281. if(delta<0.f)delta*=-1;
  282. if(fabs(delta/new)<10e-12)break;
  283. }
  284. r[m-1]=new;
  285. /* forward deflation */
  286. for(i=m;i>0;i--)
  287. defl[i-1]+=new*defl[i];
  288. defl++;
  289. }
  290. return(0);
  291. }
  292. /* for spit-and-polish only */
  293. static int Newton_Raphson(float *a,int ord,float *r){
  294. int i, k, count=0;
  295. double error=1.f;
  296. double *root=alloca(ord*sizeof(*root));
  297. for(i=0; i<ord;i++) root[i] = r[i];
  298. while(error>1e-20){
  299. error=0;
  300. for(i=0; i<ord; i++) { /* Update each point. */
  301. double pp=0.,delta;
  302. double rooti=root[i];
  303. double p=a[ord];
  304. for(k=ord-1; k>= 0; k--) {
  305. pp= pp* rooti + p;
  306. p = p * rooti + a[k];
  307. }
  308. delta = p/pp;
  309. root[i] -= delta;
  310. error+= delta*delta;
  311. }
  312. if(count>40)return(-1);
  313. count++;
  314. }
  315. /* Replaced the original bubble sort with a real sort. With your
  316. help, we can eliminate the bubble sort in our lifetime. --Monty */
  317. for(i=0; i<ord;i++) r[i] = root[i];
  318. return(0);
  319. }
  320. /* Convert lpc coefficients to lsp coefficients */
  321. int vorbis_lpc_to_lsp(float *lpc,float *lsp,int m){
  322. int order2=(m+1)>>1;
  323. int g1_order,g2_order;
  324. float *g1=alloca(sizeof(*g1)*(order2+1));
  325. float *g2=alloca(sizeof(*g2)*(order2+1));
  326. float *g1r=alloca(sizeof(*g1r)*(order2+1));
  327. float *g2r=alloca(sizeof(*g2r)*(order2+1));
  328. int i;
  329. /* even and odd are slightly different base cases */
  330. g1_order=(m+1)>>1;
  331. g2_order=(m) >>1;
  332. /* Compute the lengths of the x polynomials. */
  333. /* Compute the first half of K & R F1 & F2 polynomials. */
  334. /* Compute half of the symmetric and antisymmetric polynomials. */
  335. /* Remove the roots at +1 and -1. */
  336. g1[g1_order] = 1.f;
  337. for(i=1;i<=g1_order;i++) g1[g1_order-i] = lpc[i-1]+lpc[m-i];
  338. g2[g2_order] = 1.f;
  339. for(i=1;i<=g2_order;i++) g2[g2_order-i] = lpc[i-1]-lpc[m-i];
  340. if(g1_order>g2_order){
  341. for(i=2; i<=g2_order;i++) g2[g2_order-i] += g2[g2_order-i+2];
  342. }else{
  343. for(i=1; i<=g1_order;i++) g1[g1_order-i] -= g1[g1_order-i+1];
  344. for(i=1; i<=g2_order;i++) g2[g2_order-i] += g2[g2_order-i+1];
  345. }
  346. /* Convert into polynomials in cos(alpha) */
  347. cheby(g1,g1_order);
  348. cheby(g2,g2_order);
  349. /* Find the roots of the 2 even polynomials.*/
  350. if(Laguerre_With_Deflation(g1,g1_order,g1r) ||
  351. Laguerre_With_Deflation(g2,g2_order,g2r))
  352. return(-1);
  353. Newton_Raphson(g1,g1_order,g1r); /* if it fails, it leaves g1r alone */
  354. Newton_Raphson(g2,g2_order,g2r); /* if it fails, it leaves g2r alone */
  355. qsort(g1r,g1_order,sizeof(*g1r),comp);
  356. qsort(g2r,g2_order,sizeof(*g2r),comp);
  357. for(i=0;i<g1_order;i++)
  358. lsp[i*2] = acos(g1r[i]);
  359. for(i=0;i<g2_order;i++)
  360. lsp[i*2+1] = acos(g2r[i]);
  361. return(0);
  362. }