dec162cd530e2a76a9970bc17df1ffc493526da2
[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.23 2000/10/12 03:12:52 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
28 #include "os.h"
29 #include "scales.h"
30 #include "envelope.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 #if 0
42 static int    cheb_bandpass_stages=10;
43 static float cheb_bandpass_gain=5.589612458e+01;
44 static float cheb_bandpass_B[]={-1.,0.,5.,0.,-10.,0.,10.,0.,-5.,0.,1};
45 static float 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 #endif 
57
58 static int    cheb_highpass_stages=10;
59 static float cheb_highpass_gain= 5.291963434e+01;
60 /* z^-stage, z^-stage+1... */
61 static float cheb_highpass_B[]={1,-10,45,-120,210,-252,210,-120,45,-10,1};
62 static float cheb_highpass_A[]={
63   -0.1247628029,
64   0.1334086523,
65   -0.3997715614,
66   0.3213011089,
67   -1.1131924119,
68   1.7692446626,
69   -3.6241199038,
70   4.1950871291,
71   -4.2771757867,
72   2.3920318913};
73
74 void _ve_envelope_init(envelope_lookup *e,vorbis_info *vi){
75   int ch=vi->channels;
76   int window=vi->envelopesa;
77   int i;
78   e->winlength=window;
79   e->minenergy=fromdB(vi->preecho_minenergy);
80   e->iir=calloc(ch,sizeof(IIR_state));
81   e->filtered=calloc(ch,sizeof(float *));
82   e->ch=ch;
83   e->storage=128;
84   for(i=0;i<ch;i++){
85     IIR_init(e->iir+i,cheb_highpass_stages,cheb_highpass_gain,
86              cheb_highpass_A,cheb_highpass_B);
87     e->filtered[i]=calloc(e->storage,sizeof(float));
88   }
89
90   drft_init(&e->drft,window);
91   e->window=malloc(e->winlength*sizeof(float));
92   /* We just use a straight sin(x) window for this */
93   for(i=0;i<e->winlength;i++)
94     e->window[i]=sin((i+.5)/e->winlength*M_PI);
95 }
96
97 void _ve_envelope_clear(envelope_lookup *e){
98   int i;
99   for(i=0;i<e->ch;i++){
100     IIR_clear((e->iir+i));
101     free(e->filtered[i]);
102   }
103   drft_clear(&e->drft);
104   free(e->window);
105   free(e->filtered);
106   memset(e,0,sizeof(envelope_lookup));
107 }
108
109 static float _ve_deltai(envelope_lookup *ve,IIR_state *iir,
110                       float *pre,float *post){
111   long n2=ve->winlength*2;
112   long n=ve->winlength;
113
114   float *workA=alloca(sizeof(float)*n2),A=0.;
115   float *workB=alloca(sizeof(float)*n2),B=0.;
116   long i;
117
118   /*_analysis_output("A",granulepos,pre,n,0,0);
119     _analysis_output("B",granulepos,post,n,0,0);*/
120
121   for(i=0;i<n;i++){
122     workA[i]=pre[i]*ve->window[i];
123     workB[i]=post[i]*ve->window[i];
124   }
125
126   /*_analysis_output("Awin",granulepos,workA,n,0,0);
127     _analysis_output("Bwin",granulepos,workB,n,0,0);*/
128
129   drft_forward(&ve->drft,workA);
130   drft_forward(&ve->drft,workB);
131
132   /* we want to have a 'minimum bar' for energy, else we're just
133      basing blocks on quantization noise that outweighs the signal
134      itself (for low power signals) */
135   {
136     float min=ve->minenergy;
137     for(i=0;i<n;i++){
138       if(fabs(workA[i])<min)workA[i]=min;
139       if(fabs(workB[i])<min)workB[i]=min;
140     }
141   }
142
143   /*_analysis_output("Afft",granulepos,workA,n,0,0);
144     _analysis_output("Bfft",granulepos,workB,n,0,0);*/
145
146   for(i=0;i<n;i++){
147     A+=workA[i]*workA[i];
148     B+=workB[i]*workB[i];
149   }
150
151   A=todB(A);
152   B=todB(B);
153
154   return(B-A);
155 }
156
157 long _ve_envelope_search(vorbis_dsp_state *v,long searchpoint){
158   vorbis_info *vi=v->vi;
159   envelope_lookup *ve=v->ve;
160   long i,j;
161   
162   /* make sure we have enough storage to match the PCM */
163   if(v->pcm_storage>ve->storage){
164     ve->storage=v->pcm_storage;
165     for(i=0;i<ve->ch;i++)
166       ve->filtered[i]=realloc(ve->filtered[i],ve->storage*sizeof(float));
167   }
168
169   /* catch up the highpass to match the pcm */
170   for(i=0;i<ve->ch;i++){
171     float *filtered=ve->filtered[i];
172     float *pcm=v->pcm[i];
173     IIR_state *iir=ve->iir+i;
174     
175     for(j=ve->current;j<v->pcm_current;j++)
176       filtered[j]=IIR_filter(iir,pcm[j]);
177   }
178   ve->current=v->pcm_current;
179
180   /* Now search through our cached highpass data for breaking points */
181   /* starting point */
182   if(v->W)
183     j=v->centerW+vi->blocksizes[1]/4-vi->blocksizes[0]/4;
184   else
185     j=v->centerW;
186
187   while(j+ve->winlength<=v->pcm_current){
188     for(i=0;i<ve->ch;i++){
189       float *filtered=ve->filtered[i]+j;
190       IIR_state *iir=ve->iir+i;
191       float m=_ve_deltai(ve,iir,filtered-ve->winlength,filtered);
192       
193       if(m>vi->preecho_thresh){
194         /*granulepos++;*/
195         return(0);
196       }
197       /*granulepos++;*/
198     }
199     
200     j+=vi->blocksizes[0]/2;
201     if(j>=searchpoint)return(1);
202   }
203   
204   return(-1);
205 }
206
207 void _ve_envelope_shift(envelope_lookup *e,long shift){
208   int i;
209   for(i=0;i<e->ch;i++)
210     memmove(e->filtered[i],e->filtered[i]+shift,(e->current-shift)*
211             sizeof(float));
212   e->current-=shift;
213 }
214
215