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

  1. /**********************************************************************
  2. Copyright (c) 1991 MPEG/audio software simulation group, All Rights Reserved
  3. musicout.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  Douglas Wong        start of version 1.0 records          *
  15.  * 3/06/91  Douglas Wong        rename setup.h to dedef.h             *
  16.  *                              removed extraneous variables          *
  17.  *                              removed window_samples (now part of   *
  18.  *                              filter_samples)                       *
  19.  * 3/07/91  Davis Pan           changed output file to "codmusic"     *
  20.  * 5/10/91  Vish (PRISM)        Ported to Macintosh and Unix.         *
  21.  *                              Incorporated new "out_fifo()" which   *
  22.  *                              writes out last incomplete buffer.    *
  23.  *                              Incorporated all AIFF routines which  *
  24.  *                              are also compatible with SUN.         *
  25.  *                              Incorporated user interface for       *
  26.  *                              specifying sound file names.          *
  27.  *                              Also incorporated user interface for  *
  28.  *                              writing AIFF compatible sound files.  *
  29.  * 27jun91  dpwe (Aware)        Added musicout and &sample_frames as  *
  30.  *                              args to out_fifo (were glob refs).    *
  31.  *                              Used new 'frame_params' struct.       *
  32.  *                              Clean,simplify, track clipped output  *
  33.  *                              and total bits/frame received.        *
  34.  * 7/10/91  Earle Jennings      changed to floats to FLOAT            *
  35.  *10/ 1/91  S.I. Sudharsanan,   Ported to IBM AIX platform.           *
  36.  *          Don H. Lee,                                               *
  37.  *          Peter W. Farrett                                          *
  38.  *10/ 3/91  Don H. Lee          implemented CRC-16 error protection   *
  39.  *                              newly introduced functions are        *
  40.  *                              buffer_CRC and recover_CRC_error      *
  41.  *                              Additions and revisions are marked    *
  42.  *                              with "dhl" for clarity                *
  43.  * 2/11/92  W. Joseph Carter    Ported new code to Macintosh.  Most   *
  44.  *                              important fixes involved changing     *
  45.  *                              16-bit ints to long or unsigned in    *
  46.  *                              bit alloc routines for quant of 65535 *
  47.  *                              and passing proper function args.     *
  48.  *                              Removed "Other Joint Stereo" option   *
  49.  *                              and made bitrate be total channel     *
  50.  *                              bitrate, irrespective of the mode.    *
  51.  *                              Fixed many small bugs & reorganized.  *
  52.  *19 aug 92 Soren H. Nielsen    Changed MS-DOS file name extensions.  *
  53.  * 8/27/93 Seymour Shlien,      Fixes in Unix and MSDOS ports,        *
  54.  *         Daniel Lauzon, and                                         *
  55.  *         Bill Truerniet                                             *
  56.  **********************************************************************/
  57.  
  58. #include        "common.h"
  59. #include        "decoder.h"
  60.  
  61. #ifdef UNIX
  62. #include <time.h>
  63. #endif
  64.  
  65. /********************************************************************
  66. /*
  67. /*        This part contains the MPEG I decoder for Layers I & II.
  68. /*
  69. /*********************************************************************/
  70.  
  71. /****************************************************************
  72. /*
  73. /*        For MS-DOS user (Turbo c) change all instance of malloc
  74. /*        to _farmalloc and free to _farfree. Compiler model hugh
  75. /*        Also make sure all the pointer specified are changed to far.
  76. /*
  77. /*****************************************************************/
  78.  
  79. /*********************************************************************
  80. /*
  81. /* Core of the Layer II decoder.  Default layer is Layer II.
  82. /*
  83. /*********************************************************************/
  84.  
  85. /* Global variable definitions for "musicout.c" */
  86.  
  87. char *programName;
  88.  
  89. /* Implementations */
  90.  
  91. main(argc, argv)
  92. int argc;
  93. char **argv;
  94. {
  95. typedef short PCM[2][3][SBLIMIT];
  96.     PCM FAR *pcm_sample;
  97. typedef unsigned int SAM[2][3][SBLIMIT];
  98.     SAM FAR *sample;
  99. typedef double FRA[2][3][SBLIMIT];
  100.     FRA FAR *fraction;
  101. typedef double VE[2][HAN_SIZE];
  102.     VE FAR *w;
  103.  
  104.     Bit_stream_struc  bs;
  105.     frame_params      fr_ps;
  106.     layer             info;
  107.     FILE              *musicout;
  108.     unsigned long     sample_frames;
  109.  
  110.     int               i, j, k, stereo, done=FALSE, clip, sync;
  111.     int               error_protection, crc_error_count, total_error_count;
  112.     unsigned int      old_crc, new_crc;
  113.     unsigned int      bit_alloc[2][SBLIMIT], scfsi[2][SBLIMIT],
  114.                       scale_index[2][3][SBLIMIT];
  115.     unsigned long     bitsPerSlot, samplesPerFrame, frameNum = 0;
  116.     unsigned long     frameBits, gotBits = 0;
  117.     IFF_AIFF          pcm_aiff_data;
  118.     char              encoded_file_name[MAX_NAME_SIZE];
  119.     char              decoded_file_name[MAX_NAME_SIZE];
  120.     char              t[50];
  121.     int               need_aiff;
  122.     int topSb = 0;
  123.  
  124. #ifdef UNIX
  125.     clock_t time_to_decode = clock();
  126. #endif
  127.  
  128. #ifdef  MACINTOSH
  129.     console_options.nrows = MAC_WINDOW_SIZE;
  130.     argc = ccommand(&argv);
  131. #endif
  132.  
  133.     /* Most large variables are declared dynamically to ensure
  134.        compatibility with smaller machines */
  135.  
  136.     pcm_sample = (PCM FAR *) mem_alloc((long) sizeof(PCM), "PCM Samp");
  137.     sample = (SAM FAR *) mem_alloc((long) sizeof(SAM), "Sample");
  138.     fraction = (FRA FAR *) mem_alloc((long) sizeof(FRA), "fraction");
  139.     w = (VE FAR *) mem_alloc((long) sizeof(VE), "w");
  140.  
  141.     fr_ps.header = &info;
  142.     fr_ps.tab_num = -1;                /* no table loaded */
  143.     fr_ps.alloc = NULL;
  144.     for (i=0;i<HAN_SIZE;i++) for (j=0;j<2;j++) (*w)[j][i] = 0.0;
  145.  
  146.     programName = argv[0];
  147.     if(argc==1) {        /* no command line args -> interact */
  148.        do {
  149.           printf ("Enter encoded file name <required>: ");
  150.           gets (encoded_file_name);
  151.           if (encoded_file_name[0] == NULL_CHAR)
  152.              printf ("Encoded file name is required. \n");
  153.        } while (encoded_file_name[0] == NULL_CHAR);
  154.        printf (">>> Encoded file name is: %s \n", encoded_file_name);
  155. #ifdef  MS_DOS
  156.        printf ("Enter MPEG decoded file name <%s>: ",
  157.                new_ext(encoded_file_name, DFLT_OPEXT)); /* 92-08-19 shn */
  158. #else
  159.        printf ("Enter MPEG decoded file name <%s%s>: ", encoded_file_name,
  160.                DFLT_OPEXT);
  161. #endif
  162.        gets (decoded_file_name);
  163.        if (decoded_file_name[0] == NULL_CHAR) {
  164. #ifdef  MS_DOS
  165.            /* replace old extension with new one, 92-08-19 shn */
  166.            strcpy(decoded_file_name,new_ext(encoded_file_name, DFLT_OPEXT));
  167. #else
  168.            strcat (strcpy(decoded_file_name, encoded_file_name), DFLT_OPEXT);
  169. #endif
  170.        }
  171.        printf (">>> MPEG decoded file name is: %s \n", decoded_file_name);
  172.  
  173.        printf(
  174.           "Do you wish to write an AIFF compatible sound file ? (y/<n>) : ");
  175.        gets(t);
  176.        if (*t == 'y' || *t == 'Y') need_aiff = TRUE;
  177.        else                        need_aiff = FALSE;
  178.        if (need_aiff)
  179.             printf(">>> An AIFF compatible sound file will be written\n");
  180.        else printf(">>> A non-headered PCM sound file will be written\n"