OK, libvorbis encodes and decodes bitstreams (not complete Vorbis
[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-1999             *
9  * by 1999 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  author: Monty <xiphmont@mit.edu>
16  modifications by: Monty
17  last modification date: Oct 06 1999
18
19  Vorbis manipulates the dynamic range of the incoming PCM data
20  envelope to minimise time-domain energy leakage from percussive and
21  plosive waveforms being quantized in the MDCT domain.
22
23  ********************************************************************/
24
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdio.h>
28 #include <math.h>
29
30 #include "codec.h"
31 #include "envelope.h"
32 #include "bitwise.h"
33
34 void _ve_envelope_init(envelope_lookup *e,int samples_per){
35   int i;
36
37   e->winlen=samples_per*2;
38   e->window=malloc(e->winlen*sizeof(double));
39
40   /* We just use a straight sin^2(x) window for this */
41   for(i=0;i<e->winlen;i++){
42     double temp=sin((i+.5)/e->winlen*M_PI);
43     e->window[i]=temp*temp;
44   }
45 }
46
47 void _ve_envelope_clear(envelope_lookup *e){
48   if(e->window)free(e->window);
49   memset(e,0,sizeof(envelope_lookup));
50 }
51
52 /* initial and final blocks are special cases. Eg:
53    ______
54          `--_            
55    |_______|_`-.___|_______|_______|
56
57               ___                     
58           _--'   `--_     
59    |___.-'_|_______|_`-.___|_______|
60
61                       ___                     
62                   _--'   `--_     
63    |_______|___.-'_|_______|_`-.___|
64
65                                _____
66                            _--'
67    |_______|_______|____.-'|_______|
68  
69    as we go block by block, we watch the collective metrics span. If we 
70    span the threshhold (assuming the threshhold is active), we use an 
71    abbreviated vector */
72
73 static int _ve_envelope_generate(double *mult,double *env,double *look,
74                                  int n,int step){
75   int i,j,p;
76   double m,mo;
77
78   for(j=0;j<n;j++)if(mult[j]!=1)break;
79   if(j==n)return(0);
80
81   n*=step;
82   /* first multiplier special case */
83   m=ldexp(1,mult[0]-1);
84   for(p=0;p<step/2;p++)env[p]=m;
85   
86   /* mid multipliers normal case */
87   for(j=1;p<n-step/2;j++){
88     mo=m;
89     m=ldexp(1,mult[j]-1);
90     if(mo==m)
91       for(i=0;i<step;i++,p++)env[p]=m;
92     else
93       for(i=0;i<step;i++,p++)env[p]=m*look[i]+mo*look[i+step];
94   }
95
96   /* last multiplier special case */
97   for(;p<n;p++)env[p]=m;
98   return(1);
99 }
100
101 /* right now, we do things simple and dirty (read: our current preecho
102    is a joke).  Should this prove inadequate, then we'll think of
103    something different.  The details of the encoding format do not
104    depend on the exact behavior, only the format of the bits that come
105    out.
106
107    Mark Taylor probably has much witter ways of doing this...  Let's
108    see if simple delta analysis gives us acceptible results for now.  */
109
110 static void _ve_deltas(double *deltas,double *pcm,int n,double *win,
111                        int winsize){
112   int i,j,p=winsize/2;
113   for(j=0;j<n;j++){
114     p-=winsize/2;
115     for(i=0;i<winsize-1;i++,p++){
116       double temp=fabs(win[i]*pcm[p]-win[i+1]*pcm[p+1]);
117       if(deltas[j]<temp)deltas[j]=temp;
118     }
119     p++;
120   }
121 }
122
123 void _ve_envelope_multipliers(vorbis_dsp_state *v){
124   vorbis_info *vi=v->vi;
125   int step=vi->envelopesa;
126   static int frame=0;
127
128   /* we need a 1-1/4 envelope window overlap begin and 1/4 end */
129   int dtotal=(v->pcm_current-step/2)/vi->envelopesa;
130   int dcurr=v->envelope_current;
131   double *window=v->ve.window;
132   int winlen=v->ve.winlen;
133   int pch,ech;
134   
135   if(dtotal>dcurr){
136     for(ech=0;ech<vi->envelopech;ech++){
137       double *mult=v->multipliers[ech]+dcurr;
138       memset(mult,0,sizeof(double)*(dtotal-dcurr));
139       
140       for(pch=0;pch<vi->channels;pch++){
141         
142         /* does this channel contribute to the envelope analysis */
143         /*if(vi->envelopemap[pch]==ech){ not mapping yet */
144         if(pch==ech){
145
146           /* we need a 1/4 envelope window overlap front and back */
147           double *pcm=v->pcm[pch]+dcurr*step-step/2;
148           _ve_deltas(mult,pcm,dtotal-dcurr,window,winlen);
149
150         }
151       }
152     }
153     v->envelope_current=dtotal;
154     frame++;
155   }
156 }
157
158 /* This readies the multiplier vector for use/coding.  Clamp/adjust
159    the multipliers to the allowed range and eliminate unneeded
160    coefficients */
161
162 void _ve_envelope_sparsify(vorbis_block *vb){
163   vorbis_info *vi=vb->vd->vi;
164   int ch;
165   for(ch=0;ch<vi->envelopech;ch++){
166     int flag=0;
167     double *mult=vb->mult[ch];
168     int n=vb->multend;
169     double first=mult[0];
170     double last=first;
171     double clamp;
172     int i;
173
174     /* are we going to multiply anything? */
175     
176     for(i=1;i<n;i++){
177       if(mult[i]>=last*vi->preecho_thresh){
178         flag=1;
179         break;
180       }
181       if(i<n-1 && mult[i+1]>=last*vi->preecho_thresh){
182         flag=1;
183         break;
184       }
185       last=mult[i];
186     }
187     
188     if(flag){
189       /* we need to adjust, so we might as well go nuts */
190       
191       int begin=i;
192       clamp=last?last:1;
193       
194       for(i=0;i<begin;i++)mult[i]=0;
195       
196       last=1;
197       for(;i<n;i++){
198         if(mult[i]/last>clamp*vi->preecho_thresh){
199           last=mult[i]/vi->preecho_clamp;
200           
201           mult[i]=floor(log(mult[i]/clamp/vi->preecho_clamp)/log(2))-1;
202           if(mult[i]>15)mult[i]=15;
203         }else{
204           mult[i]=0;
205         }
206       }  
207     }else
208       memset(mult,0,sizeof(double)*n);
209   }
210 }
211
212 void _ve_envelope_apply(vorbis_block *vb,int multp){
213   vorbis_info *vi=vb->vd->vi;
214   double env[vb->multend*vi->envelopesa];
215   envelope_lookup *look=&vb->vd->ve;
216   int i,j,k;
217   
218   for(i=0;i<vi->envelopech;i++){
219     double *mult=vb->mult[i];
220     double last=1.;
221     
222     /* fill in the multiplier placeholders */
223
224     for(j=0;j<vb->multend;j++){
225       if(mult[j]){
226         last=mult[j];
227       }else
228         mult[j]=last;
229     }
230
231     /* compute the envelope curve */
232     if(_ve_envelope_generate(mult,env,look->window,vb->multend,
233                              vi->envelopesa)){
234       
235       /* apply the envelope curve */
236       for(j=0;j<vi->channels;j++){
237         
238         /* check to see if the generated envelope applies to this channel */
239         /*if(vi->envelopemap[j]==i){ not mapping yet */
240         if(j==i){
241           
242           if(multp)
243             for(k=0;k<vb->multend*vi->envelopesa;k++)
244               vb->pcm[j][k]*=env[k];
245           else
246             for(k=0;k<vb->multend*vi->envelopesa;k++)
247               vb->pcm[j][k]/=env[k];
248         }
249       }
250     }
251   }
252 }
253
254 int _ve_envelope_encode(vorbis_block *vb){
255   /* Not huffcoded yet. */
256
257   vorbis_info *vi=vb->vd->vi;
258   int scale=vb->W;
259   int n=vb->multend;
260   int i,j;
261
262   /* range is 0-15 */
263
264   for(i=0;i<vi->envelopech;i++){
265     double *mult=vb->mult[i];
266     for(j=0;j<n;j++)
267       _oggpack_write(&vb->opb,(int)(mult[j]),4);
268   }
269   return(0);
270 }
271
272 /* synthesis expands the buffers in vb if needed.  We can assume we
273    have enough storage handed to us. */
274 int _ve_envelope_decode(vorbis_block *vb){
275   vorbis_info *vi=vb->vd->vi;
276   int scale=vb->W;
277   int n=vb->multend;
278   int i,j;
279
280   /* range is 0-15 */
281
282   for(i=0;i<vi->envelopech;i++){
283     double *mult=vb->mult[i];
284     for(j=0;j<n;j++)
285       mult[j]=_oggpack_read(&vb->opb,4);
286   }
287   return(0);
288 }
289
290