Repaired 'I must have been boozing' memory management in vorbisfile.a
[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.16 2000/03/10 13:21:18 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 "vorbis/codec.h"
26
27 #include "os.h"
28 #include "mdct.h"
29 #include "envelope.h"
30 #include "bitwise.h"
31 #include "window.h"
32 #include "misc.h"
33
34 void _ve_envelope_init(envelope_lookup *e,int samples_per){
35   int i;
36
37   e->winlen=samples_per;
38   e->window=malloc(e->winlen*sizeof(double));
39   mdct_init(&e->mdct,e->winlen);
40
41   /* We just use a straight sin(x) window for this */
42   for(i=0;i<e->winlen;i++)
43     e->window[i]=sin((i+.5)/e->winlen*M_PI);
44
45 }
46
47 void _ve_envelope_clear(envelope_lookup *e){
48   if(e->window)free(e->window);
49   mdct_clear(&e->mdct);
50   memset(e,0,sizeof(envelope_lookup));
51 }
52
53 /* use MDCT for spectral power estimation */
54
55 static void _ve_deltas(double *deltas,double *pcm,int n,double *window,
56                        int winsize,mdct_lookup *m){
57   int i,j;
58   double *out=alloca(sizeof(double)*winsize);
59   
60   for(j=0;j<n;j++){
61     double acc=0.;
62  
63     memcpy(out,pcm+j*winsize,winsize*sizeof(double));
64     for(i=0;i<winsize;i++)
65       out[i]*=window[i];
66
67     mdct_forward(m,out,out);
68
69     for(i=winsize/10;i<winsize/2;i++)
70       acc+=fabs(out[i]);
71     if(deltas[j]<acc)deltas[j]=acc;
72   }
73 }
74
75 void _ve_envelope_deltas(vorbis_dsp_state *v){
76   vorbis_info *vi=v->vi;
77   int step=vi->envelopesa;
78   
79   int dtotal=v->pcm_current/vi->envelopesa;
80   int dcurr=v->envelope_current;
81   int pch;
82   
83   if(dtotal>dcurr){
84     double *mult=v->multipliers+dcurr;
85     memset(mult,0,sizeof(double)*(dtotal-dcurr));
86       
87     for(pch=0;pch<vi->channels;pch++){
88       double *pcm=v->pcm[pch]+dcurr*step;
89       _ve_deltas(mult,pcm,dtotal-dcurr,v->ve.window,v->ve.winlen,&v->ve.mdct);
90     }
91     v->envelope_current=dtotal;
92
93 #ifdef ANALYSIS
94     {
95       static int frameno=0;
96       FILE *out;
97       char buffer[80];
98       int j,k;
99       
100       sprintf(buffer,"delt%d.m",frameno);
101       out=fopen(buffer,"w+");
102       for(j=0;j<v->envelope_current;j++)
103         fprintf(out,"%d %g\n",j*vi->envelopesa+vi->envelopesa/2,v->multipliers[j]);
104       fclose(out);
105
106       sprintf(buffer,"deltpcm%d.m",frameno++);
107       out=fopen(buffer,"w+");
108       for(k=0;k<vi->channels;k++){
109         for(j=0;j<v->pcm_current;j++)
110           fprintf(out,"%d %g\n",j,v->pcm[k][j]);
111         fprintf(out,"\n");
112       }
113       fclose(out);
114     }
115 #endif
116
117   }
118 }
119
120
121
122
123
124