zenilib: zenilib/jni/external/libvorbis/lib/envelope.c Source File

zenilib 0.5.3.0

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
envelope.c
Go to the documentation of this file.
1 /********************************************************************
2 * *
3 * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
4 * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
5 * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
6 * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
7 * *
8 * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2009 *
9 * by the Xiph.Org Foundation http://www.xiph.org/ *
10 * *
11 ********************************************************************
12
13 function: PCM data envelope analysis
14 last mod: $Id: envelope.c 16227 2009-07-08 06:58:46Z xiphmont $
15
16 ********************************************************************/
17
18 #include <stdlib.h>
19 #include <string.h>
20 #include <stdio.h>
21 #include <math.h>
22 #include <ogg/ogg.h>
23 #include "vorbis/codec.h"
24 #include "codec_internal.h"
25
26 #include "os.h"
27 #include "scales.h"
28 #include "envelope.h"
29 #include "mdct.h"
30 #include "misc.h"
31
32 void _ve_envelope_init(envelope_lookup *e,vorbis_info *vi){
33 codec_setup_info *ci=vi->codec_setup;
34 vorbis_info_psy_global *gi=&ci->psy_g_param;
35 int ch=vi->channels;
36 int i,j;
37 int n=e->winlength=128;
38 e->searchstep=64; /* not random */
39
40 e->minenergy=gi->preecho_minenergy;
41 e->ch=ch;
42 e->storage=128;
43 e->cursor=ci->blocksizes[1]/2;
44 e->mdct_win=_ogg_calloc(n,sizeof(*e->mdct_win));
45 mdct_init(&e->mdct,n);
46
47 for(i=0;i<n;i++){
48 e->mdct_win[i]=sin(i/(n-1.)*M_PI);
49 e->mdct_win[i]*=e->mdct_win[i];
50 }
51
52 /* magic follows */
53 e->band[0].begin=2; e->band[0].end=4;
54 e->band[1].begin=4; e->band[1].end=5;
55 e->band[2].begin=6; e->band[2].end=6;
56 e->band[3].begin=9; e->band[3].end=8;
57 e->band[4].begin=13; e->band[4].end=8;
58 e->band[5].begin=17; e->band[5].end=8;
59 e->band[6].begin=22; e->band[6].end=8;
60
61 for(j=0;j<VE_BANDS;j++){
62 n=e->band[j].end;
63 e->band[j].window=_ogg_malloc(n*sizeof(*e->band[0].window));
64 for(i=0;i<n;i++){
65 e->band[j].window[i]=sin((i+.5)/n*M_PI);
66 e->band[j].total+=e->band[j].window[i];
67 }
68 e->band[j].total=1./e->band[j].total;
69 }
70
71 e->filter=_ogg_calloc(VE_BANDS*ch,sizeof(*e->filter));
72 e->mark=_ogg_calloc(e->storage,sizeof(*e->mark));
73
74 }
75
76 void _ve_envelope_clear(envelope_lookup *e){
77 int i;
78 mdct_clear(&e->mdct);
79 for(i=0;i<VE_BANDS;i++)
80 _ogg_free(e->band[i].window);
81 _ogg_free(e->mdct_win);
82 _ogg_free(e->filter);
83 _ogg_free(e->mark);
84 memset(e,0,sizeof(*e));
85 }
86
87 /* fairly straight threshhold-by-band based until we find something
88 that works better and isn't patented. */
89
90 static int _ve_amp(envelope_lookup *ve,
91 vorbis_info_psy_global *gi,
92 float *data,
93 envelope_band *bands,
94 envelope_filter_state *filters){
95 long n=ve->winlength;
96 int ret=0;
97 long i,j;
98 float decay;
99
100 /* we want to have a 'minimum bar' for energy, else we're just
101 basing blocks on quantization noise that outweighs the signal
102 itself (for low power signals) */
103
104 float minV=ve->minenergy;
105 float *vec=alloca(n*sizeof(*vec));
106
107 /* stretch is used to gradually lengthen the number of windows
108 considered prevoius-to-potential-trigger */
109 int stretch=max(VE_MINSTRETCH,ve->stretch/2);
110 float penalty=gi->stretch_penalty-(ve->stretch/2-VE_MINSTRETCH);
111 if(penalty<0.f)penalty=0.f;
112 if(penalty>gi->stretch_penalty)penalty=gi->stretch_penalty;
113
114 /*_analysis_output_always("lpcm",seq2,data,n,0,0,
115 totalshift+pos*ve->searchstep);*/
116
117 /* window and transform */
118 for(i=0;i<n;i++)
119 vec[i]=data[i]*ve->mdct_win[i];
120 mdct_forward(&ve->mdct,vec,vec);
121
122 /*_analysis_output_always("mdct",seq2,vec,n/2,0,1,0); */
123
124 /* near-DC spreading function; this has nothing to do with
125 psychoacoustics, just sidelobe leakage and window size */
126 {
127 float temp=vec[0]*vec[0]+.7*vec[1]*vec[1]+.2*vec[2]*vec[2];
128 int ptr=filters->nearptr;
129
130 /* the accumulation is regularly refreshed from scratch to avoid
131 floating point creep */
132 if(ptr==0){
133 decay=filters->nearDC_acc=filters->nearDC_partialacc+temp;
134 filters->nearDC_partialacc=temp;
135 }else{
136 decay=filters->nearDC_acc+=temp;
137 filters->nearDC_partialacc+=temp;
138 }
139 filters->nearDC_acc-=filters->nearDC[ptr];
140 filters->nearDC[ptr]=temp;
141
142 decay*=(1./(VE_NEARDC+1));
143 filters->nearptr++;
144 if(filters->nearptr>=VE_NEARDC)filters->nearptr=0;
145 decay=todB(&decay)*.5-15.f;
146 }
147
148 /* perform spreading and limiting, also smooth the spectrum. yes,
149 the MDCT results in all real coefficients, but it still *behaves*
150 like real/imaginary pairs */
151 for(i=0;i<n/2;i+=2){
152 float val=vec[i]*vec[i]+vec[i+1]*vec[i+1];
153 val=todB(&val)*.5f;
154 if(val<decay)val=decay;
155 if(val<minV)val=minV;
156 vec[i>>1]=val;
157 decay-=8.;
158 }
159
160 /*_analysis_output_always("spread",seq2++,vec,n/4,0,0,0);*/
161
162 /* perform preecho/postecho triggering by band */
163 for(j=0;j<VE_BANDS;j++){
164 float acc=0.;
165 float valmax,valmin;
166
167 /* accumulate amplitude */
168 for(i=0;i<bands[j].end;i++)
169 acc+=vec[i+bands[j].begin]*bands[j].window[i];
170
171 acc*=bands[j].total;
172
173 /* convert amplitude to delta */
174 {
175 int p,this=filters[j].ampptr;
176 float postmax,postmin,premax=-99999.f,premin=99999.f;
177
178 p=this;
179 p--;
180 if(p<0)p+=VE_AMP;
181 postmax=max(acc,filters[j].ampbuf[p]);
182 postmin=min(acc,filters[j].ampbuf[p]);
183
184 for(i=0;i<stretch;i++){
185 p--;
186 if(p<0)p+=VE_AMP;
187 premax=max(premax,filters[j].ampbuf[p]);
188 premin=min(premin,filters[j].ampbuf[p]);
189 }
190
191 valmin=postmin-premin;
192 valmax=postmax-premax;
193
194 /*filters[j].markers[pos]=valmax;*/
195 filters[j].ampbuf[this]=acc;
196 filters[j].ampptr++;
197 if(filters[j].ampptr>=VE_AMP)filters[j].ampptr=0;
198 }
199
200 /* look at min/max, decide trigger */
201 if(valmax>gi->preecho_thresh[j]+penalty){
202 ret|=1;
203 ret|=4;
204 }
205 if(valmin<gi->postecho_thresh[j]-penalty)ret|=2;
206 }
207
208 return(ret);
209 }
210
211 #if 0
212 static int seq=0;
213 static ogg_int64_t totalshift=-1024;
214 #endif
215
216 long _ve_envelope_search(vorbis_dsp_state *v){
217 vorbis_info *vi=v->vi;
218 codec_setup_info *ci=vi->codec_setup;
219 vorbis_info_psy_global *gi=&ci->psy_g_param;
220 envelope_lookup *ve=((private_state *)(v->backend_state))->ve;
221 long i,j;
222
223 int first=ve->current/ve->searchstep;
224 int last=v->pcm_current/ve->searchstep-VE_WIN;
225 if(first<0)first=0;
226
227 /* make sure we have enough storage to match the PCM */
228 if(last+VE_WIN+VE_POST>ve->storage){
229 ve->storage=last+VE_WIN+VE_POST; /* be sure */
230 ve->mark=_ogg_realloc(ve->mark,ve->storage*sizeof(*ve->mark));
231 }
232
233 for(j=first;j<last;j++){
234 int ret=0;
235
236 ve->stretch++;
237 if(ve->stretch>VE_MAXSTRETCH*2)
238 ve->stretch=VE_MAXSTRETCH*2;
239
240 for(i=0;i<ve->ch;i++){
241 float *pcm=v->pcm[i]+ve->searchstep*(j);
242 ret|=_ve_amp(ve,gi,pcm,ve->band,ve->filter+i*VE_BANDS);
243 }
244
245 ve->mark[j+VE_POST]=0;
246 if(ret&1){
247 ve->mark[j]=1;
248 ve->mark[j+1]=1;
249 }
250
251 if(ret&2){
252 ve->mark[j]=1;
253 if(j>0)ve->mark[j-1]=1;
254 }
255
256 if(ret&4)ve->stretch=-1;
257 }
258
259 ve->current=last*ve->searchstep;
260
261 {
262 long centerW=v->centerW;
263 long testW=
264 centerW+
265 ci->blocksizes[v->W]/4+
266 ci->blocksizes[1]/2+
267 ci->blocksizes[0]/4;
268
269 j=ve->cursor;
270
271 while(j<ve->current-(ve->searchstep)){/* account for postecho
272 working back one window */
273 if(j>=testW)return(1);
274
275 ve->cursor=j;
276
277 if(ve->mark[j/ve->searchstep]){
278 if(j>centerW){
279
280 #if 0
281 if(j>ve->curmark){
282 float *marker=alloca(v->pcm_current*sizeof(*marker));
283 int l,m;
284 memset(marker,0,sizeof(*marker)*v->pcm_current);
285 fprintf(stderr,"mark! seq=%d, cursor:%fs time:%fs\n",
286 seq,
287 (totalshift+ve->cursor)/44100.,
288 (totalshift+j)/44100.);
289 _analysis_output_always("pcmL",seq,v->pcm[0],v->pcm_current,0,0,totalshift);
290 _analysis_output_always("pcmR",seq,v->pcm[1],v->pcm_current,0,0,totalshift);
291
292 _analysis_output_always("markL",seq,v->pcm[0],j,0,0,totalshift);
293 _analysis_output_always("markR",seq,v->pcm[1],j,0,0,totalshift);
294
295 for(m=0;m<VE_BANDS;m++){
296 char buf[80];
297 sprintf(buf,"delL%d",m);
298 for(l=0;l<last;l++)marker[l*ve->searchstep]=ve->filter[m].markers[l]*.1;
299 _analysis_output_always(buf,seq,marker,v->pcm_current,0,0,totalshift);
300 }
301
302 for(m=0;m<VE_BANDS;m++){
303 char buf[80];
304 sprintf(buf,"delR%d",m);
305 for(l=0;l<last;l++)marker[l*ve->searchstep]=ve->filter[m+VE_BANDS].markers[l]*.1;
306 _analysis_output_always(buf,seq,marker,v->pcm_current,0,0,totalshift);
307 }
308
309 for(l=0;l<last;l++)marker[l*ve->searchstep]=ve->mark[l]*.4;
310 _analysis_output_always("mark",seq,marker,v->pcm_current,0,0,totalshift);
311
312
313 seq++;
314
315 }
316 #endif
317
318 ve->curmark=j;
319 if(j>=testW)return(1);
320 return(0);
321 }
322 }
323 j+=ve->searchstep;
324 }
325 }
326
327 return(-1);
328 }
329
330 int _ve_envelope_mark(vorbis_dsp_state *v){
331 envelope_lookup *ve=((private_state *)(v->backend_state))->ve;
332 vorbis_info *vi=v->vi;
333 codec_setup_info *ci=vi->codec_setup;
334 long centerW=v->centerW;
335 long beginW=centerW-ci->blocksizes[v->W]/4;
336 long endW=centerW+ci->blocksizes[v->W]/4;
337 if(v->W){
338 beginW-=ci->blocksizes[v->lW]/4;
339 endW+=ci->blocksizes[v->nW]/4;
340 }else{
341 beginW-=ci->blocksizes[0]/4;
342 endW+=ci->blocksizes[0]/4;
343 }
344
345 if(ve->curmark>=beginW && ve->curmark<endW)return(1);
346 {
347 long first=beginW/ve->searchstep;
348 long last=endW/ve->searchstep;
349 long i;
350 for(i=first;i<last;i++)
351 if(ve->mark[i])return(1);
352 }
353 return(0);
354 }
355
356 void _ve_envelope_shift(envelope_lookup *e,long shift){
357 int smallsize=e->current/e->searchstep+VE_POST; /* adjust for placing marks
358 ahead of ve->current */
359 int smallshift=shift/e->searchstep;
360
361 memmove(e->mark,e->mark+smallshift,(smallsize-smallshift)*sizeof(*e->mark));
362
363 #if 0
364 for(i=0;i<VE_BANDS*e->ch;i++)
365 memmove(e->filter[i].markers,
366 e->filter[i].markers+smallshift,
367 (1024-smallshift)*sizeof(*(*e->filter).markers));
368 totalshift+=shift;
369 #endif
370
371 e->current-=shift;
372 if(e->curmark>=0)
373 e->curmark-=shift;
374 e->cursor-=shift;
375 }
mdct.h
vorbis_dsp_state::pcm
float ** pcm
Definition: codec.h:63
val
GLuint const GLfloat * val
Definition: glew.h:2715
VE_NEARDC
#define VE_NEARDC
Definition: envelope.h:29
vorbis_dsp_state::lW
long lW
Definition: codec.h:72
vorbis_dsp_state::pcm_current
int pcm_current
Definition: codec.h:66
misc.h
VE_BANDS
#define VE_BANDS
Definition: envelope.h:28
envelope_filter_state::nearDC_acc
float nearDC_acc
Definition: envelope.h:39
marker
const GLchar * marker
Definition: gl2ext.h:1092
envelope_filter_state::nearDC_partialacc
float nearDC_partialacc
Definition: envelope.h:40
f
GLclampf f
Definition: glew.h:3390
envelope_band::begin
int begin
Definition: envelope.h:46
_ve_envelope_shift
void _ve_envelope_shift(envelope_lookup *e, long shift)
Definition: envelope.c:356
n
GLclampd n
Definition: glew.h:7287
memmove
#define memmove
Definition: SDL_qsort.c:81
e
return Display return Display Bool Bool int int e
Definition: SDL_x11sym.h:30
mdct_init
void mdct_init(mdct_lookup *lookup, int n)
Definition: mdct.c:52
mdct_forward
void mdct_forward(mdct_lookup *init, DATA_TYPE *in, DATA_TYPE *out)
Definition: mdct.c:493
j
int32_t j
Definition: e_log.c:102
memset
#define memset
Definition: SDL_malloc.c:633
_ogg_free
#define _ogg_free
Definition: os_types.h:25
codec.h
codec_internal.h
envelope_filter_state
Definition: envelope.h:34
envelope_band::end
int end
Definition: envelope.h:47
_ve_envelope_mark
int _ve_envelope_mark(vorbis_dsp_state *v)
Definition: envelope.c:330
_ve_envelope_clear
void _ve_envelope_clear(envelope_lookup *e)
Definition: envelope.c:76
_ve_envelope_init
void _ve_envelope_init(envelope_lookup *e, vorbis_info *vi)
Definition: envelope.c:32
vorbis_dsp_state::nW
long nW
Definition: codec.h:74
ogg.h
envelope_lookup::storage
long storage
Definition: envelope.h:67
ret
ret
Definition: glew_str_glx.c:2
vorbis_dsp_state::vi
vorbis_info * vi
Definition: codec.h:61
_ve_amp
static int _ve_amp(envelope_lookup *ve, vorbis_info_psy_global *gi, float *data, envelope_band *bands, envelope_filter_state *filters)
Definition: envelope.c:90
vorbis_info::codec_setup
void * codec_setup
Definition: codec.h:53
vorbis_info::channels
int channels
Definition: codec.h:30
codec_setup_info::psy_g_param
vorbis_info_psy_global psy_g_param
Definition: codec_internal.h:124
envelope_band::total
float total
Definition: envelope.h:49
first
GLint first
Definition: gl2ext.h:1011
v
const GLdouble * v
Definition: glew.h:1377
envelope_lookup::filter
envelope_filter_state * filter
Definition: envelope.h:62
todB
static float todB(const float *x)
Definition: scales.h:44
VE_AMP
#define VE_AMP
Definition: envelope.h:26
envelope_lookup::curmark
long curmark
Definition: envelope.h:69
data
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl2ext.h:848
envelope_lookup::current
long current
Definition: envelope.h:68
VE_MINSTRETCH
#define VE_MINSTRETCH
Definition: envelope.h:31
scales.h
codec_setup_info::blocksizes
long blocksizes[2]
Definition: codec_internal.h:99
p
GLfloat GLfloat p
Definition: glew.h:14938
vorbis_dsp_state::centerW
long centerW
Definition: codec.h:75
os.h
envelope_lookup::mdct
mdct_lookup mdct
Definition: envelope.h:58
VE_MAXSTRETCH
#define VE_MAXSTRETCH
Definition: envelope.h:32
envelope.h
envelope_lookup::winlength
int winlength
Definition: envelope.h:54
vorbis_info_psy_global
Definition: psy.h:66
envelope_filter_state::ampbuf
float ampbuf[VE_AMP]
Definition: envelope.h:35
vec
FT_Vector * vec
Definition: ftbbox.c:579
_ogg_realloc
#define _ogg_realloc
Definition: os_types.h:24
envelope_lookup
Definition: envelope.h:52
M_PI
#define M_PI
Definition: os.h:45
sin
double sin(double x)
Definition: s_sin.c:56
envelope_filter_state::nearptr
int nearptr
Definition: envelope.h:41
envelope_lookup::cursor
long cursor
Definition: envelope.h:70
vorbis_info_psy_global::preecho_thresh
float preecho_thresh[VE_BANDS]
Definition: psy.h:70
l
GLdouble l
Definition: glew.h:8383
vi
XVisualInfo * vi
Definition: glewinfo_tail.c:221
envelope_lookup::mdct_win
float * mdct_win
Definition: envelope.h:59
buf
GLenum GLuint GLsizei const GLchar * buf
Definition: glew.h:2539
envelope_lookup::band
envelope_band band[VE_BANDS]
Definition: envelope.h:61
envelope_lookup::minenergy
float minenergy
Definition: envelope.h:56
vorbis_dsp_state::W
long W
Definition: codec.h:73
_ogg_calloc
#define _ogg_calloc
Definition: os_types.h:23
envelope_lookup::searchstep
int searchstep
Definition: envelope.h:55
codec_setup_info
Definition: codec_internal.h:94
String.h
VE_WIN
#define VE_WIN
Definition: envelope.h:24
mdct_clear
void mdct_clear(mdct_lookup *l)
Definition: mdct.c:339
_ve_envelope_search
long _ve_envelope_search(vorbis_dsp_state *v)
Definition: envelope.c:216
envelope_band::window
float * window
Definition: envelope.h:48
envelope_lookup::stretch
int stretch
Definition: envelope.h:63
min
#define min(x, y)
Definition: os.h:75
envelope_band
Definition: envelope.h:45
vorbis_info_psy_global::preecho_minenergy
float preecho_minenergy
Definition: psy.h:73
i
int i
Definition: pngrutil.c:1377
max
#define max(x, y)
Definition: os.h:79
envelope_lookup::mark
int * mark
Definition: envelope.h:65
private_state
Definition: codec_internal.h:61
envelope_lookup::ch
int ch
Definition: envelope.h:53
m
#define m(i, j)
VE_POST
#define VE_POST
Definition: envelope.h:25
envelope_filter_state::nearDC
float nearDC[VE_NEARDC]
Definition: envelope.h:38
ogg_int64_t
long ogg_int64_t
Definition: config_types.h:23
envelope_filter_state::ampptr
int ampptr
Definition: envelope.h:36
vorbis_info_psy_global::stretch_penalty
float stretch_penalty
Definition: psy.h:72
vorbis_dsp_state::backend_state
void * backend_state
Definition: codec.h:85
vorbis_info
Definition: codec.h:28
_ogg_malloc
#define _ogg_malloc
Definition: os_types.h:22
vorbis_dsp_state
Definition: codec.h:59