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