vrshoot

annotate libs/vorbis/envelope.c @ 0:b2f14e535253

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