psytune.c 14 KB

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