media/libvorbis/lib/vorbis_lsp.c

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

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

mercurial