Hm, the earlier fix for 'slow envelope analysis on silence after
[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-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.25 2000/11/07 09:51:42 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 <ogg/ogg.h>
26 #include "vorbis/codec.h"
27 #include "codec_internal.h"
28
29 #include "os.h"
30 #include "scales.h"
31 #include "envelope.h"
32 #include "misc.h"
33
34 /* We use a Chebyshev bandbass for the preecho trigger bandpass; it's
35    close enough for sample rates 32000-48000 Hz (corner frequencies at
36    6k/14k assuming sample rate of 44.1kHz) */
37
38 /* Digital filter designed by mkfilter/mkshape/gencode A.J. Fisher
39    Command line: /www/usr/fisher/helpers/mkfilter -Ch \
40    -6.0000000000e+00 -Bp -o 5 -a 1.3605442177e-01 3.1746031746e-01 -l */
41
42 #if 0
43 static int    cheb_bandpass_stages=10;
44 static float cheb_bandpass_gain=5.589612458e+01;
45 static float cheb_bandpass_B[]={-1.,0.,5.,0.,-10.,0.,10.,0.,-5.,0.,1};
46 static float cheb_bandpass_A[]={
47   -0.1917409386,
48   0.0078657069,
49   -0.7126903444,
50   0.0266343467,
51   -1.4047174730,
52   0.0466964232,
53   -1.9032773429,
54   0.0451493360,
55   -1.4471447397,
56   0.0303413711};
57 #endif 
58
59 static int    cheb_highpass_stages=10;
60 static float cheb_highpass_gain= 5.291963434e+01;
61 /* z^-stage, z^-stage+1... */
62 static float cheb_highpass_B[]={1,-10,45,-120,210,-252,210,-120,45,-10,1};
63 static float cheb_highpass_A[]={
64   -0.1247628029,
65   0.1334086523,
66   -0.3997715614,
67   0.3213011089,
68   -1.1131924119,
69   1.7692446626,
70   -3.6241199038,
71   4.1950871291,
72   -4.2771757867,
73   2.3920318913};
74
75 void _ve_envelope_init(envelope_lookup *e,vorbis_info *vi){
76   codec_setup_info *ci=vi->codec_setup;
77   int ch=vi->channels;
78   int window=ci->envelopesa;
79   int i;
80   e->winlength=window;
81   e->minenergy=fromdB(ci->preecho_minenergy);
82   e->iir=_ogg_calloc(ch,sizeof(IIR_state));
83   e->filtered=_ogg_calloc(ch,sizeof(float *));
84   e->ch=ch;
85   e->storage=128;
86   for(i=0;i<ch;i++){
87     IIR_init(e->iir+i,cheb_highpass_stages,cheb_highpass_gain,
88              cheb_highpass_A,cheb_highpass_B);
89     e->filtered[i]=_ogg_calloc(e->storage,sizeof(float));
90   }
91
92   drft_init(&e->drft,window);
93   e->window=_ogg_malloc(e->winlength*sizeof(float));
94   /* We just use a straight sin(x) window for this */
95   for(i=0;i<e->winlength;i++)
96     e->window[i]=sin((i+.5)/e->winlength*M_PI);
97 }
98
99 void _ve_envelope_clear(envelope_lookup *e){
100   int i;
101   for(i=0;i<e->ch;i++){
102     IIR_clear((e->iir+i));
103     free(e->filtered[i]);
104   }
105   drft_clear(&e->drft);
106   free(e->window);
107   free(e->filtered);
108   memset(e,0,sizeof(envelope_lookup));
109 }
110
111 static float _ve_deltai(envelope_lookup *ve,IIR_state *iir,
112                       float *pre,float *post){
113   long n2=ve->winlength*2;
114   long n=ve->winlength;
115
116   float *workA=alloca(sizeof(float)*n2),A=0.;
117   float *workB=alloca(sizeof(float)*n2),B=0.;
118   long i;
119
120   /*_analysis_output("A",granulepos,pre,n,0,0);
121     _analysis_output("B",granulepos,post,n,0,0);*/
122
123   for(i=0;i<n;i++){
124     workA[i]=pre[i]*ve->window[i];
125     workB[i]=post[i]*ve->window[i];
126   }
127
128   /*_analysis_output("Awin",granulepos,workA,n,0,0);
129     _analysis_output("Bwin",granulepos,workB,n,0,0);*/
130
131   drft_forward(&ve->drft,workA);
132   drft_forward(&ve->drft,workB);
133
134   /* we want to have a 'minimum bar' for energy, else we're just
135      basing blocks on quantization noise that outweighs the signal
136      itself (for low power signals) */
137   {
138     float min=ve->minenergy;
139     for(i=0;i<n;i++){
140       if(fabs(workA[i])<min)workA[i]=min;
141       if(fabs(workB[i])<min)workB[i]=min;
142     }
143   }
144
145   /*_analysis_output("Afft",granulepos,workA,n,0,0);
146     _analysis_output("Bfft",granulepos,workB,n,0,0);*/
147
148   for(i=0;i<n;i++){
149     A+=workA[i]*workA[i];
150     B+=workB[i]*workB[i];
151   }
152
153   A=todB(A);
154   B=todB(B);
155
156   return(B-A);
157 }
158
159 long _ve_envelope_search(vorbis_dsp_state *v,long searchpoint){
160   vorbis_info *vi=v->vi;
161   codec_setup_info *ci=vi->codec_setup;
162   envelope_lookup *ve=((backend_lookup_state *)(v->backend_state))->ve;
163   long i,j;
164   
165   /* make sure we have enough storage to match the PCM */
166   if(v->pcm_storage>ve->storage){
167     ve->storage=v->pcm_storage;
168     for(i=0;i<ve->ch;i++)
169       ve->filtered[i]=_ogg_realloc(ve->filtered[i],ve->storage*sizeof(float));
170   }
171
172   /* catch up the highpass to match the pcm */
173   for(i=0;i<ve->ch;i++){
174     float *filtered=ve->filtered[i];
175     float *pcm=v->pcm[i];
176     IIR_state *iir=ve->iir+i;
177     int flag=1;
178     
179     for(j=ve->current;j<v->pcm_current;j++){
180       filtered[j]=IIR_filter(iir,pcm[j]);
181       if(pcm[j])flag=0;
182     }
183     if(flag && ve->current+64<v->pcm_current)IIR_reset(iir);
184   }
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+ci->blocksizes[1]/4-ci->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       float *filtered=ve->filtered[i]+j;
198       IIR_state *iir=ve->iir+i;
199       float m=_ve_deltai(ve,iir,filtered-ve->winlength,filtered);
200       
201       if(m>ci->preecho_thresh){
202         /*granulepos++;*/
203         return(0);
204       }
205       /*granulepos++;*/
206     }
207     
208     j+=ci->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(float));
220   e->current-=shift;
221 }
222
223