dbf-halloween2015

annotate libs/vorbis/envelope.c @ 4:4316c0c879e9

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