New LSP root finding code; replace Newton-Raphson-Maehly with Laguerre's
[platform/upstream/libvorbis.git] / lib / envelope.c
1 /********************************************************************
2  *                                                                  *
3  * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE.   *
4  * USE, DISTRIBUTION AND REPRODUCTION OF THIS SOURCE IS GOVERNED BY *
5  * THE GNU LESSER/LIBRARY PUBLIC LICENSE, WHICH IS INCLUDED WITH    *
6  * THIS SOURCE. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.        *
7  *                                                                  *
8  * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2001             *
9  * by the XIPHOPHORUS Company http://www.xiph.org/                  *
10  *                                                                  *
11  ********************************************************************
12
13  function: PCM data envelope analysis and manipulation
14  last mod: $Id: envelope.c,v 1.33 2001/02/17 10:13:47 xiphmont Exp $
15
16  Preecho calculation.
17
18  ********************************************************************/
19
20 #include <stdlib.h>
21 #include <string.h>
22 #include <stdio.h>
23 #include <math.h>
24 #include <ogg/ogg.h>
25 #include "vorbis/codec.h"
26 #include "codec_internal.h"
27
28 #include "os.h"
29 #include "scales.h"
30 #include "envelope.h"
31 #include "misc.h"
32
33 /* Digital filter designed by mkfilter/mkshape/gencode A.J. Fisher */
34
35 static int   cheb_highpass_stages=6;
36 static float cheb_highpass_B[]={1.f,-6.f,15.f,-20.f,15.f,-6.f,1.f};
37
38 static int   cheb_bandpass_stages=6;
39 static float cheb_bandpass_B[]={-1.f,0.f,3.f,0.f,-3.f,0.f,1.f};
40
41
42 /* 10kHz Chebyshev highpass */
43 static float cheb_highpass10k_gain= 54.34519586f;
44 static float cheb_highpass10k_A[]={
45   -0.2064797169f,
46   -0.5609713214f,
47   -1.1352465327f,
48   -1.4495555418f,
49   -1.7938140760f,
50   -0.9473564683f};
51
52 /* 6kHz-10kHz Chebyshev bandpass */
53 static float cheb_bandpass6k_gain=113.4643935f;
54 static float cheb_bandpass6k_A[]={
55   -0.5712621337f,
56   1.5626130710f,
57   -3.3348854983f,
58   4.0471340821f,
59   -4.0051680331f,
60   2.2786325610f};
61
62 /* 3kHz-6kHz Chebyshev bandpass */
63 static float cheb_bandpass3k_gain= 248.8359377f;
64 static float cheb_bandpass3k_A[]={
65   -0.6564230022f,
66   3.3747911257f,
67   -8.0098635981f,
68   11.0040876874f,
69   -9.2250963484f,
70   4.4760355389f};
71
72 /* 1.5kHz-3kHz Chebyshev bandpass */
73 static float cheb_bandpass1k_gain= 1798.537183f;
74 static float cheb_bandpass1k_A[]={
75   -0.8097527363f,
76   4.7725742682f,
77   -11.9800219408f,
78   16.3770336223f,
79   -12.8553129536f,
80   5.4948074309f};
81
82 void _ve_envelope_init(envelope_lookup *e,vorbis_info *vi){
83   codec_setup_info *ci=vi->codec_setup;
84   int ch=vi->channels;
85   int window=ci->envelopesa;
86   int i;
87   e->winlength=window;
88   e->minenergy=fromdB(ci->preecho_minenergy);
89   e->iir=_ogg_calloc(ch*4,sizeof(IIR_state));
90   e->filtered=_ogg_calloc(ch*4,sizeof(float *));
91   e->ch=ch;
92   e->storage=128;
93   for(i=0;i<ch*4;i+=4){
94
95     IIR_init(e->iir+i,cheb_highpass_stages,cheb_highpass10k_gain,
96              cheb_highpass10k_A,cheb_highpass_B);
97     IIR_init(e->iir+i+1,cheb_bandpass_stages,cheb_bandpass6k_gain,
98              cheb_bandpass6k_A,cheb_bandpass_B);
99     IIR_init(e->iir+i+2,cheb_bandpass_stages,cheb_bandpass3k_gain,
100              cheb_bandpass3k_A,cheb_bandpass_B);
101     IIR_init(e->iir+i+3,cheb_bandpass_stages,cheb_bandpass1k_gain,
102              cheb_bandpass1k_A,cheb_bandpass_B);
103
104     e->filtered[i]=_ogg_calloc(e->storage,sizeof(float));
105     e->filtered[i+1]=_ogg_calloc(e->storage,sizeof(float));
106     e->filtered[i+2]=_ogg_calloc(e->storage,sizeof(float));
107     e->filtered[i+3]=_ogg_calloc(e->storage,sizeof(float));
108   }
109
110 }
111
112 void _ve_envelope_clear(envelope_lookup *e){
113   int i;
114   for(i=0;i<e->ch*4;i++){
115     IIR_clear((e->iir+i));
116     _ogg_free(e->filtered[i]);
117   }
118   _ogg_free(e->filtered);
119   _ogg_free(e->iir);
120   memset(e,0,sizeof(envelope_lookup));
121 }
122
123 /* straight threshhold based until we find something that works better
124    and isn't patented */
125 static float _ve_deltai(envelope_lookup *ve,float *pre,float *post){
126   long n=ve->winlength;
127
128   long i;
129
130   /* we want to have a 'minimum bar' for energy, else we're just
131      basing blocks on quantization noise that outweighs the signal
132      itself (for low power signals) */
133
134   float min=ve->minenergy;
135   float A=min*min*n;
136   float B=A;
137
138   for(i=0;i<n;i++){
139     A+=pre[i]*pre[i];
140     B+=post[i]*post[i];
141   }
142
143   A=todB(A);
144   B=todB(B);
145
146   return(B-A);
147 }
148
149 long _ve_envelope_search(vorbis_dsp_state *v,long searchpoint){
150   vorbis_info *vi=v->vi;
151   codec_setup_info *ci=vi->codec_setup;
152   envelope_lookup *ve=((backend_lookup_state *)(v->backend_state))->ve;
153   long i,j,k,l;
154   float *work=alloca(sizeof(float)*ve->winlength*2);
155   static int seq=0;
156
157   /* make sure we have enough storage to match the PCM */
158   if(v->pcm_storage>ve->storage){
159     ve->storage=v->pcm_storage;
160     for(i=0;i<ve->ch*4;i++)
161       ve->filtered[i]=_ogg_realloc(ve->filtered[i],ve->storage*sizeof(float));
162   }
163
164   /* catch up the highpass to match the pcm */
165   for(i=0;i<ve->ch;i++){
166     float *pcm=v->pcm[i];
167     float *filtered0=ve->filtered[i*4];
168     float *filtered1=ve->filtered[i*4+1];
169     float *filtered2=ve->filtered[i*4+2];
170     float *filtered3=ve->filtered[i*4+3];
171     IIR_state *iir0=ve->iir+i*4;
172     IIR_state *iir1=ve->iir+i*4+1;
173     IIR_state *iir2=ve->iir+i*4+2;
174     IIR_state *iir3=ve->iir+i*4+3;
175     int flag=1;
176     
177     for(j=ve->current;j<v->pcm_current;j++){
178       filtered0[j]=IIR_filter(iir0,pcm[j]);
179       filtered1[j]=IIR_filter(iir1,pcm[j]);
180       filtered2[j]=IIR_filter(iir2,pcm[j]);
181       filtered3[j]=IIR_filter(iir3,pcm[j]);
182       if(pcm[j])flag=0;
183     }
184     if(flag && ve->current+64<v->pcm_current){
185       IIR_reset(iir0);
186       IIR_reset(iir1);
187       IIR_reset(iir2);
188       IIR_reset(iir3);
189     }
190
191   }
192
193   ve->current=v->pcm_current;
194
195   /* Now search through our cached highpass data for breaking points */
196   /* starting point */
197   if(v->W)
198     j=v->centerW+ci->blocksizes[1]/4-ci->blocksizes[0]/4;
199   else
200     j=v->centerW;
201
202   if(j<ve->lastmark)j=ve->lastmark;
203   
204   while(j+ve->winlength<=v->pcm_current){
205     for(i=0;i<ve->ch;i++){
206       for(k=0;k<4;k++){
207         float *filtered=ve->filtered[i*4+k]+j;
208         float m=_ve_deltai(ve,filtered-ve->winlength,filtered);
209       
210         if(m>ci->preecho_thresh[k]){
211           /*granulepos++;*/
212           ve->lastmark=j;
213           return(0);
214         }
215         if(m<ci->postecho_thresh[k]){
216           /*granulepos++;*/
217           ve->lastmark=j;
218           return(0);
219         }
220         /*granulepos++;*/
221       }
222     }
223
224     j+=min(ci->blocksizes[0],ve->winlength)/2;
225
226     if(j>=searchpoint){
227       return(1);
228     }
229   }
230  
231   return(-1);
232 }
233
234 void _ve_envelope_shift(envelope_lookup *e,long shift){
235   int i;
236   for(i=0;i<e->ch*4;i++)
237     memmove(e->filtered[i],e->filtered[i]+shift,(e->current-shift)*
238             sizeof(float));
239   e->current-=shift;
240   e->lastmark-=shift;
241 }
242
243