lsp.c 12 KB

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