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