1 /********************************************************************
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. *
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/ *
12 ********************************************************************
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 $
19 ********************************************************************/
26 #include "vorbis/codec.h"
27 #include "codec_internal.h"
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) */
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 */
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[]={
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[]={
75 void _ve_envelope_init(envelope_lookup *e,vorbis_info *vi){
76 codec_setup_info *ci=vi->codec_setup;
78 int window=ci->envelopesa;
81 e->minenergy=fromdB(ci->preecho_minenergy);
82 e->iir=_ogg_calloc(ch,sizeof(IIR_state));
83 e->filtered=_ogg_calloc(ch,sizeof(float *));
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));
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);
99 void _ve_envelope_clear(envelope_lookup *e){
101 for(i=0;i<e->ch;i++){
102 IIR_clear((e->iir+i));
103 free(e->filtered[i]);
105 drft_clear(&e->drft);
108 memset(e,0,sizeof(envelope_lookup));
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;
116 float *workA=alloca(sizeof(float)*n2),A=0.;
117 float *workB=alloca(sizeof(float)*n2),B=0.;
120 /*_analysis_output("A",granulepos,pre,n,0,0);
121 _analysis_output("B",granulepos,post,n,0,0);*/
124 workA[i]=pre[i]*ve->window[i];
125 workB[i]=post[i]*ve->window[i];
128 /*_analysis_output("Awin",granulepos,workA,n,0,0);
129 _analysis_output("Bwin",granulepos,workB,n,0,0);*/
131 drft_forward(&ve->drft,workA);
132 drft_forward(&ve->drft,workB);
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) */
138 float min=ve->minenergy;
140 if(fabs(workA[i])<min)workA[i]=min;
141 if(fabs(workB[i])<min)workB[i]=min;
145 /*_analysis_output("Afft",granulepos,workA,n,0,0);
146 _analysis_output("Bfft",granulepos,workB,n,0,0);*/
149 A+=workA[i]*workA[i];
150 B+=workB[i]*workB[i];
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;
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));
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;
179 for(j=ve->current;j<v->pcm_current;j++){
180 filtered[j]=IIR_filter(iir,pcm[j]);
183 if(flag && ve->current+64<v->pcm_current)IIR_reset(iir);
186 ve->current=v->pcm_current;
188 /* Now search through our cached highpass data for breaking points */
191 j=v->centerW+ci->blocksizes[1]/4-ci->blocksizes[0]/4;
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);
201 if(m>ci->preecho_thresh){
208 j+=ci->blocksizes[0]/2;
209 if(j>=searchpoint)return(1);
215 void _ve_envelope_shift(envelope_lookup *e,long shift){
218 memmove(e->filtered[i],e->filtered[i]+shift,(e->current-shift)*