home *** CD-ROM | disk | FTP | other *** search
/ Da Capo / da_capo_vol1.bin / programs / amiga / misc / mpegaudio / tonal.c < prev   
C/C++ Source or Header  |  1994-03-21  |  37KB  |  1,035 lines

  1. /**********************************************************************
  2. Copyright (c) 1991 MPEG/audio software simulation group, All Rights Reserved
  3. tonal.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.9t                                                       *
  12.  *   changes made since last update:                                  *
  13.  *   date   programmers         comment                               *
  14.  * 2/25/91  Douglas Wong        start of version 1.1 records          *
  15.  * 3/06/91  Douglas Wong        rename: setup.h to endef.h            *
  16.  *                              updated I_psycho_one and II_psycho_one*
  17.  * 3/11/91  W. J. Carter        Added Douglas Wong's updates dated    *
  18.  *                              3/9/91 for I_Psycho_One() and for     *
  19.  *                              II_Psycho_One().                      *
  20.  * 5/10/91  W. Joseph Carter    Ported to Macintosh and Unix.         *
  21.  *                              Located and fixed numerous software   *
  22.  *                              bugs and table data errors.           *
  23.  * 6/11/91  Davis Pan           corrected several bugs                *
  24.  *                              based on comments from H. Fuchs       *
  25.  * 01jul91  dpwe (Aware Inc.)   Made pow() args float                 *
  26.  *                              Removed logical bug in I_tonal_label: *
  27.  *                              Sometimes *tone returned == STOP      *
  28.  * 7/10/91  Earle Jennings      no change necessary in port to MsDos  *
  29.  * 11sep91  dpwe@aware.com      Subtracted 90.3dB from II_f_f_t peaks *
  30.  * 10/1/91  Peter W. Farrett    Updated II_Psycho_One(),I_Psycho_One()*
  31.  *                              to include comments.                  *
  32.  *11/29/91  Masahiro Iwadare    Bug fix regarding POWERNORM           *
  33.  *                              fixed several other miscellaneous bugs*
  34.  * 2/11/92  W. Joseph Carter    Ported new code to Macintosh.  Most   *
  35.  *                              important fixes involved changing     *
  36.  *                              16-bit ints to long or unsigned in    *
  37.  *                              bit alloc routines for quant of 65535 *
  38.  *                              and passing proper function args.     *
  39.  *                              Removed "Other Joint Stereo" option   *
  40.  *                              and made bitrate be total channel     *
  41.  *                              bitrate, irrespective of the mode.    *
  42.  *                              Fixed many small bugs & reorganized.  *
  43.  * 2/12/92  Masahiro Iwadare    Fixed some potential bugs in          *
  44.  *          Davis Pan           subsampling()                         *
  45.  * 2/25/92  Masahiro Iwadare    Fixed some more potential bugs        *
  46.  * 6/24/92  Tan Ah Peng         Modified window for FFT               * 
  47.  *                              (denominator N-1 to N)                *
  48.  *                              Updated all critical band rate &      *
  49.  *                              absolute threshold tables and critical*
  50.  *                              boundaries for use with Layer I & II  *  
  51.  *                              Corrected boundary limits for tonal   *
  52.  *                              component computation                 *
  53.  *                              Placement of non-tonal component at   *
  54.  *                              geometric mean of critical band       *
  55.  *                              (previous placement method commented  *
  56.  *                               out - can be used if desired)        *
  57.  * 3/01/93  Mike Li             Infinite looping fix in noise_label() *
  58.  * 3/19/93  Jens Spille         fixed integer overflow problem in     *
  59.  *                              psychoacoutic model 1                 *
  60.  * 3/19/93  Giorgio Dimino      modifications to better account for   *
  61.  *                              tonal and non-tonal components        *
  62.  * 5/28/93 Sriram Jayasimha     "London" mod. to psychoacoustic model1*
  63.  * 8/05/93 Masahiro Iwadare     noise_label modification "option"     *
  64.  **********************************************************************/
  65.  
  66. #include "common.h"
  67. #include "encoder.h"
  68. #define LONDON                  /* enable "LONDON" modification */
  69. #define MAKE_SENSE              /* enable "MAKE_SENSE" modification */
  70. #define MI_OPTION               /* enable "MI_OPTION" modification */
  71. /**********************************************************************/
  72. /*
  73. /*        This module implements the psychoacoustic model I for the
  74. /* MPEG encoder layer II. It uses simplified tonal and noise masking
  75. /* threshold analysis to generate SMR for the encoder bit allocation
  76. /* routine.
  77. /*
  78. /**********************************************************************/
  79.  
  80. int crit_band;
  81. int FAR *cbound;
  82. int sub_size;
  83.  
  84. void read_cbound(lay,freq)  /* this function reads in critical */
  85. int lay, freq;              /* band boundaries                 */
  86. {
  87.  int i,j,k;
  88.  FILE *fp;
  89.  char r[16], t[80];
  90.  
  91.  strcpy(r, "2cb1");
  92.  r[0] = (char) lay + '0';
  93.  r[3] = (char) freq + '0';
  94.  if( !(fp = OpenTableFile(r)) ){       /* check boundary values */
  95.     printf("Please check %s boundary table\n",r);
  96.     exit(1);
  97.  }
  98.  fgets(t,80,fp);               /* read input for critical bands */
  99.  sscanf(t,"%d\n",&crit_band);
  100.  cbound = (int FAR *) mem_alloc(sizeof(int) * crit_band, "cbound");
  101.  for(i=0;i<crit_band;i++){   /* continue to read input for */
  102.     fgets(t,80,fp);            /* critical band boundaries   */
  103.     sscanf(t,"%d %d\n",&j, &k);
  104.     if(i==j) cbound[j] = k;
  105.     else {                     /* error */
  106.        printf("Please check index %d in cbound table %s\n",i,r);
  107.        exit(1);
  108.     }
  109.  }
  110.  fclose(fp);
  111. }        
  112.  
  113. void read_freq_band(ltg,lay,freq)  /* this function reads in   */
  114. int lay, freq;                     /* frequency bands and bark */
  115. g_ptr FAR *ltg;                /* values                   */
  116. {
  117.  int i,j, k;
  118.  double b,c;
  119.  FILE *fp;
  120.  char r[16], t[80];
  121.  
  122.  strcpy(r, "2th1");
  123.  r[0] = (char) lay + '0';
  124.  r[3] = (char) freq + '0';
  125.  if( !(fp = OpenTableFile(r)) ){   /* check freq. values  */
  126.     printf("Please check frequency and cband table %s\n",r);
  127.     exit(1);
  128.  }
  129.  fgets(t,80,fp);              /* read input for freq. subbands */
  130.  sscanf(t,"%d\n",&sub_size);
  131.  *ltg = (g_ptr FAR ) mem_alloc(sizeof(g_thres) * sub_size, "ltg");
  132.  (*ltg)[0].line = 0;          /* initialize global masking threshold */
  133.  (*ltg)[0].bark = 0;
  134.  (*ltg)[0].hear = 0;
  135.  for(i=1;i<sub_size;i++){    /* continue to read freq. subband */
  136.     fgets(t,80,fp);          /* and assign                     */
  137.     sscanf(t,"%d %d %lf %lf\n",&j, &k, &b, &c);
  138.     if(i == j){
  139.        (*ltg)[j].line = k;
  140.        (*ltg)[j].bark = b;
  141.        (*ltg)[j].hear = c;
  142.     }
  143.     else {                   /* error */
  144.        printf("Please check index %d in freq-cb table %s\n",i,r);
  145.        exit(1);
  146.     }
  147.  }
  148.  fclose(fp);
  149. }
  150.  
  151. void make_map(power, ltg)       /* this function calculates the */
  152. mask FAR power[HAN_SIZE];   /* global masking threshold     */
  153. g_thres FAR *ltg;
  154. {
  155.  int i,j;
  156.  
  157.  for(i=1;i<sub_size;i++) for(j=ltg[i-1].line;j<=ltg[i].line;j++)
  158.     power[j].map = i;
  159. }
  160.  
  161. double add_db(a,b)
  162. double a,b;
  163. {
  164.  a = pow(10.0,a/10.0);
  165.  b = pow(10.0,b/10.0);
  166.  return 10 * log10(a+b);
  167. }
  168.  
  169. /****************************************************************/
  170. /*
  171. /*        Fast Fourier transform of the input samples.
  172. /*
  173. /****************************************************************/
  174.  
  175. void II_f_f_t(sample, power)      /* this function calculates an */
  176. double FAR sample[FFT_SIZE];  /* FFT analysis for the freq.  */
  177. mask FAR power[HAN_SIZE];     /* domain                      */
  178. {
  179.  int i,j,k,L,l=0;
  180.  int ip, le, le1;
  181.  double t_r, t_i, u_r, u_i;
  182.  static int M, MM1, init = 0, N;
  183.  double *x_r, *x_i, *energy;
  184.  static int *rev;
  185.  static dou