psytune.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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-2007 *
  9. * by the Xiph.Org Foundation http://www.xiph.org/ *
  10. * *
  11. ********************************************************************
  12. function: simple utility that runs audio through the psychoacoustics
  13. without encoding
  14. last mod: $Id: psytune.c 16037 2009-05-26 21:10:58Z xiphmont $
  15. ********************************************************************/
  16. /* NB: this is dead code, retained purely for doc and reference value
  17. don't try to compile it */
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <math.h>
  22. #include "vorbis/codec.h"
  23. #include "codec_internal.h"
  24. #include "os.h"
  25. #include "misc.h"
  26. #include "psy.h"
  27. #include "mdct.h"
  28. #include "smallft.h"
  29. #include "window.h"
  30. #include "scales.h"
  31. #include "lpc.h"
  32. #include "lsp.h"
  33. #include "masking.h"
  34. #include "registry.h"
  35. static vorbis_info_psy_global _psy_set0G={
  36. 0, /* decaydBpms */
  37. 8, /* lines per eighth octave */
  38. /* thresh sample period, preecho clamp trigger threshhold, range, minenergy */
  39. 256, {26.f,26.f,26.f,30.f}, {-90.f,-90.f,-90.f,-90.f}, -90.f,
  40. -6.f,
  41. 0,
  42. 0.,
  43. 0.,
  44. };
  45. static vp_part _vp_part0[]={
  46. { 1,9e10f, 9e10f, 1.f,9999.f},
  47. { 9999, .75f, 9e10f, .5f,9999.f},
  48. /*{ 9999, 1.5f, 9e10f, .5f,9999.f},*/
  49. { 18,9e10f, 9e10f, .5f, 30.f},
  50. { 9999,9e10f, 9e10f, .5f, 30.f}
  51. };
  52. static vp_couple _vp_couple0[]={
  53. { 1, {9e10f,9e10f,0}, { 0.f, 0.f,0}, { 0.f, 0.f,0}, {0.f,0.f,0}},
  54. { 18, {9e10f,9e10f,0}, { 0.f, 0.f,0}, { 0.f, 0.f,0}, {0.f,0.f,0}},
  55. { 9999, {9e10f,9e10f,0}, { 0.f, 9e10f,0}, { 0.f,22.f,1}, {0.f,0.f,0}}
  56. };
  57. static vorbis_info_psy _psy_set0={
  58. ATH_Bark_dB_lineaggressive,
  59. -100.f,
  60. -140.f,
  61. 6.f, /* floor master att */
  62. /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 */
  63. /* x: 63 88 125 175 250 350 500 700 1k 1.4k 2k 2.8k 4k 5.6k 8k 11.5k 16k Hz */
  64. /* y: 0 10 20 30 40 50 60 70 80 90 100 dB */
  65. 1, /* tonemaskp */
  66. 0.f, /* tone master att */
  67. /* 0 10 20 30 40 50 60 70 80 90 100 */
  68. {
  69. {-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f}, /*63*/
  70. {-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f}, /*88*/
  71. {-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f,-999.f}, /*125*/
  72. {-30.f,-30.f,-35.f,-40.f,-40.f,-50.f,-60.f,-70.f,-80.f,-90.f,-100.f}, /*175*/
  73. {-30.f,-30.f,-35.f,-40.f,-40.f,-50.f,-60.f,-70.f,-80.f,-90.f,-100.f}, /*250*/
  74. {-30.f,-30.f,-35.f,-40.f,-40.f,-50.f,-60.f,-70.f,-80.f,-90.f,-100.f}, /*350*/
  75. {-30.f,-30.f,-35.f,-40.f,-40.f,-50.f,-60.f,-70.f,-80.f,-90.f,-100.f}, /*500*/
  76. {-30.f,-30.f,-35.f,-40.f,-40.f,-50.f,-60.f,-70.f,-80.f,-90.f,-100.f}, /*700*/
  77. {-30.f,-30.f,-35.f,-40.f,-40.f,-50.f,-60.f,-70.f,-80.f,-90.f,-100.f}, /*1000*/
  78. {-30.f,-30.f,-35.f,-40.f,-40.f,-50.f,-60.f,-70.f,-80.f,-90.f,-100.f}, /*1400*/
  79. {-40.f,-40.f,-40.f,-40.f,-40.f,-50.f,-60.f,-70.f,-80.f,-90.f,-100.f}, /*2000*/
  80. {-40.f,-40.f,-40.f,-40.f,-40.f,-50.f,-60.f,-70.f,-80.f,-90.f,-100.f}, /*2800*/
  81. {-40.f,-40.f,-40.f,-40.f,-40.f,-50.f,-60.f,-70.f,-80.f,-90.f,-100.f}, /*4000*/
  82. {-30.f,-35.f,-35.f,-40.f,-40.f,-50.f,-60.f,-70.f,-80.f,-90.f,-100.f}, /*5600*/
  83. {-30.f,-30.f,-33.f,-35.f,-40.f,-50.f,-60.f,-70.f,-80.f,-90.f,-100.f}, /*8000*/
  84. {-30.f,-30.f,-33.f,-35.f,-40.f,-45.f,-50.f,-60.f,-70.f,-85.f,-100.f}, /*11500*/
  85. {-24.f,-24.f,-26.f,-32.f,-32.f,-42.f,-50.f,-60.f,-70.f,-85.f,-100.f}, /*16000*/
  86. },
  87. 1,/* peakattp */
  88. {{-14.f,-20.f,-20.f,-20.f,-26.f,-32.f,-40.f,-40.f,-40.f,-40.f,-40.f},/*63*/
  89. {-14.f,-20.f,-20.f,-20.f,-26.f,-32.f,-40.f,-40.f,-40.f,-40.f,-40.f},/*88*/
  90. {-14.f,-20.f,-20.f,-20.f,-26.f,-32.f,-40.f,-40.f,-40.f,-40.f,-40.f},/*125*/
  91. {-14.f,-20.f,-20.f,-20.f,-26.f,-32.f,-40.f,-40.f,-40.f,-40.f,-40.f},/*175*/
  92. {-14.f,-20.f,-20.f,-20.f,-26.f,-32.f,-40.f,-40.f,-40.f,-40.f,-40.f},/*250*/
  93. {-14.f,-20.f,-20.f,-20.f,-26.f,-32.f,-40.f,-40.f,-40.f,-40.f,-40.f},/*350*/
  94. {-14.f,-20.f,-20.f,-20.f,-26.f,-32.f,-40.f,-40.f,-40.f,-40.f,-40.f},/*500*/
  95. {-14.f,-20.f,-20.f,-20.f,-26.f,-32.f,-40.f,-40.f,-40.f,-40.f,-40.f},/*700*/
  96. {-14.f,-20.f,-20.f,-20.f,-26.f,-32.f,-40.f,-40.f,-40.f,-40.f,-40.f},/*1000*/
  97. {-14.f,-20.f,-20.f,-20.f,-26.f,-32.f,-40.f,-40.f,-40.f,-40.f,-40.f},/*1400*/
  98. {-14.f,-20.f,-20.f,-20.f,-26.f,-32.f,-40.f,-40.f,-40.f,-40.f,-40.f},/*2000*/
  99. {-14.f,-20.f,-20.f,-20.f,-26.f,-32.f,-40.f,-40.f,-40.f,-40.f,-40.f},/*2800*/
  100. {-14.f,-20.f,-20.f,-20.f,-26.f,-32.f,-40.f,-40.f,-40.f,-40.f,-40.f},/*4000*/
  101. {-10.f,-12.f,-14.f,-16.f,-16.f,-20.f,-24.f,-30.f,-32.f,-40.f,-40.f},/*5600*/
  102. {-10.f,-12.f,-14.f,-16.f,-16.f,-20.f,-24.f,-30.f,-32.f,-40.f,-40.f},/*8000*/
  103. {-10.f,-10.f,-10.f,-12.f,-14.f,-18.f,-22.f,-28.f,-32.f,-40.f,-40.f},/*11500*/
  104. {-10.f,-10.f,-10.f,-12.f,-14.f,-18.f,-22.f,-28.f,-32.f,-40.f,-40.f},/*16000*/
  105. },
  106. 1,/*noisemaskp */
  107. -10.f, /* suppress any noise curve over maxspec+n */
  108. .5f, /* low window */
  109. .5f, /* high window */
  110. 10,
  111. 10,
  112. 25,
  113. {.000f, 0.f, /*63*/
  114. .000f, 0.f, /*88*/
  115. .000f, 0.f, /*125*/
  116. .000f, 0.f, /*175*/
  117. .000f, 0.f, /*250*/
  118. .000f, 0.f, /*350*/
  119. .000f, 0.f, /*500*/
  120. .000f, 0.f, /*700*/
  121. .000f, 0.f, /*1000*/
  122. .300f, 0.f, /*1400*/
  123. .300f, 0.f, /*2000*/
  124. .300f, 0.f, /*2800*/
  125. .500f, 0.f, /*4000*/
  126. .700f, 0.f, /*5600*/
  127. .850f, 0.f, /*8000*/
  128. .900f, 0.f, /*11500*/
  129. .900f, 1.f, /*16000*/
  130. },
  131. 95.f, /* even decade + 5 is important; saves an rint() later in a
  132. tight loop) */
  133. -44.,
  134. 32,
  135. _vp_part0,_vp_couple0
  136. };
  137. static vorbis_info_floor1 _floor_set0={1,
  138. {0},
  139. {32},
  140. {0},
  141. {0},
  142. {{-1}},
  143. 2,
  144. {0,1024,
  145. 88,31,243,
  146. 14,54,143,460,
  147. 6,3,10, 22,18,26, 41,36,47,
  148. 69,61,78, 112,99,126, 185,162,211,
  149. 329,282,387, 672,553,825
  150. },
  151. 60,30,400,
  152. 20,8,1,18.,
  153. 20,600,
  154. 960};
  155. static vorbis_info_mapping0 mapping_info={1,{0,1},{0},{0},{0},0, 1, {0},{1}};
  156. static codec_setup_info codec_setup0={ {0,0},
  157. 1,1,1,1,1,0,1,
  158. {NULL},
  159. {0},{&mapping_info},
  160. {0},{NULL},
  161. {1},{&_floor_set0},
  162. {2},{NULL},
  163. {NULL},
  164. {&_psy_set0},
  165. &_psy_set0G};
  166. static int noisy=0;
  167. void analysis(char *base,int i,float *v,int n,int bark,int dB){
  168. if(noisy){
  169. int j;
  170. FILE *of;
  171. char buffer[80];
  172. sprintf(buffer,"%s_%d.m",base,i);
  173. of=fopen(buffer,"w");
  174. for(j=0;j<n;j++){
  175. if(dB && v[j]==0)
  176. fprintf(of,"\n\n");
  177. else{
  178. if(bark)
  179. fprintf(of,"%g ",toBARK(22050.f*j/n));
  180. else
  181. fprintf(of,"%g ",(float)j);
  182. if(dB){
  183. fprintf(of,"%g\n",todB(v+j));
  184. }else{
  185. fprintf(of,"%g\n",v[j]);
  186. }
  187. }
  188. }
  189. fclose(of);
  190. }
  191. }
  192. long frameno=0;
  193. /****************************************************************/
  194. int main(int argc,char *argv[]){
  195. int eos=0;
  196. float nonz=0.f;
  197. float acc=0.f;
  198. float tot=0.f;
  199. float ampmax=-9999,newmax;
  200. float local_ampmax[2];
  201. int framesize=2048;
  202. float ampmax_att_per_sec=-6.;
  203. float *pcm[2],*out[2],*window,*flr[2],*mask[2],*work[2];
  204. signed char *buffer,*buffer2;
  205. mdct_lookup m_look;
  206. drft_lookup f_look;
  207. vorbis_look_psy p_look;
  208. vorbis_look_psy_global *pg_look;
  209. vorbis_look_floor *floor_look;
  210. vorbis_info vi;
  211. long i,j,k;
  212. int ath=0;
  213. int decayp=0;
  214. argv++;
  215. while(*argv){
  216. if(*argv[0]=='-'){
  217. /* option */
  218. if(argv[0][1]=='v'){
  219. noisy=0;
  220. }
  221. }else
  222. if(*argv[0]=='+'){
  223. /* option */
  224. if(argv[0][1]=='v'){
  225. noisy=1;
  226. }
  227. }else
  228. framesize=atoi(argv[0]);
  229. argv++;
  230. }
  231. vi.channels=2;
  232. vi.codec_setup=&codec_setup0;
  233. pcm[0]=_ogg_malloc(framesize*sizeof(float));
  234. pcm[1]=_ogg_malloc(framesize*sizeof(float));
  235. out[0]=_ogg_calloc(framesize/2,sizeof(float));
  236. out[1]=_ogg_calloc(framesize/2,sizeof(float));
  237. work[0]=_ogg_calloc(framesize,sizeof(float));
  238. work[1]=_ogg_calloc(framesize,sizeof(float));
  239. flr[0]=_ogg_calloc(framesize/2,sizeof(float));
  240. flr[1]=_ogg_calloc(framesize/2,sizeof(float));
  241. buffer=_ogg_malloc(framesize*4);
  242. buffer2=buffer+framesize*2;
  243. window=_vorbis_window_create(0,framesize,framesize/2,framesize/2);
  244. mdct_init(&m_look,framesize);
  245. drft_init(&f_look,framesize);
  246. _vp_psy_init(&p_look,&_psy_set0,&_psy_set0G,framesize/2,44100);
  247. pg_look=_vp_global_look(&vi);
  248. floor_look=_floor_P[1]->look(NULL,NULL,&_floor_set0);
  249. /* we cheat on the WAV header; we just bypass 44 bytes and never
  250. verify that it matches 16bit/stereo/44.1kHz. */
  251. fread(buffer,1,44,stdin);
  252. fwrite(buffer,1,44,stdout);
  253. memset(buffer,0,framesize*2);
  254. analysis("window",0,window,framesize,0,0);
  255. fprintf(stderr,"Processing for frame size %d...\n",framesize);
  256. while(!eos){
  257. long bytes=fread(buffer2,1,framesize*2,stdin);
  258. if(bytes<framesize*2)
  259. memset(buffer2+bytes,0,framesize*2-bytes);
  260. if(bytes!=0){
  261. int nonzero[2];
  262. /* uninterleave samples */
  263. for(i=0;i<framesize;i++){
  264. pcm[0][i]=((buffer[i*4+1]<<8)|
  265. (0x00ff&(int)buffer[i*4]))/32768.f;
  266. pcm[1][i]=((buffer[i*4+3]<<8)|
  267. (0x00ff&(int)buffer[i*4+2]))/32768.f;
  268. }
  269. {
  270. float secs=framesize/44100.;
  271. ampmax+=secs*ampmax_att_per_sec;
  272. if(ampmax<-9999)ampmax=-9999;
  273. }
  274. for(i=0;i<2;i++){
  275. float scale=4.f/framesize;
  276. float *fft=work[i];
  277. float *mdct=pcm[i];
  278. float *logmdct=mdct+framesize/2;
  279. analysis("pre",frameno+i,pcm[i],framesize,0,0);
  280. /* fft and mdct transforms */
  281. for(j=0;j<framesize;j++)
  282. fft[j]=pcm[i][j]*=window[j];
  283. drft_forward(&f_look,fft);
  284. local_ampmax[i]=-9999.f;
  285. fft[0]*=scale;
  286. fft[0]=todB(fft);
  287. for(j=1;j<framesize-1;j+=2){
  288. float temp=scale*FAST_HYPOT(fft[j],fft[j+1]);
  289. temp=fft[(j+1)>>1]=todB(&temp);
  290. if(temp>local_ampmax[i])local_ampmax[i]=temp;
  291. }
  292. if(local_ampmax[i]>ampmax)ampmax=local_ampmax[i];
  293. mdct_forward(&m_look,pcm[i],mdct);
  294. for(j=0;j<framesize/2;j++)
  295. logmdct[j]=todB(mdct+j);
  296. analysis("mdct",frameno+i,logmdct,framesize/2,1,0);
  297. analysis("fft",frameno+i,fft,framesize/2,1,0);
  298. }
  299. for(i=0;i<2;i++){
  300. float amp;
  301. float *fft=work[i];
  302. float *logmax=fft;
  303. float *mdct=pcm[i];
  304. float *logmdct=mdct+framesize/2;
  305. float *mask=fft+framesize/2;
  306. /* floor psychoacoustics */
  307. _vp_compute_mask(&p_look,
  308. pg_look,
  309. i,
  310. fft,
  311. logmdct,
  312. mask,
  313. ampmax,
  314. local_ampmax[i],
  315. framesize/2);
  316. analysis("mask",frameno+i,mask,framesize/2,1,0);
  317. {
  318. vorbis_block vb;
  319. vorbis_dsp_state vd;
  320. memset(&vd,0,sizeof(vd));
  321. vd.vi=&vi;
  322. vb.vd=&vd;
  323. vb.pcmend=framesize;
  324. /* floor quantization/application */
  325. nonzero[i]=_floor_P[1]->forward(&vb,floor_look,
  326. mdct,
  327. logmdct,
  328. mask,
  329. logmax,
  330. flr[i]);
  331. }
  332. _vp_remove_floor(&p_look,
  333. pg_look,
  334. logmdct,
  335. mdct,
  336. flr[i],
  337. pcm[i],
  338. local_ampmax[i]);
  339. for(j=0;j<framesize/2;j++)
  340. if(fabs(pcm[i][j])>1500)
  341. fprintf(stderr,"%ld ",frameno+i);
  342. analysis("res",frameno+i,pcm[i],framesize/2,1,0);
  343. analysis("codedflr",frameno+i,flr[i],framesize/2,1,1);
  344. }
  345. /* residue prequantization */
  346. _vp_partition_prequant(&p_look,
  347. &vi,
  348. pcm,
  349. nonzero);
  350. for(i=0;i<2;i++)
  351. analysis("quant",frameno+i,pcm[i],framesize/2,1,0);
  352. /* channel coupling / stereo quantization */
  353. _vp_couple(&p_look,
  354. &mapping_info,
  355. pcm,
  356. nonzero);
  357. for(i=0;i<2;i++)
  358. analysis("coupled",frameno+i,pcm[i],framesize/2,1,0);
  359. /* decoupling */
  360. for(i=mapping_info.coupling_steps-1;i>=0;i--){
  361. float *pcmM=pcm[mapping_info.coupling_mag[i]];
  362. float *pcmA=pcm[mapping_info.coupling_ang[i]];
  363. for(j=0;j<framesize/2;j++){
  364. float mag=pcmM[j];
  365. float ang=pcmA[j];
  366. if(mag>0)
  367. if(ang>0){
  368. pcmM[j]=mag;
  369. pcmA[j]=mag-ang;
  370. }else{
  371. pcmA[j]=mag;
  372. pcmM[j]=mag+ang;
  373. }
  374. else
  375. if(ang>0){
  376. pcmM[j]=mag;
  377. pcmA[j]=mag+ang;
  378. }else{
  379. pcmA[j]=mag;
  380. pcmM[j]=mag-ang;
  381. }
  382. }
  383. }
  384. for(i=0;i<2;i++)
  385. analysis("decoupled",frameno+i,pcm[i],framesize/2,1,0);
  386. for(i=0;i<2;i++){
  387. float amp;
  388. for(j=0;j<framesize/2;j++)
  389. pcm[i][j]*=flr[i][j];
  390. analysis("final",frameno+i,pcm[i],framesize/2,1,1);
  391. /* take it back to time */
  392. mdct_backward(&m_look,pcm[i],pcm[i]);
  393. for(j=0;j<framesize/2;j++)
  394. out[i][j]+=pcm[i][j]*window[j];
  395. analysis("out",frameno+i,out[i],framesize/2,0,0);
  396. }
  397. /* write data. Use the part of buffer we're about to shift out */
  398. for(i=0;i<2;i++){
  399. char *ptr=buffer+i*2;
  400. float *mono=out[i];
  401. int flag=0;
  402. for(j=0;j<framesize/2;j++){
  403. int val=mono[j]*32767.;
  404. /* might as well guard against clipping */
  405. if(val>32767){
  406. if(!flag)fprintf(stderr,"clipping in frame %ld ",frameno+i);
  407. flag=1;
  408. val=32767;
  409. }
  410. if(val<-32768){
  411. if(!flag)fprintf(stderr,"clipping in frame %ld ",frameno+i);
  412. flag=1;
  413. val=-32768;
  414. }
  415. ptr[0]=val&0xff;
  416. ptr[1]=(val>>8)&0xff;
  417. ptr+=4;
  418. }
  419. }
  420. fprintf(stderr,"*");
  421. fwrite(buffer,1,framesize*2,stdout);
  422. memmove(buffer,buffer2,framesize*2);
  423. for(i=0;i<2;i++){
  424. for(j=0,k=framesize/2;j<framesize/2;j++,k++)
  425. out[i][j]=pcm[i][k]*window[k];
  426. }
  427. frameno+=2;
  428. }else
  429. eos=1;
  430. }
  431. fprintf(stderr,"average raw bits of entropy: %.03g/sample\n",acc/tot);
  432. fprintf(stderr,"average nonzero samples: %.03g/%d\n",nonz/tot*framesize/2,
  433. framesize/2);
  434. fprintf(stderr,"Done\n\n");
  435. return 0;
  436. }