00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #include <math.h>
00036 #include <string.h>
00037 #include <stdlib.h>
00038 #include "lsp.h"
00039 #include "os.h"
00040 #include "misc.h"
00041 #include "lookup.h"
00042 #include "scales.h"
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055 #define FLOAT_LOOKUP
00056 #undef INT_LOOKUP
00057
00058 #ifdef FLOAT_LOOKUP
00059 #include "lookup.c"
00060
00061
00062
00063
00064 void vorbis_lsp_to_curve(float *curve,int *map,int n,int ln,float *lsp,int m,
00065 float amp,float ampoffset){
00066 int i;
00067 float wdel=M_PI/ln;
00068
00069
00070
00071 for(i=0;i<m;i++)lsp[i]=vorbis_coslook(lsp[i]);
00072
00073 i=0;
00074 while(i<n){
00075 int k=map[i];
00076 int qexp;
00077 float p=.7071067812f;
00078 float q=.7071067812f;
00079 float w=vorbis_coslook(wdel*k);
00080 float *ftmp=lsp;
00081 int c=m>>1;
00082
00083 do{
00084 q*=ftmp[0]-w;
00085 p*=ftmp[1]-w;
00086 ftmp+=2;
00087 }while(--c);
00088
00089 if(m&1){
00090
00091
00092 q*=ftmp[0]-w;
00093 q*=q;
00094 p*=p*(1.f-w*w);
00095 }else{
00096
00097 q*=q*(1.f+w);
00098 p*=p*(1.f-w);
00099 }
00100
00101 q=frexp(p+q,&qexp);
00102 q=vorbis_fromdBlook(amp*
00103 vorbis_invsqlook(q)*
00104 vorbis_invsq2explook(qexp+m)-
00105 ampoffset);
00106
00107 do{
00108 curve[i++]*=q;
00109 }while(map[i]==k);
00110 }
00111
00112 }
00113
00114 #else
00115
00116 #ifdef INT_LOOKUP
00117 #include "lookup.c"
00118
00119
00120
00121 static int MLOOP_1[64]={
00122 0,10,11,11, 12,12,12,12, 13,13,13,13, 13,13,13,13,
00123 14,14,14,14, 14,14,14,14, 14,14,14,14, 14,14,14,14,
00124 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
00125 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
00126 };
00127
00128 static int MLOOP_2[64]={
00129 0,4,5,5, 6,6,6,6, 7,7,7,7, 7,7,7,7,
00130 8,8,8,8, 8,8,8,8, 8,8,8,8, 8,8,8,8,
00131 9,9,9,9, 9,9,9,9, 9,9,9,9, 9,9,9,9,
00132 9,9,9,9, 9,9,9,9, 9,9,9,9, 9,9,9,9,
00133 };
00134
00135 static int MLOOP_3[8]={0,1,2,2,3,3,3,3};
00136
00137
00138
00139 void vorbis_lsp_to_curve(float *curve,int *map,int n,int ln,float *lsp,int m,
00140 float amp,float ampoffset){
00141
00142
00143
00144
00145 int i;
00146 int ampoffseti=rint(ampoffset*4096.f);
00147 int ampi=rint(amp*16.f);
00148 long *ilsp=alloca(m*sizeof(*ilsp));
00149 for(i=0;i<m;i++)ilsp[i]=vorbis_coslook_i(lsp[i]/M_PI*65536.f+.5f);
00150
00151 i=0;
00152 while(i<n){
00153 int j,k=map[i];
00154 unsigned long pi=46341;
00155 unsigned long qi=46341;
00156 int qexp=0,shift;
00157 long wi=vorbis_coslook_i(k*65536/ln);
00158
00159 qi*=labs(ilsp[0]-wi);
00160 pi*=labs(ilsp[1]-wi);
00161
00162 for(j=3;j<m;j+=2){
00163 if(!(shift=MLOOP_1[(pi|qi)>>25]))
00164 if(!(shift=MLOOP_2[(pi|qi)>>19]))
00165 shift=MLOOP_3[(pi|qi)>>16];
00166 qi=(qi>>shift)*labs(ilsp[j-1]-wi);
00167 pi=(pi>>shift)*labs(ilsp[j]-wi);
00168 qexp+=shift;
00169 }
00170 if(!(shift=MLOOP_1[(pi|qi)>>25]))
00171 if(!(shift=MLOOP_2[(pi|qi)>>19]))
00172 shift=MLOOP_3[(pi|qi)>>16];
00173
00174
00175
00176 if(m&1){
00177
00178
00179 qi=(qi>>shift)*labs(ilsp[j-1]-wi);
00180 pi=(pi>>shift)<<14;
00181 qexp+=shift;
00182
00183 if(!(shift=MLOOP_1[(pi|qi)>>25]))
00184 if(!(shift=MLOOP_2[(pi|qi)>>19]))
00185 shift=MLOOP_3[(pi|qi)>>16];
00186
00187 pi>>=shift;
00188 qi>>=shift;
00189 qexp+=shift-14*((m+1)>>1);
00190
00191 pi=((pi*pi)>>16);
00192 qi=((qi*qi)>>16);
00193 qexp=qexp*2+m;
00194
00195 pi*=(1<<14)-((wi*wi)>>14);
00196 qi+=pi>>14;
00197
00198 }else{
00199
00200
00201
00202
00203
00204 pi>>=shift;
00205 qi>>=shift;
00206 qexp+=shift-7*m;
00207
00208 pi=((pi*pi)>>16);
00209 qi=((qi*qi)>>16);
00210 qexp=qexp*2+m;
00211
00212 pi*=(1<<14)-wi;
00213 qi*=(1<<14)+wi;
00214 qi=(qi+pi)>>14;
00215
00216 }
00217
00218
00219
00220
00221
00222
00223 if(qi&0xffff0000){
00224 qi>>=1; qexp++;
00225 }else
00226 while(qi && !(qi&0x8000)){
00227 qi<<=1; qexp--;
00228 }
00229
00230 amp=vorbis_fromdBlook_i(ampi*
00231 vorbis_invsqlook_i(qi,qexp)-
00232
00233 ampoffseti);
00234
00235 curve[i]*=amp;
00236 while(map[++i]==k)curve[i]*=amp;
00237 }
00238 }
00239
00240 #else
00241
00242
00243
00244
00245
00246
00247 void vorbis_lsp_to_curve(float *curve,int *map,int n,int ln,float *lsp,int m,
00248 float amp,float ampoffset){
00249 int i;
00250 float wdel=M_PI/ln;
00251 for(i=0;i<m;i++)lsp[i]=2.f*cos(lsp[i]);
00252
00253 i=0;
00254 while(i<n){
00255 int j,k=map[i];
00256 float p=.5f;
00257 float q=.5f;
00258 float w=2.f*cos(wdel*k);
00259 for(j=1;j<m;j+=2){
00260 q *= w-lsp[j-1];
00261 p *= w-lsp[j];
00262 }
00263 if(j==m){
00264
00265
00266 q*=w-lsp[j-1];
00267 p*=p*(4.f-w*w);
00268 q*=q;
00269 }else{
00270
00271 p*=p*(2.f-w);
00272 q*=q*(2.f+w);
00273 }
00274
00275 q=fromdB(amp/sqrt(p+q)-ampoffset);
00276
00277 curve[i]*=q;
00278 while(map[++i]==k)curve[i]*=q;
00279 }
00280 }
00281
00282 #endif
00283 #endif
00284
00285 static void cheby(float *g, int ord) {
00286 int i, j;
00287
00288 g[0] *= .5f;
00289 for(i=2; i<= ord; i++) {
00290 for(j=ord; j >= i; j--) {
00291 g[j-2] -= g[j];
00292 g[j] += g[j];
00293 }
00294 }
00295 }
00296
00297 static int comp(const void *a,const void *b){
00298 return (*(float *)a<*(float *)b)-(*(float *)a>*(float *)b);
00299 }
00300
00301
00302
00303
00304
00305
00306
00307
00308 #define EPSILON 10e-7
00309 static int Laguerre_With_Deflation(float *a,int ord,float *r){
00310 int i,m;
00311
00312 double *defl= (double*)_ogg_malloc(sizeof(*defl)*(ord+1));
00313 double *defl_cpy = defl;
00314 for(i=0;i<=ord;i++)defl[i]=a[i];
00315
00316 for(m=ord;m>0;m--){
00317 double newval = 0.f,delta;
00318
00319
00320 while(1){
00321 double p=defl[m],pp=0.f,ppp=0.f,denom;
00322
00323
00324 for(i=m;i>0;i--){
00325 ppp = newval*ppp + pp;
00326 pp = newval*pp + p;
00327 p = newval*p + defl[i-1];
00328 }
00329
00330
00331 denom=(m-1) * ((m-1)*pp*pp - m*p*ppp);
00332 if(denom<0){
00333 free(defl_cpy);
00334 return(-1);
00335 }
00336
00337
00338 if(pp>0){
00339 denom = pp + sqrt(denom);
00340 if(denom<EPSILON)denom=EPSILON;
00341 }else{
00342 denom = pp - sqrt(denom);
00343 if(denom>-(EPSILON))denom=-(EPSILON);
00344 }
00345
00346 delta = m*p/denom;
00347 newval -= delta;
00348
00349 if(delta<0.f)delta*=-1;
00350
00351 if(fabs(delta/newval)<10e-12)break;
00352
00353 }
00354
00355 r[m-1]=newval;
00356
00357
00358
00359 for(i=m;i>0;i--)
00360 defl[i-1]+=newval*defl[i];
00361 defl++;
00362
00363 }
00364
00365 free(defl_cpy);
00366 return(0);
00367 }
00368
00369
00370
00371 static int Newton_Raphson(float *a,int ord,float *r)
00372 {
00373 int i, k, count=0;
00374 double error=1.f;
00375 double *root= (double*)_ogg_malloc(ord*sizeof(*root));
00376 double *root_cpy = root;
00377
00378 for(i=0; i<ord;i++) root[i] = r[i];
00379
00380 while(error>1e-20){
00381 error=0;
00382
00383 for(i=0; i<ord; i++) {
00384 double pp=0.,delta;
00385 double rooti=root[i];
00386 double p=a[ord];
00387 for(k=ord-1; k>= 0; k--) {
00388
00389 pp= pp* rooti + p;
00390 p = p * rooti + a[k];
00391 }
00392
00393 delta = p/pp;
00394 root[i] -= delta;
00395 error+= delta*delta;
00396 }
00397
00398 if(count>40){
00399 free(root_cpy);
00400 return(-1);
00401 }
00402
00403
00404 count++;
00405 }
00406
00407
00408
00409
00410 for(i=0; i<ord;i++) r[i] = root[i];
00411 free(root_cpy);
00412 return(0);
00413 }
00414
00415
00416
00417 int vorbis_lpc_to_lsp(float *lpc,float *lsp,int m){
00418 int order2=(m+1)>>1;
00419 int g1_order,g2_order;
00420
00421
00422
00423
00424
00425
00426
00427 float *g1=(float*)_ogg_malloc(sizeof(*g1)*(order2+1));
00428 float *g2=(float*)_ogg_malloc(sizeof(*g2)*(order2+1));
00429 float *g1r=(float*)_ogg_malloc(sizeof(*g1r)*(order2+1));
00430 float *g2r=(float*)_ogg_malloc(sizeof(*g2r)*(order2+1));
00431 float *g1_cpy = g1;
00432 float *g2_cpy = g2;
00433 float *g1r_cpy = g1r;
00434 float *g2r_cpy = g2r;
00435
00436 int i;
00437
00438
00439 g1_order=(m+1)>>1;
00440 g2_order=(m) >>1;
00441
00442
00443
00444
00445
00446
00447 g1[g1_order] = 1.f;
00448 for(i=1;i<=g1_order;i++) g1[g1_order-i] = lpc[i-1]+lpc[m-i];
00449 g2[g2_order] = 1.f;
00450 for(i=1;i<=g2_order;i++) g2[g2_order-i] = lpc[i-1]-lpc[m-i];
00451
00452 if(g1_order>g2_order){
00453 for(i=2; i<=g2_order;i++) g2[g2_order-i] += g2[g2_order-i+2];
00454 }else{
00455 for(i=1; i<=g1_order;i++) g1[g1_order-i] -= g1[g1_order-i+1];
00456 for(i=1; i<=g2_order;i++) g2[g2_order-i] += g2[g2_order-i+1];
00457 }
00458
00459
00460 cheby(g1,g1_order);
00461 cheby(g2,g2_order);
00462
00463
00464 if(Laguerre_With_Deflation(g1,g1_order,g1r) ||
00465 Laguerre_With_Deflation(g2,g2_order,g2r)){
00466
00467 free(g1_cpy);
00468 free(g2_cpy);
00469 free(g1r_cpy);
00470 free(g2r_cpy);
00471
00472 return(-1);
00473 }
00474
00475
00476 Newton_Raphson(g1,g1_order,g1r);
00477 Newton_Raphson(g2,g2_order,g2r);
00478
00479 qsort(g1r,g1_order,sizeof(*g1r),comp);
00480 qsort(g2r,g2_order,sizeof(*g2r),comp);
00481
00482 for(i=0;i<g1_order;i++)
00483 lsp[i*2] = acos(g1r[i]);
00484
00485 for(i=0;i<g2_order;i++)
00486 lsp[i*2+1] = acos(g2r[i]);
00487
00488
00489 free(g1_cpy);
00490 free(g2_cpy);
00491 free(g1r_cpy);
00492 free(g2r_cpy);
00493
00494
00495 return(0);
00496 }