vorbis_psy.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. /* Copyright (C) 2005 Jean-Marc Valin, CSIRO, Christopher Montgomery
  2. File: vorbis_psy.c
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions
  5. are met:
  6. - Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. - Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. - Neither the name of the Xiph.org Foundation nor the names of its
  12. contributors may be used to endorse or promote products derived from
  13. this software without specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  15. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  16. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  17. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
  18. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  19. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  20. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  21. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  22. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  23. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #ifdef HAVE_CONFIG_H
  27. #include "config.h"
  28. #endif
  29. #ifdef VORBIS_PSYCHO
  30. #include "arch.h"
  31. #include "smallft.h"
  32. #include "lpc.h"
  33. #include "vorbis_psy.h"
  34. #include "os_support.h"
  35. #include <stdlib.h>
  36. #include <math.h>
  37. #include <string.h>
  38. /* psychoacoustic setup ********************************************/
  39. static VorbisPsyInfo example_tuning = {
  40. .5,.5,
  41. 3,3,25,
  42. /*63 125 250 500 1k 2k 4k 8k 16k*/
  43. // vorbis mode 4 style
  44. //{-32,-32,-32,-32,-28,-24,-22,-20,-20, -20, -20, -8, -6, -6, -6, -6, -6},
  45. { -4, -6, -6, -6, -6, -6, -6, -6, -8, -8,-10,-10, -8, -6, -4, -4, -2},
  46. {
  47. 0, 1, 2, 3, 4, 5, 5, 5, /* 7dB */
  48. 6, 6, 6, 5, 4, 4, 4, 4, /* 15dB */
  49. 4, 4, 5, 5, 5, 6, 6, 6, /* 23dB */
  50. 7, 7, 7, 8, 8, 8, 9, 10, /* 31dB */
  51. 11,12,13,14,15,16,17, 18, /* 39dB */
  52. }
  53. };
  54. /* there was no great place to put this.... */
  55. #include <stdio.h>
  56. static void _analysis_output(char *base,int i,float *v,int n,int bark,int dB){
  57. int j;
  58. FILE *of;
  59. char buffer[80];
  60. sprintf(buffer,"%s_%d.m",base,i);
  61. of=fopen(buffer,"w");
  62. if(!of)perror("failed to open data dump file");
  63. for(j=0;j<n;j++){
  64. if(bark){
  65. float b=toBARK((4000.f*j/n)+.25);
  66. fprintf(of,"%f ",b);
  67. }else
  68. fprintf(of,"%f ",(double)j);
  69. if(dB){
  70. float val;
  71. if(v[j]==0.)
  72. val=-140.;
  73. else
  74. val=todB(v[j]);
  75. fprintf(of,"%f\n",val);
  76. }else{
  77. fprintf(of,"%f\n",v[j]);
  78. }
  79. }
  80. fclose(of);
  81. }
  82. static void bark_noise_hybridmp(int n,const long *b,
  83. const float *f,
  84. float *noise,
  85. const float offset,
  86. const int fixed){
  87. float *N=alloca(n*sizeof(*N));
  88. float *X=alloca(n*sizeof(*N));
  89. float *XX=alloca(n*sizeof(*N));
  90. float *Y=alloca(n*sizeof(*N));
  91. float *XY=alloca(n*sizeof(*N));
  92. float tN, tX, tXX, tY, tXY;
  93. int i;
  94. int lo, hi;
  95. float R, A, B, D;
  96. float w, x, y;
  97. tN = tX = tXX = tY = tXY = 0.f;
  98. y = f[0] + offset;
  99. if (y < 1.f) y = 1.f;
  100. w = y * y * .5;
  101. tN += w;
  102. tX += w;
  103. tY += w * y;
  104. N[0] = tN;
  105. X[0] = tX;
  106. XX[0] = tXX;
  107. Y[0] = tY;
  108. XY[0] = tXY;
  109. for (i = 1, x = 1.f; i < n; i++, x += 1.f) {
  110. y = f[i] + offset;
  111. if (y < 1.f) y = 1.f;
  112. w = y * y;
  113. tN += w;
  114. tX += w * x;
  115. tXX += w * x * x;
  116. tY += w * y;
  117. tXY += w * x * y;
  118. N[i] = tN;
  119. X[i] = tX;
  120. XX[i] = tXX;
  121. Y[i] = tY;
  122. XY[i] = tXY;
  123. }
  124. for (i = 0, x = 0.f;; i++, x += 1.f) {
  125. lo = b[i] >> 16;
  126. if( lo>=0 ) break;
  127. hi = b[i] & 0xffff;
  128. tN = N[hi] + N[-lo];
  129. tX = X[hi] - X[-lo];
  130. tXX = XX[hi] + XX[-lo];
  131. tY = Y[hi] + Y[-lo];
  132. tXY = XY[hi] - XY[-lo];
  133. A = tY * tXX - tX * tXY;
  134. B = tN * tXY - tX * tY;
  135. D = tN * tXX - tX * tX;
  136. R = (A + x * B) / D;
  137. if (R < 0.f)
  138. R = 0.f;
  139. noise[i] = R - offset;
  140. }
  141. for ( ;; i++, x += 1.f) {
  142. lo = b[i] >> 16;
  143. hi = b[i] & 0xffff;
  144. if(hi>=n)break;
  145. tN = N[hi] - N[lo];
  146. tX = X[hi] - X[lo];
  147. tXX = XX[hi] - XX[lo];
  148. tY = Y[hi] - Y[lo];
  149. tXY = XY[hi] - XY[lo];
  150. A = tY * tXX - tX * tXY;
  151. B = tN * tXY - tX * tY;
  152. D = tN * tXX - tX * tX;
  153. R = (A + x * B) / D;
  154. if (R < 0.f) R = 0.f;
  155. noise[i] = R - offset;
  156. }
  157. for ( ; i < n; i++, x += 1.f) {
  158. R = (A + x * B) / D;
  159. if (R < 0.f) R = 0.f;
  160. noise[i] = R - offset;
  161. }
  162. if (fixed <= 0) return;
  163. for (i = 0, x = 0.f;; i++, x += 1.f) {
  164. hi = i + fixed / 2;
  165. lo = hi - fixed;
  166. if(lo>=0)break;
  167. tN = N[hi] + N[-lo];
  168. tX = X[hi] - X[-lo];
  169. tXX = XX[hi] + XX[-lo];
  170. tY = Y[hi] + Y[-lo];
  171. tXY = XY[hi] - XY[-lo];
  172. A = tY * tXX - tX * tXY;
  173. B = tN * tXY - tX * tY;
  174. D = tN * tXX - tX * tX;
  175. R = (A + x * B) / D;
  176. if (R - offset < noise[i]) noise[i] = R - offset;
  177. }
  178. for ( ;; i++, x += 1.f) {
  179. hi = i + fixed / 2;
  180. lo = hi - fixed;
  181. if(hi>=n)break;
  182. tN = N[hi] - N[lo];
  183. tX = X[hi] - X[lo];
  184. tXX = XX[hi] - XX[lo];
  185. tY = Y[hi] - Y[lo];
  186. tXY = XY[hi] - XY[lo];
  187. A = tY * tXX - tX * tXY;
  188. B = tN * tXY - tX * tY;
  189. D = tN * tXX - tX * tX;
  190. R = (A + x * B) / D;
  191. if (R - offset < noise[i]) noise[i] = R - offset;
  192. }
  193. for ( ; i < n; i++, x += 1.f) {
  194. R = (A + x * B) / D;
  195. if (R - offset < noise[i]) noise[i] = R - offset;
  196. }
  197. }
  198. static void _vp_noisemask(VorbisPsy *p,
  199. float *logfreq,
  200. float *logmask){
  201. int i,n=p->n/2;
  202. float *work=alloca(n*sizeof(*work));
  203. bark_noise_hybridmp(n,p->bark,logfreq,logmask,
  204. 140.,-1);
  205. for(i=0;i<n;i++)work[i]=logfreq[i]-logmask[i];
  206. bark_noise_hybridmp(n,p->bark,work,logmask,0.,
  207. p->vi->noisewindowfixed);
  208. for(i=0;i<n;i++)work[i]=logfreq[i]-work[i];
  209. {
  210. static int seq=0;
  211. float work2[n];
  212. for(i=0;i<n;i++){
  213. work2[i]=logmask[i]+work[i];
  214. }
  215. //_analysis_output("logfreq",seq,logfreq,n,0,0);
  216. //_analysis_output("median",seq,work,n,0,0);
  217. //_analysis_output("envelope",seq,work2,n,0,0);
  218. seq++;
  219. }
  220. for(i=0;i<n;i++){
  221. int dB=logmask[i]+.5;
  222. if(dB>=NOISE_COMPAND_LEVELS)dB=NOISE_COMPAND_LEVELS-1;
  223. if(dB<0)dB=0;
  224. logmask[i]= work[i]+p->vi->noisecompand[dB]+p->noiseoffset[i];
  225. }
  226. }
  227. VorbisPsy *vorbis_psy_init(int rate, int n)
  228. {
  229. long i,j,lo=-99,hi=1;
  230. VorbisPsy *p = speex_alloc(sizeof(VorbisPsy));
  231. memset(p,0,sizeof(*p));
  232. p->n = n;
  233. spx_drft_init(&p->lookup, n);
  234. p->bark = speex_alloc(n*sizeof(*p->bark));
  235. p->rate=rate;
  236. p->vi = &example_tuning;
  237. /* BH4 window */
  238. p->window = speex_alloc(sizeof(*p->window)*n);
  239. float a0 = .35875f;
  240. float a1 = .48829f;
  241. float a2 = .14128f;
  242. float a3 = .01168f;
  243. for(i=0;i<n;i++)
  244. p->window[i] = //a0 - a1*cos(2.*M_PI/n*(i+.5)) + a2*cos(4.*M_PI/n*(i+.5)) - a3*cos(6.*M_PI/n*(i+.5));
  245. sin((i+.5)/n * M_PI)*sin((i+.5)/n * M_PI);
  246. /* bark scale lookups */
  247. for(i=0;i<n;i++){
  248. float bark=toBARK(rate/(2*n)*i);
  249. for(;lo+p->vi->noisewindowlomin<i &&
  250. toBARK(rate/(2*n)*lo)<(bark-p->vi->noisewindowlo);lo++);
  251. for(;hi<=n && (hi<i+p->vi->noisewindowhimin ||
  252. toBARK(rate/(2*n)*hi)<(bark+p->vi->noisewindowhi));hi++);
  253. p->bark[i]=((lo-1)<<16)+(hi-1);
  254. }
  255. /* set up rolling noise median */
  256. p->noiseoffset=speex_alloc(n*sizeof(*p->noiseoffset));
  257. for(i=0;i<n;i++){
  258. float halfoc=toOC((i+.5)*rate/(2.*n))*2.;
  259. int inthalfoc;
  260. float del;
  261. if(halfoc<0)halfoc=0;
  262. if(halfoc>=P_BANDS-1)halfoc=P_BANDS-1;
  263. inthalfoc=(int)halfoc;
  264. del=halfoc-inthalfoc;
  265. p->noiseoffset[i]=
  266. p->vi->noiseoff[inthalfoc]*(1.-del) +
  267. p->vi->noiseoff[inthalfoc+1]*del;
  268. }
  269. #if 0
  270. _analysis_output_always("noiseoff0",ls,p->noiseoffset,n,1,0,0);
  271. #endif
  272. return p;
  273. }
  274. void vorbis_psy_destroy(VorbisPsy *p)
  275. {
  276. if(p){
  277. spx_drft_clear(&p->lookup);
  278. if(p->bark)
  279. speex_free(p->bark);
  280. if(p->noiseoffset)
  281. speex_free(p->noiseoffset);
  282. if(p->window)
  283. speex_free(p->window);
  284. memset(p,0,sizeof(*p));
  285. speex_free(p);
  286. }
  287. }
  288. void compute_curve(VorbisPsy *psy, float *audio, float *curve)
  289. {
  290. int i;
  291. float work[psy->n];
  292. float scale=4.f/psy->n;
  293. float scale_dB;
  294. scale_dB=todB(scale);
  295. /* window the PCM data; use a BH4 window, not vorbis */
  296. for(i=0;i<psy->n;i++)
  297. work[i]=audio[i] * psy->window[i];
  298. {
  299. static int seq=0;
  300. //_analysis_output("win",seq,work,psy->n,0,0);
  301. seq++;
  302. }
  303. /* FFT yields more accurate tonal estimation (not phase sensitive) */
  304. spx_drft_forward(&psy->lookup,work);
  305. /* magnitudes */
  306. work[0]=scale_dB+todB(work[0]);
  307. for(i=1;i<psy->n-1;i+=2){
  308. float temp = work[i]*work[i] + work[i+1]*work[i+1];
  309. work[(i+1)>>1] = scale_dB+.5f * todB(temp);
  310. }
  311. /* derive a noise curve */
  312. _vp_noisemask(psy,work,curve);
  313. #define SIDEL 12
  314. for (i=0;i<SIDEL;i++)
  315. {
  316. curve[i]=curve[SIDEL];
  317. }
  318. #define SIDEH 12
  319. for (i=0;i<SIDEH;i++)
  320. {
  321. curve[(psy->n>>1)-i-1]=curve[(psy->n>>1)-SIDEH];
  322. }
  323. for(i=0;i<((psy->n)>>1);i++)
  324. curve[i] = fromdB(1.2*curve[i]+.2*i);
  325. //curve[i] = fromdB(0.8*curve[i]+.35*i);
  326. //curve[i] = fromdB(0.9*curve[i])*pow(1.0*i+45,1.3);
  327. }
  328. /* Transform a masking curve (power spectrum) into a pole-zero filter */
  329. void curve_to_lpc(VorbisPsy *psy, float *curve, float *awk1, float *awk2, int ord)
  330. {
  331. int i;
  332. float ac[psy->n];
  333. float tmp;
  334. int len = psy->n >> 1;
  335. for (i=0;i<2*len;i++)
  336. ac[i] = 0;
  337. for (i=1;i<len;i++)
  338. ac[2*i-1] = curve[i];
  339. ac[0] = curve[0];
  340. ac[2*len-1] = curve[len-1];
  341. spx_drft_backward(&psy->lookup, ac);
  342. _spx_lpc(awk1, ac, ord);
  343. tmp = 1.;
  344. for (i=0;i<ord;i++)
  345. {
  346. tmp *= .99;
  347. awk1[i] *= tmp;
  348. }
  349. #if 0
  350. for (i=0;i<ord;i++)
  351. awk2[i] = 0;
  352. #else
  353. /* Use the second (awk2) filter to correct the first one */
  354. for (i=0;i<2*len;i++)
  355. ac[i] = 0;
  356. for (i=0;i<ord;i++)
  357. ac[i+1] = awk1[i];
  358. ac[0] = 1;
  359. spx_drft_forward(&psy->lookup, ac);
  360. /* Compute (power) response of awk1 (all zero) */
  361. ac[0] *= ac[0];
  362. for (i=1;i<len;i++)
  363. ac[i] = ac[2*i-1]*ac[2*i-1] + ac[2*i]*ac[2*i];
  364. ac[len] = ac[2*len-1]*ac[2*len-1];
  365. /* Compute correction required */
  366. for (i=0;i<len;i++)
  367. curve[i] = 1. / (1e-6f+curve[i]*ac[i]);
  368. for (i=0;i<2*len;i++)
  369. ac[i] = 0;
  370. for (i=1;i<len;i++)
  371. ac[2*i-1] = curve[i];
  372. ac[0] = curve[0];
  373. ac[2*len-1] = curve[len-1];
  374. spx_drft_backward(&psy->lookup, ac);
  375. _spx_lpc(awk2, ac, ord);
  376. tmp = 1;
  377. for (i=0;i<ord;i++)
  378. {
  379. tmp *= .99;
  380. awk2[i] *= tmp;
  381. }
  382. #endif
  383. }
  384. #if 0
  385. #include <stdio.h>
  386. #include <math.h>
  387. #define ORDER 10
  388. #define CURVE_SIZE 24
  389. int main()
  390. {
  391. int i;
  392. float curve[CURVE_SIZE];
  393. float awk1[ORDER], awk2[ORDER];
  394. for (i=0;i<CURVE_SIZE;i++)
  395. scanf("%f ", &curve[i]);
  396. for (i=0;i<CURVE_SIZE;i++)
  397. curve[i] = pow(10.f, .1*curve[i]);
  398. curve_to_lpc(curve, CURVE_SIZE, awk1, awk2, ORDER);
  399. for (i=0;i<ORDER;i++)
  400. printf("%f ", awk1[i]);
  401. printf ("\n");
  402. for (i=0;i<ORDER;i++)
  403. printf("%f ", awk2[i]);
  404. printf ("\n");
  405. return 0;
  406. }
  407. #endif
  408. #endif