vrshoot

annotate libs/vorbis/lsp.c @ 2:334d17aed7de

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