envelope.c 10 KB

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