1 /********************************************************************
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. *
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/ *
12 ********************************************************************
14 function: PCM data envelope analysis and manipulation
15 author: Monty <xiphmont@mit.edu>
16 modifications by: Monty
17 last modification date: Oct 17 1999
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.
23 ********************************************************************/
35 void _ve_envelope_init(envelope_lookup *e,int samples_per){
38 e->winlen=samples_per*2;
39 e->window=malloc(e->winlen*sizeof(double));
41 /* We just use a straight sin^2(x) window for this */
42 for(i=0;i<e->winlen;i++){
43 double temp=sin((i+.5)/e->winlen*M_PI);
44 e->window[i]=temp*temp;
48 void _ve_envelope_clear(envelope_lookup *e){
49 if(e->window)free(e->window);
50 memset(e,0,sizeof(envelope_lookup));
53 /* initial and final blocks are special cases. Eg:
56 |_______|_`-.___|_______|_______|
60 |___.-'_|_______|_`-.___|_______|
64 |_______|___.-'_|_______|_`-.___|
68 |_______|_______|____.-'|_______|
70 as we go block by block, we watch the collective metrics span. If we
71 span the threshhold (assuming the threshhold is active), we use an
74 static int _ve_envelope_generate(double *mult,double *env,double *look,
79 for(j=0;j<n;j++)if(mult[j]!=1)break;
83 /* first multiplier special case */
85 for(p=0;p<step/2;p++)env[p]=m;
87 /* mid multipliers normal case */
88 for(j=1;p<n-step/2;j++){
92 for(i=0;i<step;i++,p++)env[p]=m;
94 for(i=0;i<step;i++,p++)env[p]=m*look[i]+mo*look[i+step];
97 /* last multiplier special case */
98 for(;p<n;p++)env[p]=m;
102 /* use MDCT for spectral power estimation */
104 static void _ve_deltas(double *deltas,double *pcm,int n,double *win,
108 double out[winsize/2];
110 mdct_init(&m,winsize);
115 mdct_forward(&m,pcm+j*winsize/2,out,win);
116 for(i=0;i<winsize/2;i++)
118 if(deltas[j]<acc)deltas[j]=acc;
125 static int frameno=0;
129 sprintf(buffer,"delta%d.m",frameno++);
130 out=fopen(buffer,"w+");
132 fprintf(out,"%g\n",deltas[j]);
138 void _ve_envelope_multipliers(vorbis_dsp_state *v){
139 vorbis_info *vi=v->vi;
140 int step=vi->envelopesa;
143 /* we need a 1-1/4 envelope window overlap begin and 1/4 end */
144 int dtotal=(v->pcm_current-step/2)/vi->envelopesa;
145 int dcurr=v->envelope_current;
146 double *window=v->ve.window;
147 int winlen=v->ve.winlen;
151 for(ech=0;ech<vi->envelopech;ech++){
152 double *mult=v->multipliers[ech]+dcurr;
153 memset(mult,0,sizeof(double)*(dtotal-dcurr));
155 for(pch=0;pch<vi->channels;pch++){
157 /* does this channel contribute to the envelope analysis */
158 /*if(vi->envelopemap[pch]==ech){ not mapping yet */
161 /* we need a 1/4 envelope window overlap front and back */
162 double *pcm=v->pcm[pch]+dcurr*step-step/2;
163 _ve_deltas(mult,pcm,dtotal-dcurr,window,winlen);
168 v->envelope_current=dtotal;
173 /* This readies the multiplier vector for use/coding. Clamp/adjust
174 the multipliers to the allowed range and eliminate unneeded
177 void _ve_envelope_sparsify(vorbis_block *vb){
178 vorbis_info *vi=vb->vd->vi;
180 for(ch=0;ch<vi->envelopech;ch++){
182 double *mult=vb->mult[ch];
184 double first=mult[0];
191 static int frameno=0.;
196 sprintf(buffer,"del%d.m",frameno++);
197 out=fopen(buffer,"w+");
199 fprintf(out,"%g\n",mult[j]);
204 /* are we going to multiply anything? */
207 if(mult[i]>=last*vi->preecho_thresh){
211 if(i<n-1 && mult[i+1]>=last*vi->preecho_thresh){
219 /* we need to adjust, so we might as well go nuts */
224 for(i=0;i<begin;i++)mult[i]=0;
227 if(mult[i]>=last*vi->preecho_thresh){
230 mult[i]=floor(log(mult[i]/clamp)/log(2));
231 if(mult[i]>15)mult[i]=15;
232 if(mult[i]<1)mult[i]=1;
239 memset(mult,0,sizeof(double)*n);
243 static int frameno=0.;
248 sprintf(buffer,"sparse%d.m",frameno++);
249 out=fopen(buffer,"w+");
251 fprintf(out,"%g\n",mult[j]);
260 void _ve_envelope_apply(vorbis_block *vb,int multp){
261 static int frameno=0;
262 vorbis_info *vi=vb->vd->vi;
263 double env[vb->multend*vi->envelopesa];
264 envelope_lookup *look=&vb->vd->ve;
267 for(i=0;i<vi->envelopech;i++){
268 double *mult=vb->mult[i];
271 /* fill in the multiplier placeholders */
273 for(j=0;j<vb->multend;j++){
281 /* compute the envelope curve */
282 if(_ve_envelope_generate(mult,env,look->window,vb->multend,
289 sprintf(buffer,"env%d.m",frameno);
290 out=fopen(buffer,"w+");
291 for(j=0;j<vb->multend*vi->envelopesa;j++)
292 fprintf(out,"%g\n",env[j]);
294 sprintf(buffer,"prepcm%d.m",frameno);
295 out=fopen(buffer,"w+");
296 for(j=0;j<vb->multend*vi->envelopesa;j++)
297 fprintf(out,"%g\n",vb->pcm[i][j]);
302 for(k=0;k<vi->channels;k++){
305 for(j=0;j<vb->multend*vi->envelopesa;j++)
306 vb->pcm[k][j]*=env[j];
308 for(j=0;j<vb->multend*vi->envelopesa;j++)
309 vb->pcm[k][j]/=env[j];
318 sprintf(buffer,"pcm%d.m",frameno);
319 out=fopen(buffer,"w+");
320 for(j=0;j<vb->multend*vi->envelopesa;j++)
321 fprintf(out,"%g\n",vb->pcm[i][j]);
330 int _ve_envelope_encode(vorbis_block *vb){
331 /* Not huffcoded yet. */
333 vorbis_info *vi=vb->vd->vi;
340 for(i=0;i<vi->envelopech;i++){
341 double *mult=vb->mult[i];
343 _oggpack_write(&vb->opb,(int)(mult[j]),4);
349 /* synthesis expands the buffers in vb if needed. We can assume we
350 have enough storage handed to us. */
351 int _ve_envelope_decode(vorbis_block *vb){
352 vorbis_info *vi=vb->vd->vi;
359 for(i=0;i<vi->envelopech;i++){
360 double *mult=vb->mult[i];
362 mult[j]=_oggpack_read(&vb->opb,4);