X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=lib%2Flpc.c;h=877da47f8efeb9ee138479f4a152d793e0fdc878;hb=80e7915d0271dce18deddca3a468a7cb61b2e50f;hp=8ae1561922771e645614ae8039d5a3a1a3d0b97d;hpb=9a89cfb8602f9e2e242c69790c4327626bbd346a;p=platform%2Fupstream%2Flibvorbis.git diff --git a/lib/lpc.c b/lib/lpc.c index 8ae1561..877da47 100644 --- a/lib/lpc.c +++ b/lib/lpc.c @@ -1,18 +1,16 @@ /******************************************************************** * * - * THIS FILE IS PART OF THE Ogg Vorbis SOFTWARE CODEC SOURCE CODE. * - * USE, DISTRIBUTION AND REPRODUCTION OF THIS SOURCE IS GOVERNED BY * - * THE GNU PUBLIC LICENSE 2, WHICH IS INCLUDED WITH THIS SOURCE. * - * PLEASE READ THESE TERMS DISTRIBUTING. * + * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. * + * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * + * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * + * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * * * - * THE OggSQUISH SOURCE CODE IS (C) COPYRIGHT 1994-2000 * - * by Monty and The XIPHOPHORUS Company * - * http://www.xiph.org/ * + * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2009 * + * by the Xiph.Org Foundation https://xiph.org/ * * * ******************************************************************** function: LPC low level routines - last mod: $Id: lpc.c,v 1.11 1999/12/31 12:35:14 xiphmont Exp $ ********************************************************************/ @@ -45,13 +43,13 @@ Carsten Bormann *********************************************************************/ #include -#include #include #include #include "os.h" #include "smallft.h" #include "lpc.h" -#include "barkmel.h" +#include "scales.h" +#include "misc.h" /* Autocorrelation LPC coeff generation algorithm invented by N. Levinson in 1947, modified by J. Durbin in 1959. */ @@ -59,30 +57,34 @@ Carsten Bormann /* Input : n elements of time doamin data Output: m lpc coefficients, excitation energy */ -double vorbis_lpc_from_data(double *data,double *lpc,int n,int m){ - double *aut=alloca(sizeof(double)*(m+1)); +float vorbis_lpc_from_data(float *data,float *lpci,int n,int m){ + double *aut=alloca(sizeof(*aut)*(m+1)); + double *lpc=alloca(sizeof(*lpc)*(m)); double error; + double epsilon; int i,j; /* autocorrelation, p+1 lag coefficients */ - j=m+1; while(j--){ - double d=0; - for(i=j;iln; - int m=l->m; - double *work=alloca(sizeof(double)*(n+n)); - double fscale=.5/n; - int i,j; - - /* input is a real curve. make it complex-real */ - /* This mixes phase, but the LPC generation doesn't care. */ - for(i=0;ifft,work); - - /* The autocorrelation will not be circular. Shift, else we lose - most of the power in the edges. */ - - for(i=0,j=n/2;in=n; - l->ln=mapped; - l->m=m; - - l->linearmap=malloc(n*sizeof(int)); - l->barknorm=malloc(mapped*sizeof(double)); - - /* we choose a scaling constant so that: - floor(bark(rate-1)*C)=mapped-1 - floor(bark(rate)*C)=mapped */ - - scale=mapped/fBARK(rate); - - /* the mapping from a linear scale to a smaller bark scale is - straightforward with a single catch; make sure not to skip any - bark-scale bins. In order to do this, we assign map_N = min - (map_N-1 + 1, bark(N)) */ - { - int last=-1; - for(i=0;i=mapped)val=mapped; /* guard against the approximation */ - if(val>last+1)val=last+1; - l->linearmap[i]=val; - last=val; - } - } - - /* 'Normalization' is just making sure that power isn't lost in the - log scale by virtue of compressing the scale in higher - frequencies. We figure the weight of bands in proportion to - their linear/bark width ratio below, again, authoritatively. We - use computed width (not the number of actual bins above) for - smoothness in the scale; they should agree closely unless the - encoder chose parameters poorly (and got a bark scale that would - have had lots of skipped bins) */ - - for(i=0;ibarknorm[i]=iBARK((i+1)/scale)-iBARK(i/scale); - - /* we cheat decoding the LPC spectrum via FFTs */ - - drft_init(&l->fft,mapped*2); - -} - -void lpc_clear(lpc_lookup *l){ - if(l){ - if(l->barknorm)free(l->barknorm); - if(l->linearmap)free(l->linearmap); - drft_clear(&l->fft); - } -} - - -/* less efficient than the decode side (written for clarity). We're - not bottlenecked here anyway */ - -double vorbis_curve_to_lpc(double *curve,double *lpc,lpc_lookup *l){ - /* map the input curve to a bark-scale curve for encoding */ - - int mapped=l->ln; - double *work=alloca(sizeof(double)*mapped); - int i; - - memset(work,0,sizeof(double)*mapped); - - /* Only the decode side is behavior-specced; for now in the encoder, - we select the maximum value of each band as representative (this - helps make sure peaks don't go out of range. In error terms, - selecting min would make more sense, but the codebook is trained - numerically, so we don't lose in encoding. We'd still want to - use the original curve for error and noise estimation */ - - for(i=0;in;i++){ - int bark=l->linearmap[i]; - if(work[bark]barknorm[i]; - -#ifdef ANALYSIS - { - int j; - FILE *out; - char buffer[80]; - static int frameno=0; - - sprintf(buffer,"prelpc%d.m",frameno); - out=fopen(buffer,"w+"); - for(j=0;jn;j++) - fprintf(out,"%g\n",curve[j]); - fclose(out); - sprintf(buffer,"preloglpc%d.m",frameno++); - out=fopen(buffer,"w+"); - for(j=0;jln;j++) - fprintf(out,"%g\n",work[j]); - fclose(out); - } -#endif - - return vorbis_lpc_from_spectrum(work,lpc,l); -} - - -/* One can do this the long way by generating the transfer function in - the time domain and taking the forward FFT of the result. The - results from direct calculation are cleaner and faster. + if(i&1)lpc[j]+=lpc[j]*r; - This version does a linear curve generation and then later - interpolates the log curve from the linear curve. This could stand - optimization; it could both be more precise as well as not compute - quite a few unused values */ + error*=1.-r*r; -void _vlpc_de_helper(double *curve,double *lpc,double amp, - lpc_lookup *l){ - int i; - memset(curve,0,sizeof(double)*l->ln*2); - if(amp==0)return; - - for(i=0;im;i++){ - curve[i*2+1]=lpc[i]/4/amp; - curve[i*2+2]=-lpc[i]/4/amp; } - drft_backward(&l->fft,curve); /* reappropriated ;-) */ + done: + /* slightly damp the filter */ { - int l2=l->ln*2; - double unit=1./amp; - curve[0]=(1./(curve[0]*2+unit)); - for(i=1;iln;i++){ - double real=(curve[i]+curve[l2-i]); - double imag=(curve[i]-curve[l2-i]); - curve[i]=(1./hypot(real+unit,imag)); + double g = .99; + double damp = g; + for(j=0;jln*2)); - int i; - static int frameno=0; - - _vlpc_de_helper(lcurve,lpc,amp,l); - -#ifdef ANALYSIS - { - int j; - FILE *out; - char buffer[80]; - - sprintf(buffer,"loglpc%d.m",frameno++); - out=fopen(buffer,"w+"); - for(j=0;jln;j++) - fprintf(out,"%g\n",lcurve[j]); - fclose(out); - } -#endif - - if(amp==0)return; - - for(i=0;iln;i++)lcurve[i]/=l->barknorm[i]; - for(i=0;in;i++)curve[i]=lcurve[l->linearmap[i]]; - -#ifdef ANALYSIS - { - int j; - FILE *out; - char buffer[80]; - - sprintf(buffer,"lpc%d.m",frameno-1); - out=fopen(buffer,"w+"); - for(j=0;jn;j++) - fprintf(out,"%g\n",curve[j]); - fclose(out); - } -#endif -} - -void vorbis_lpc_apply(double *residue,double *lpc,double amp,lpc_lookup *l){ - double *lcurve=alloca(sizeof(double)*((l->ln+l->n)*2)); - int i; - static int frameno=0; - - if(amp==0){ - memset(residue,0,l->n*sizeof(double)); - }else{ - - _vlpc_de_helper(lcurve,lpc,amp,l); - -#ifdef ANALYSIS - { - int j; - FILE *out; - char buffer[80]; - - sprintf(buffer,"loglpc%d.m",frameno++); - out=fopen(buffer,"w+"); - for(j=0;jln;j++) - fprintf(out,"%g\n",lcurve[j]); - fclose(out); - } -#endif - - for(i=0;iln;i++)lcurve[i]/=l->barknorm[i]; - for(i=0;in;i++) - if(residue[i]!=0) - residue[i]*=lcurve[l->linearmap[i]]; - } -} - -/* subtract or add an lpc filter to data. Vorbis doesn't actually use this. */ - -void vorbis_lpc_residue(double *coeff,double *prime,int m, - double *data,long n){ - /* in: coeff[0...m-1] LPC coefficients - prime[0...m-1] initial values - data[0...n-1] data samples - out: data[0...n-1] residuals from LPC prediction */ + for(j=0;j