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