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