home *** CD-ROM | disk | FTP | other *** search
/ Da Capo / da_capo_vol1.bin / programs / amiga / misc / mpegaudio / psy.c < prev    next >
C/C++ Source or Header  |  1994-03-21  |  19KB  |  444 lines

  1. /**********************************************************************
  2. Copyright (c) 1991 MPEG/audio software simulation group, All Rights Reserved
  3. psy.c
  4. **********************************************************************/
  5. /**********************************************************************
  6.  * MPEG/audio coding/decoding software, work in progress              *
  7.  *   NOT for public distribution until verified and approved by the   *
  8.  *   MPEG/audio committee.  For further information, please contact   *
  9.  *   Davis Pan, 508-493-2241, e-mail: pan@3d.enet.dec.com             *
  10.  *                                                                    *
  11.  * VERSION 3.9                                                        *
  12.  *   changes made since last update:                                  *
  13.  *   date   programmers         comment                               *
  14.  * 2/25/91  Davis Pan           start of version 1.0 records          *
  15.  * 5/10/91  W. Joseph Carter    Ported to Macintosh and Unix.         *
  16.  * 7/10/91  Earle Jennings      Ported to MsDos.                      *
  17.  *                              replace of floats with FLOAT          *
  18.  * 2/11/92  W. Joseph Carter    Fixed mem_alloc() arg for "absthr".   *
  19.  * 7/24/92  M. Iwadare          HANN window coefficients modified.    *
  20.  * 7/27/92  Masahiro Iwadare    Bug fix, FFT modification for Layer 3 *
  21.  * 7/27/92  Masahiro Iwadare    Bug fix, "new", "old", and "oldest"   *
  22.  *                              updates                               *
  23.  * 8/07/92  Mike Coleman        Bug fix, read_absthr()                *
  24.  **********************************************************************/
  25.  
  26. #include "common.h"
  27. #include "encoder.h"
  28.  
  29. void psycho_anal(buffer,savebuf,chn,lay,snr32,sfreq)
  30. short int *buffer;
  31. short int savebuf[1056];
  32. int   chn, lay;
  33. FLOAT snr32[32];
  34. double sfreq;        /* to match prototype : float args are always double */
  35. {
  36.  unsigned int   i, j, k;
  37.  FLOAT          r_prime, phi_prime;
  38.  FLOAT          freq_mult, bval_lo, minthres, sum_energy;
  39.  double         tb, temp1, temp2, temp3;
  40.  
  41. /* The static variables "r", "phi_sav", "new", "old" and "oldest" have    */
  42. /* to be remembered for the unpredictability measure.  For "r" and        */
  43. /* "phi_sav", the first index from the left is the channel select and     */
  44. /* the second index is the "age" of the data.                             */
  45.  
  46.  static int     new = 0, old = 1, oldest = 0;
  47.  static int     init = 0, flush, sync_flush, syncsize, sfreq_idx;
  48.  
  49. /* The following static variables are constants.                           */
  50.  
  51.  static double  nmt = 5.5;
  52.  
  53.  static FLOAT   crit_band[27] = {0,  100,  200, 300, 400, 510, 630,  770,
  54.                                920, 1080, 1270,1480,1720,2000,2320, 2700,
  55.                               3150, 3700, 4400,5300,6400,7700,9500,12000,
  56.                              15500,25000,30000};
  57.  
  58.  static FLOAT   bmax[27] = {20.0, 20.0, 20.0, 20.0, 20.0, 17.0, 15.0,
  59.                             10.0,  7.0,  4.4,  4.5,  4.5,  4.5,  4.5,
  60.                              4.5,  4.5,  4.5,  4.5,  4.5,  4.5,  4.5,
  61.                              4.5,  4.5,  4.5,  3.5,  3.5,  3.5};
  62.  
  63. /* The following pointer variables point to large areas of memory         */
  64. /* dynamically allocated by the mem_alloc() function.  Dynamic memory     */
  65. /* allocation is used in order to avoid stack frame or data area          */
  66. /* overflow errors that otherwise would have occurred at compile time     */
  67. /* on the Macintosh computer.                                             */
  68.  
  69.  FLOAT          *grouped_c, *grouped_e, *nb, *cb, *ecb, *bc;
  70.  FLOAT          *wsamp_r, *wsamp_i, *phi, *energy;
  71.  FLOAT          *c, *fthr;
  72.  F32            *snrtmp;
  73.  
  74.  static int     *numlines;
  75.  static int     *partition;
  76.  static FLOAT   *cbval, *rnorm;
  77.  static FLOAT   *window;
  78.  static FLOAT   *absthr;
  79.  static double  *tmn;
  80.  static FCB     *s;
  81.  static FHBLK   *lthr;
  82.  static F2HBLK  *r, *phi_sav;
  83.  
  84. /* These dynamic memory allocations simulate "automatic" variables        */
  85. /* placed on the stack.  For each mem_alloc() call here, there must be    */
  86. /* a corresponding mem_free() call at the end of this function.           */
  87.  
  88.  grouped_c = (FLOAT *) mem_alloc(sizeof(FCB), "grouped_c");
  89.  grouped_e = (FLOAT *) mem_alloc(sizeof(FCB), "grouped_e");
  90.  nb = (FLOAT *) mem_alloc(sizeof(FCB), "nb");
  91.  cb = (FLOAT *) mem_alloc(sizeof(FCB), "cb");
  92.  ecb = (FLOAT *) mem_alloc(sizeof(FCB), "ecb");
  93.  bc = (FLOAT *) mem_alloc(sizeof(FCB), "bc");
  94.  wsamp_r = (FLOAT *) mem_alloc(sizeof(FBLK), "wsamp_r");
  95.  wsamp_i = (FLOAT *) mem_alloc(sizeof(FBLK), "wsamp_i");
  96.  phi = (FLOAT *) mem_alloc(sizeof(FBLK), "phi");
  97.  energy = (FLOAT *) mem_alloc(sizeof(FBLK), "energy");
  98.  c = (FLOAT *) mem_alloc(sizeof(FHBLK), "c");
  99.  fthr = (FLOAT *) mem_alloc(sizeof(FHBLK), "fthr");
  100.  snrtmp = (F32 *) mem_alloc(sizeof(F2_32), "snrtmp");
  101.  
  102.  if(init==0){
  103.  
  104. /* These dynamic memory allocations simulate "static" variables placed    */
  105. /* in the data space.  Each mem_alloc() call here occurs only once at     */
  106. /* initialization time.  The mem_free() function must not be called.      */
  107.  
  108.      numlines = (int *) mem_alloc(sizeof(ICB), "numlines");
  109.      partition = (int *) mem_alloc(sizeof(IHBLK), "partition");
  110.      cbval = (FLOAT *) mem_alloc(sizeof(FCB), "cbval");
  111.      rnorm = (FLOAT *) mem_alloc(sizeof(FCB), "rnorm");
  112.      window = (FLOAT *) mem_alloc(sizeof(FBLK), "window");
  113.      absthr = (FLOAT *) mem_alloc(sizeof(FHBLK), "absthr");
  114.      tmn = (double *) mem_alloc(sizeof(DCB), "tmn");
  115.      s = (FCB *) mem_alloc(sizeof(FCBCB), "s");
  116.      lthr = (FHBLK *) mem_alloc(sizeof(F2HBLK), "lthr");
  117.      r = (F2HBLK *) mem_alloc(sizeof(F22HBLK), "r");
  118.      phi_sav = (F2HBLK *) mem_alloc(sizeof(F22HBLK), "phi_sav");
  119.  
  120.      i = sfreq + 0.5;
  121.      switch(i){
  122.         case 32000: sfreq_idx = 0; break;
  123.         case 44100: sfreq_idx = 1; break;
  124.         case 48000: sfreq_idx = 2; break;
  125.         default:    printf("error, invalid sampling frequency: %d Hz\n",i);
  126.         exit(-1);
  127.      }
  128.      printf("absthr[][] sampling frequency index: %d\n",sfreq_idx);
  129.      read_absthr(absthr, sfreq_idx);
  130.      if(lay==1){
  131.         flush = 384;
  132.         syncsize = 1024;
  133.         sync_flush = 576;
  134.      }
  135.      else {
  136.         flush = 384*3.0/2.0;
  137.         syncsize = 1056;
  138.         sync_flush = syncsize - flush;
  139.      }
  140. /* calculate HANN window coefficients */
  141. /*   for(i=0;i<BLKSIZE;i++)window[i]=0.5*(1-cos(2.0*PI*i/(BLKSIZE-1.0))); */
  142.      for(i=0;i<BLKSIZE;i++)window[i]=0.5*(1-cos(2.0*PI*(i-0.5)/BLKSIZE));
  143. /* reset states used in unpredictability measure */
  144.      for(i=0;i<HBLKSIZE;i++){
  145.         r[0][0][i]=r[1][0][i]=r[0][1][i]=r[1][1][i]=0;
  146.         phi_sav[0][0][i]=phi_sav[1][0][i]=0;
  147.         phi_sav[0][1][i]=phi_sav[1][1][i]=0;
  148.         lthr[0][i] = 60802371420160.0;
  149.         lthr[1][i] = 60802371420160.0;
  150.      }
  151. /*****************************************************************************
  152.  * Initialization: Compute the following constants for use later             *
  153.  *    partition[HBLKSIZE] = the partition number associated with each        *
  154.  *                          frequency line                                   *
  155.  *    cbval[CBANDS]       = the center (average) bark value of each          *
  156.  *                          partition                                        *
  157.  *    numlines[CBANDS]    = the number of frequency lines in each partition  *
  158.  *    tmn[CBANDS]         = tone masking noise                               *
  159.  *****************************************************************************/
  160. /* compute fft frequency multiplicand */
  161.      freq_mult = sfreq/BLKSIZE;
  162.  
  163. /* calculate fft frequency, then bval of each line (use fthr[] as tmp storage)*/
  164.      for(i=0;i<HBLKSIZE;i++){
  165.         temp1 = i*freq_mult;
  166.         j = 1;
  167.         while(temp1>crit_band[j])j++;
  168.         fthr[i]=j-1+(temp1-crit_band[j-1])/(crit_band[j]-crit_band[j-1]);
  169.      }
  170.      partition[0] = 0;
  171. /* temp2 is the counter of the number of frequency lines in each partition */
  172.      temp2 = 1;
  173.      cbval[0]=fthr[0];
  174.      bval_lo=fthr[0];
  175.      for(i=1;i<HBLKSIZE;i++){
  176.         if((fthr[i]-bval_lo)>0.33){
  177.            partition[i]=partition[i-1]+1;
  178.            cbval[partition[i-1]] = cbval[partitio