home *** CD-ROM | disk | FTP | other *** search
/ Da Capo / da_capo_vol1.bin / programs / amiga / misc / mpegaudio / common.c next >
C/C++ Source or Header  |  1994-03-21  |  48KB  |  1,540 lines

  1. /**********************************************************************
  2. Copyright (c) 1991 MPEG/audio software simulation group, All Rights Reserved
  3. common.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 4.0                                                        *
  12.  *   changes made since last update:                                  *
  13.  *   date   programmers         comment                               *
  14.  * 2/25/91  Doulas Wong,        start of version 1.0 records          *
  15.  *          Davis Pan                                                 *
  16.  * 5/10/91  W. Joseph Carter    Created this file for all common      *
  17.  *                              functions and global variables.       *
  18.  *                              Ported to Macintosh and Unix.         *
  19.  *                              Added Jean-Georges Fritsch's          *
  20.  *                              "bitstream.c" package.                *
  21.  *                              Added routines to handle AIFF PCM     *
  22.  *                              sound files.                          *
  23.  *                              Added "mem_alloc()" and "mem_free()"  *
  24.  *                              routines for memory allocation        *
  25.  *                              portability.                          *
  26.  *                              Added routines to convert between     *
  27.  *                              Apple SANE extended floating point    *
  28.  *                              format and IEEE double precision      *
  29.  *                              floating point format.  For AIFF.     *
  30.  * 02jul91 dpwe (Aware Inc)     Moved allocation table input here;    *
  31.  *                              Tables read from subdir TABLES_PATH.  *
  32.  *                              Added some debug printout fns (Write*)*
  33.  * 7/10/91 Earle Jennings       replacement of the one float by FLOAT *
  34.  *                              port to MsDos from MacIntosh version  *
  35.  * 8/ 5/91 Jean-Georges Fritsch fixed bug in open_bit_stream_r()      *
  36.  *10/ 1/91 S.I. Sudharsanan,    Ported to IBM AIX platform.           *
  37.  *         Don H. Lee,                                                *
  38.  *         Peter W. Farrett                                           *
  39.  *10/3/91  Don H. Lee           implemented CRC-16 error protection   *
  40.  *                              newly introduced functions are        *
  41.  *                              I_CRC_calc, II_CRC_calc and           *
  42.  *                              update_CRC. Additions and revisions   *
  43.  *                              are marked with dhl for clarity       *
  44.  *10/18/91 Jean-Georges Fritsch fixed bug in update_CRC(),            *
  45.  *                              II_CRC_calc() and I_CRC_calc()        *
  46.  * 2/11/92  W. Joseph Carter    Ported new code to Macintosh.  Most   *
  47.  *                              important fixes involved changing     *
  48.  *                              16-bit ints to long or unsigned in    *
  49.  *                              bit alloc routines for quant of 65535 *
  50.  *                              and passing proper function args.     *
  51.  *                              Removed "Other Joint Stereo" option   *
  52.  *                              and made bitrate be total channel     *
  53.  *                              bitrate, irrespective of the mode.    *
  54.  *                              Fixed many small bugs & reorganized.  *
  55.  * 3/20/92 Jean-Georges Fritsch  fixed bug in start-of-frame search   *
  56.  * 6/15/92 Juan Pineda          added refill_buffer(bs) "n"           *
  57.  *                              initialization                        *
  58.  * 7/08/92 Susanne Ritscher     MS-DOS, MSC6.0 port fixes             *
  59.  * 7/27/92 Mike Li               (re-)Port to MS-DOS                  *
  60.  * 8/19/92 Soren H. Nielsen     Fixed bug in I_CRC_calc and in        *
  61.  *                              II_CRC_calc.  Added function: new_ext *
  62.  *                              for better MS-DOS compatability       *
  63.  * 3/10/93 Kevin Peterson       changed aiff_read_headers to handle   *
  64.  *                              chunks in any order.  now returns     *
  65.  *                              position of sound data in file.       *
  66.  * 3/31/93 Jens Spille          changed IFF_* string compares to use  *
  67.  *                              strcmp()                              *
  68.  * 5/30/93 Masahiro Iwadare    ?? the previous modification does not *
  69.  *                  work. recovered to the original. ?? *
  70.  * 8/27/93 Seymour Shlien,      Fixes in Unix and MSDOS ports,        *
  71.  *         Daniel Lauzon, and                                         *
  72.  *         Bill Truerniet                                             *
  73.  **********************************************************************/
  74.  
  75. /***********************************************************************
  76. *
  77. *  Global Include Files
  78. *
  79. ***********************************************************************/
  80.  
  81. #include        "common.h"
  82.  
  83. #ifdef  MACINTOSH
  84.  
  85. #include        <SANE.h>
  86. #include        <pascal.h>
  87.  
  88. #endif
  89.  
  90. #include <ctype.h>
  91.  
  92. /***********************************************************************
  93. *
  94. *  Global Variable Definitions
  95. *
  96. ***********************************************************************/
  97.  
  98. char *mode_names[4] = { "stereo", "j-stereo", "dual-ch", "single-ch" };
  99. char *layer_names[3] = { "I", "II", "III" };
  100.  
  101. double  s_freq[4] = {44.1, 48, 32, 0};
  102.  
  103. int     bitrate[3][15] = {
  104.           {0,32,64,96,128,160,192,224,256,288,320,352,384,416,448},
  105.           {0,32,48,56,64,80,96,112,128,160,192,224,256,320,384},
  106.           {0,32,40,48,56,64,80,96,112,128,160,192,224,256,320}
  107.         };
  108.  
  109. double FAR multiple[64] = {
  110. 2.00000000000000, 1.58740105196820, 1.25992104989487,
  111. 1.00000000000000, 0.79370052598410, 0.62996052494744, 0.50000000000000,
  112. 0.39685026299205, 0.31498026247372, 0.25000000000000, 0.19842513149602,
  113. 0.15749013123686, 0.12500000000000, 0.09921256574801, 0.07874506561843,
  114. 0.06250000000000, 0.04960628287401, 0.03937253280921, 0.03125000000000,
  115. 0.02480314143700, 0.01968626640461, 0.01562500000000, 0.01240157071850,
  116. 0.00984313320230, 0.00781250000000, 0.00620078535925, 0.00492156660115,
  117. 0.00390625000000, 0.00310039267963, 0.00246078330058, 0.00195312500000,
  118. 0.00155019633981, 0.00123039165029, 0.00097656250000, 0.00077509816991,
  119. 0.00061519582514, 0.00048828125000, 0.00038754908495, 0.00030759791257,
  120. 0.00024414062500, 0.00019377454248, 0.00015379895629, 0.00012207031250,
  121. 0.00009688727124, 0.00007689947814, 0.00006103515625, 0.00004844363562,
  122. 0.00003844973907, 0.00003051757813, 0.00002422181781, 0.00001922486954,
  123. 0.00001525878906, 0.00001211090890, 0.00000961243477, 0.00000762939453,
  124. 0.00000605545445, 0.00000480621738, 0.00000381469727, 0.00000302772723,
  125. 0.00000240310869, 0.00000190734863, 0.00000151386361, 0.00000120155435,
  126. 1E-20
  127. };
  128.  
  129. /***********************************************************************
  130. *
  131. *  Global Function Definitions
  132. *
  133. ***********************************************************************/
  134.  
  135. /* The system uses a variety of data files.  By opening them via this
  136.    function, we can accommodate various locations. */
  137.  
  138. FILE *OpenTableFile(name)
  139. char *name;
  140. {
  141. char fulname[80];
  142. char *envdir;
  143. FILE *f;
  144.  
  145.      fulname[0] = '\0';
  146.  
  147. #ifdef TABLES_PATH
  148.        strcpy(fulname, TABLES_PATH);   /* default relative path for tables */
  149. #endif /* TABLES_PATH */          /* (includes terminal path seperator */
  150.  
  151. #ifdef UNIX                       /* envir. variables for UNIX only */
  152.        {
  153.         char *getenv();
  154.  
  155.         envdir = getenv(MPEGTABENV);   /* check for environment */
  156.         if(envdir != NULL)
  157.             strcpy(fulname, envdir);
  158.         strcat(fulname, PATH_SEPARATOR);  /* add a "/" on the end */
  159.       }
  160. #endif /* UNIX */
  161.  
  162.     strcat(fulname, name);
  163.     if( (f=fopen(fulname,"r