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