New LSP books, more reliable fit and lower bit usage
[platform/upstream/libvorbis.git] / lib / envelope.c
1 /********************************************************************
2  *                                                                  *
3  * THIS FILE IS PART OF THE Ogg Vorbis SOFTWARE CODEC SOURCE CODE.  *
4  * USE, DISTRIBUTION AND REPRODUCTION OF THIS SOURCE IS GOVERNED BY *
5  * THE GNU PUBLIC LICENSE 2, WHICH IS INCLUDED WITH THIS SOURCE.    *
6  * PLEASE READ THESE TERMS DISTRIBUTING.                            *
7  *                                                                  *
8  * THE OggSQUISH SOURCE CODE IS (C) COPYRIGHT 1994-2000             *
9  * by Monty <monty@xiph.org> and The XIPHOPHORUS Company            *
10  * http://www.xiph.org/                                             *
11  *                                                                  *
12  ********************************************************************
13
14  function: PCM data envelope analysis and manipulation
15  last mod: $Id: envelope.c,v 1.20 2000/07/12 09:36:17 xiphmont Exp $
16
17  Preecho calculation.
18
19  ********************************************************************/
20
21 #include <stdlib.h>
22 #include <string.h>
23 #include <stdio.h>
24 #include <math.h>
25 #include "vorbis/codec.h"
26
27 #include "os.h"
28 #include "scales.h"
29 #include "envelope.h"
30 #include "bitwise.h"
31 #include "misc.h"
32
33 /* We use a Chebyshev bandbass for the preecho trigger bandpass; it's
34    close enough for sample rates 32000-48000 Hz (corner frequencies at
35    6k/14k assuming sample rate of 44.1kHz) */
36
37 /* Digital filter designed by mkfilter/mkshape/gencode A.J. Fisher
38    Command line: /www/usr/fisher/helpers/mkfilter -Ch \
39    -6.0000000000e+00 -Bp -o 5 -a 1.3605442177e-01 3.1746031746e-01 -l */
40
41 static int    cheb_bandpass_stages=10;
42 static double cheb_bandpass_gain=5.589612458e+01;
43 /* z^-stage, z^-stage+1... */
44 static double cheb_bandpass_B[]={-1.,0.,5.,0.,-10.,0.,10.,0.,-5.,0.,1};
45 static double cheb_bandpass_A[]={
46   -0.1917409386,
47   0.0078657069,
48   -0.7126903444,
49   0.0266343467,
50   -1.4047174730,
51   0.0466964232,
52   -1.9032773429,
53   0.0451493360,
54   -1.4471447397,
55   0.0303413711};
56
57 static int    cheb_highpass_stages=10;
58 static double cheb_highpass_gain= 5.291963434e+01;
59 /* z^-stage, z^-stage+1... */
60 static double cheb_highpass_B[]={1,-10,45,-120,210,-252,210,-120,45,-10,1};
61 static double cheb_highpass_A[]={
62   -0.1247628029,
63   0.1334086523,
64   -0.3997715614,
65   0.3213011089,
66   -1.1131924119,
67   1.7692446626,
68   -3.6241199038,
69   4.1950871291,
70   -4.2771757867,
71   2.3920318913};
72
73 void _ve_envelope_init(envelope_lookup *e,vorbis_info *vi){
74   long rate=vi->rate;
75   int ch=vi->channels;
76   int window=vi->envelopesa;
77   int i;
78   e->winlength=window;
79   e->iir=calloc(ch,sizeof(IIR_state));
80   e->filtered=calloc(ch,sizeof(double *));
81   e->ch=ch;
82   e->storage=128;
83   for(i=0;i<ch;i++){
84     IIR_init(e->iir+i,cheb_highpass_stages,cheb_highpass_gain,
85              cheb_highpass_A,cheb_highpass_B);
86     e->filtered[i]=calloc(e->storage,sizeof(double));
87   }
88
89   drft_init(&e->drft,window);
90   e->window=malloc(e->winlength*sizeof(double));
91   /* We just use a straight sin(x) window for this */
92   for(i=0;i<e->winlength;i++)
93     e->window[i]=sin((i+.5)/e->winlength*M_PI);
94 }
95
96 void _ve_envelope_clear(envelope_lookup *e){
97   int i;
98   for(i=0;i<e->ch;i++){
99     IIR_clear((e->iir+i));
100     free(e->filtered[i]);
101   }
102   drft_clear(&e->drft);
103   free(e->window);
104   free(e->filtered);
105   memset(e,0,sizeof(envelope_lookup));
106 }
107
108 /*static int frameno=0;*/
109
110 static void smooth_noise(long n,double *f,double *noise){
111   long i;
112   long lo=0,hi=0;
113   double acc=0.;
114
115   for(i=0;i<n;i++){
116     long newhi=i*1.0442718740+5;
117     long newlo=i*.8781245150-5;
118     if(newhi>n)newhi=n;
119     
120     for(;lo<newlo;lo++)
121       acc-=f[lo]*f[lo];
122     for(;hi<newhi;hi++)
123       acc+=f[hi]*f[hi];
124     noise[i]=todB(sqrt(acc/(hi-lo)));
125   }
126 }
127
128 static double _ve_deltai(envelope_lookup *ve,IIR_state *iir,
129                       double *pre,double *post){
130   long no=ve->winlength/3; /* past the highpass rollon! */
131   long n2=ve->winlength*2;
132   long n=ve->winlength;
133
134   double *workA=alloca(sizeof(double)*n2),A=0.;
135   double *workB=alloca(sizeof(double)*n2),B=0.;
136   long i;
137
138   /*_analysis_output("A",frameno,pre,n,0,0);
139     _analysis_output("B",frameno,post,n,0,0);*/
140
141   for(i=0;i<n;i++){
142     workA[i]=pre[i]*ve->window[i];
143     workB[i]=post[i]*ve->window[i];
144   }
145
146   /*_analysis_output("Awin",frameno,workA,n,0,0);
147     _analysis_output("Bwin",frameno,workB,n,0,0);*/
148
149   drft_forward(&ve->drft,workA);
150   drft_forward(&ve->drft,workB);
151   /*_analysis_output("Afft",frameno,workA,n,0,0);
152     _analysis_output("Bfft",frameno,workB,n,0,0);*/
153
154   for(i=0;i<n;i++){
155     A+=workA[i]*workA[i];
156     B+=workB[i]*workB[i];
157   }
158
159   A=todB(A);
160   B=todB(B);
161
162   return(B-A);
163 }
164
165 long _ve_envelope_search(vorbis_dsp_state *v,long searchpoint){
166   vorbis_info *vi=v->vi;
167   envelope_lookup *ve=v->ve;
168   long i,j;
169   
170   /* make sure we have enough storage to match the PCM */
171   if(v->pcm_storage>ve->storage){
172     ve->storage=v->pcm_storage;
173     for(i=0;i<ve->ch;i++)
174       ve->filtered[i]=realloc(ve->filtered[i],ve->storage*sizeof(double));
175   }
176
177   /* catch up the highpass to match the pcm */
178   for(i=0;i<ve->ch;i++){
179     double *filtered=ve->filtered[i];
180     double *pcm=v->pcm[i];
181     IIR_state *iir=ve->iir+i;
182     
183     for(j=ve->current;j<v->pcm_current;j++)
184       filtered[j]=IIR_filter(iir,pcm[j]);
185   }
186   ve->current=v->pcm_current;
187
188   /* Now search through our cached highpass data for breaking points */
189   /* starting point */
190   if(v->W)
191     j=v->centerW+vi->blocksizes[1]/4-vi->blocksizes[0]/4;
192   else
193     j=v->centerW;
194
195   while(j+ve->winlength<=v->pcm_current){
196     for(i=0;i<ve->ch;i++){
197       double *filtered=ve->filtered[i]+j;
198       IIR_state *iir=ve->iir+i;
199       double m=_ve_deltai(ve,iir,filtered-ve->winlength,filtered);
200       
201       if(m>vi->preecho_thresh){
202         /*frameno++;*/
203         return(0);
204       }
205       /*frameno++;*/
206     }
207     
208     j+=vi->blocksizes[0]/2;
209     if(j>=searchpoint)return(1);
210   }
211   
212   return(-1);
213 }
214
215 void _ve_envelope_shift(envelope_lookup *e,long shift){
216   int i;
217   for(i=0;i<e->ch;i++)
218     memmove(e->filtered[i],e->filtered[i]+shift,(e->current-shift)*
219             sizeof(double));
220   e->current-=shift;
221 }
222
223