envelope.c 10 KB

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