envelope.c 10 KB

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