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