home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 3 / TheARMClub_PDCD3.iso / hensa / converters / audiompeg_1 / !AudioMPEG / c / musicin < prev    next >
Text File  |  1995-05-14  |  24KB  |  585 lines

  1. /**********************************************************************
  2. Copyright (c) 1991 MPEG/audio software simulation group, All Rights Reserved
  3. musicin.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.  * 3/01/91  Douglas Wong,       start of version 1.1 records          *
  15.  *          Davis Pan                                                 *
  16.  * 3/06/91  Douglas Wong,       rename: setup.h to endef.h            *
  17.  *                              removed extraneous variables          *
  18.  * 3/21/91  J.Georges Fritsch   introduction of the bit-stream        *
  19.  *                              package. This package allows you      *
  20.  *                              to generate the bit-stream in a       *
  21.  *                              binary or ascii format                *
  22.  * 3/31/91  Bill Aspromonte     replaced the read of the SB matrix    *
  23.  *                              by an "code generated" one            *
  24.  * 5/10/91  W. Joseph Carter    Ported to Macintosh and Unix.         *
  25.  *                              Incorporated Jean-Georges Fritsch's   *
  26.  *                              "bitstream.c" package.                *
  27.  *                              Modified to strictly adhere to        *
  28.  *                              encoded bitstream specs, including    *
  29.  *                              "Berlin changes".                     *
  30.  *                              Modified user interface dialog & code *
  31.  *                              to accept any input & output          *
  32.  *                              filenames desired.  Also added        *
  33.  *                              de-emphasis prompt and final bail-out *
  34.  *                              opportunity before encoding.          *
  35.  *                              Added AIFF PCM sound file reading     *
  36.  *                              capability.                           *
  37.  *                              Modified PCM sound file handling to   *
  38.  *                              process all incoming samples and fill *
  39.  *                              out last encoded frame with zeros     *
  40.  *                              (silence) if needed.                  *
  41.  *                              Located and fixed numerous software   *
  42.  *                              bugs and table data errors.           *
  43.  * 27jun91  dpwe (Aware Inc)    Used new frame_params struct.         *
  44.  *                              Clear all automatic arrays.           *
  45.  *                              Changed some variable names,          *
  46.  *                              simplified some code.                 *
  47.  *                              Track number of bits actually sent.   *
  48.  *                              Fixed padding slot, stereo bitrate    *
  49.  *                              Added joint-stereo : scales L+R.      *
  50.  * 6/12/91  Earle Jennings      added fix for MS_DOS in obtain_param  *
  51.  * 6/13/91  Earle Jennings      added stack length adjustment before  *
  52.  *                              main for MS_DOS                       *
  53.  * 7/10/91  Earle Jennings      conversion of all float to FLOAT      *
  54.  *                              port to MsDos from MacIntosh completed*
  55.  * 8/ 8/91  Jens Spille         Change for MS-C6.00                   *
  56.  * 8/22/91  Jens Spille         new obtain_parameters()               *
  57.  *10/ 1/91  S.I. Sudharsanan,   Ported to IBM AIX platform.           *
  58.  *          Don H. Lee,                                               *
  59.  *          Peter W. Farrett                                          *
  60.  *10/ 3/91  Don H. Lee          implemented CRC-16 error protection   *
  61.  *                              newly introduced functions are        *
  62.  *                              I_CRC_calc, II_CRC_calc and encode_CRC*
  63.  *                              Additions and revisions are marked    *
  64.  *                              with "dhl" for clarity                *
  65.  *11/11/91 Katherine Wang       Documentation of code.                *
  66.  *                                (variables in documentation are     *
  67.  *                                surround by the # symbol, and an '*'*
  68.  *                                denotes layer I or II versions)     *
  69.  * 2/11/92  W. Joseph Carter    Ported new code to Macintosh.  Most   *
  70.  *                              important fixes involved changing     *
  71.  *                              16-bit ints to long or unsigned in    *
  72.  *                              bit alloc routines for quant of 65535 *
  73.  *                              and passing proper function args.     *
  74.  *                              Removed "Other Joint Stereo" option   *
  75.  *                              and made bitrate be total channel     *
  76.  *                              bitrate, irrespective of the mode.    *
  77.  *                              Fixed many small bugs & reorganized.  *
  78.  * 2/25/92  Masahiro Iwadare    made code cleaner and more consistent *
  79.  * 8/07/92  Mike Coleman        make exit() codes return error status *
  80.  *                              made slight changes for portability   *
  81.  *19 aug 92 Soren H. Nielsen    Changed MS-DOS file name extensions.  *
  82.  * 8/25/92  Shaun Astarabadi    Replaced rint() function with explicit*
  83.  *                              rounding for portability with MSDOS.  *
  84.  * 9/22/92  jddevine@aware.com  Fixed _scale_factor_calc() calls.     *
  85.  *10/19/92  Masahiro Iwadare    added info->mode and info->mode_ext   *
  86.  *                              updates for AIFF format files         *
  87.  * 3/10/93  Kevin Peterson      In parse_args, only set non default   *
  88.  *                              bit rate if specified in arg list.    *
  89.  *                              Use return value from aiff_read_hdrs  *
  90.  *                              to fseek to start of sound data       *
  91.  * 7/26/93  Davis Pan           fixed bug in printing info->mode_ext  *
  92.  *                              value for joint stereo condition      *
  93.  * 8/27/93 Seymour Shlien,      Fixes in Unix and MSDOS ports,        *
  94.  *         Daniel Lauzon, and                                         *
  95.  *         Bill Truerniet                                             *
  96.  **********************************************************************/
  97.  
  98. #include "common.h"
  99. #include "encoder.h"
  100.  
  101. /* Global variable definitions for "musicin.c" */
  102.  
  103. FILE               *musicin;
  104. Bit_stream_struc   bs;
  105. char               *programName;
  106.  
  107. /* Implementations */
  108.  
  109.  
  110. /************************************************************************
  111.  *
  112.  * parse_args
  113.  *
  114.  * PURPOSE:  Sets encoding parameters to the specifications of the
  115.  * command line.  Default settings are used for parameters
  116.  * not specified in the command line.
  117.  *
  118.  * SEMANTICS:  The command line is parsed according to the following
  119.  * syntax:
  120.  *
  121.  * -l  is followed by the layer number
  122.  * -m  is followed by the mode
  123.  * -p  is followed by the psychoacoustic model number
  124.  * -s  is followed by the sampling rate
  125.  * -b  is followed by the total bitrate, irrespective of the mode
  126.  * -d  is followed by the emphasis flag
  127.  * -c  is followed by the copyright/no_copyright flag
  128.  * -o  is followed by the original/not_original flag 
  129.  * -e  is followed by the error_protection on/off flag
  130.  *
  131.  * If the input file is in AIFF format, the sampling frequency is read
  132.  * from the AIFF header.
  133.  *
  134.  * The input and output filenames are read into #inpath# and #outpath#.
  135.  *
  136.  ************************************************************************/
  137.  
  138. void
  139. parse_args(argc, argv, fr_ps, psy, num_samples, inPath, outPath)
  140. int     argc;
  141. char    **argv;
  142. frame_params  *fr_ps;
  143. int     *psy;
  144. unsigned long *num_samples;
  145. char    inPath[MAX_NAME_SIZE];
  146. char    outPath[MAX_NAME_SIZE];
  147. {
  148.    FLOAT srate;
  149.    int   brate;
  150.    layer *info = fr_ps->header;
  151.    int   err = 0, i = 0;
  152.    long samplerate;
  153.    long soundPosition;
  154.  
  155.    /* preset defaults */
  156.    inPath[0] = '\0';   outPath[0] = '\0';
  157.    info->lay = DFLT_LAY;
  158.    switch(DFLT_MOD) {
  159.       case 's': info->mode = MPG_MD_STEREO; info->mode_ext = 0; break;
  160.       case 'd': info->mode = MPG_MD_DUAL_CHANNEL; info->mode_ext=0; break;
  161.       case 'j': info->mode = MPG_MD_JOINT_STEREO; break;
  162.       case 'm': info->mode = MPG_MD_MONO; info->mode_ext = 0; break;
  163.       default:
  164.          fprintf(stderr, "%s: Bad mode dflt %c\n", programName, DFLT_MOD);
  165.          abort();
  166.    }
  167.    *psy = DFLT_PSY;
  168.    if((info->sampling_frequency = SmpFrqIndex((long)(1000*DFLT_SFQ))) < 0) {
  169.       fprintf(stderr, "%s: bad sfrq default %.2f\n", programName, DFLT_SFQ);
  170.       abort();
  171.    }
  172.    if((info->bitrate_index = BitrateIndex(info->lay, DFLT_BRT)) < 0) {
  173.       fprintf(stderr, "%s: bad default bitrate %u\n", programName, DFLT_BRT);
  174.       abort();
  175.    }
  176.    switch(DFLT_EMP) {
  177.       case 'n': info->emphasis = 0; break;
  178.       case '5': info->emphasis = 1; break;
  179.       case 'c': info->emphasis = 3; break;
  180.       default: 
  181.          fprintf(stderr, "%s: Bad emph dflt %c\n", programName, DFLT_EMP);
  182.          abort();
  183.    }
  184.    info->copyright = 0; info->original = 0; info->error_protection = FALSE;
  185.  
  186.    /* process args */
  187.    while(++i<argc && err == 0) {
  188.       char c, *token, *arg, *nextArg;
  189.       int  argUsed;
  190.  
  191.       token = argv[i];
  192.       if(*token++ == '-') {
  193.          if(i+1 < argc) nextArg = argv[i+1];
  194.          else           nextArg = "";
  195.          argUsed = 0;
  196.          while(c = *token++) {
  197.             if(*token /* NumericQ(token) */) arg = token;
  198.             else                             arg = nextArg;
  199.             switch(c) {
  200.                case 'l':        info->lay = atoi(arg); argUsed = 1;
  201.                   if(info->lay<1 || info->lay>2) {
  202.                      fprintf(stderr,"%s: -l layer must be 1 or 2, not %s\n",
  203.                           programName, arg);
  204.                      err = 1;
  205.                   }
  206.                   break;
  207.                case 'm':        argUsed = 1;
  208.                   if (*arg == 's')
  209.                     { info->mode = MPG_MD_STEREO; info->mode_ext = 0; }
  210.                   else if (*arg == 'd')
  211.                     { info->mode = MPG_MD_DUAL_CHANNEL; info->mode_ext=0; }
  212.                   else if (*arg == 'j')
  213.                     { info->mode = MPG_MD_JOINT_STEREO; }
  214.                   else if (*arg == 'm')
  215.                     { info->mode = MPG_MD_MONO; info->mode_ext = 0; }
  216.                   else {
  217.                     fprintf(stderr,"%s: -m mode must be s/d/j/m not %s\n",
  218.                             programName, arg);
  219.                     err = 1;
  220.                   }
  221.                   break;
  222.                case 'p':        *psy = atoi(arg); argUsed = 1;
  223.                   if(*psy<1 || *psy>2) {
  224.                      fprintf(stderr,"%s: -p model must be 1 or 2, not %s\n",
  225.                              programName, arg);
  226.                      err = 1;
  227.                   }
  228.                   break;
  229.  
  230.                case 's':
  231.                   argUsed = 1;
  232.                   srate = atof( arg );
  233.                   /* samplerate = rint( 1000.0 * srate ); $A  */
  234.                   samplerate = (long) (( 1000.0 * srate ) + 0.5);
  235.                   if( (info->sampling_frequency = SmpFrqIndex((long) samplerate)) < 0 )
  236.                       err = 1;
  237.                   break;
  238.                   
  239.                case 'b':        
  240.              argUsed = 1;
  241.           brate = atoi(arg); 
  242.           if( (info->bitrate_index = BitrateIndex(info->lay, brate)) < 0)
  243.              err=1;                  
  244.          break;
  245.                case 'd':        argUsed = 1;
  246.                   if (*arg == 'n')                    info->emphasis = 0;
  247.                   else if (*arg == '5')               info->emphasis = 1;
  248.                   else if (*arg == 'c')               info->emphasis = 3;
  249.                   else {
  250.                      fprintf(stderr,"%s: -d emp must be n/5/c not %s\n",
  251.                              programName, arg);
  252.                      err = 1;
  253.                   }
  254.                   break;
  255.                 case 'c':       info->copyright = 1; break;
  256.                 case 'o':       info->original  = 1; break;
  257.                 case 'e':       info->error_protection = TRUE; break;
  258.                 default:        fprintf(stderr,"%s: unrec option %c\n",
  259.                                         programName, c);
  260.                                 err = 1; break;
  261.             }
  262.             if(argUsed) {
  263.                if(arg == token)    token = "";   /* no more from token */
  264.                else                ++i;          /* skip arg we used */
  265.                arg = ""; argUsed = 0;
  266.             }
  267.          }
  268.       }
  269.       else {
  270.          if(inPath[0] == '\0')       strcpy(inPath, argv[i]);
  271.          else if(outPath[0] == '\0') strcpy(outPath, argv[i]);
  272.          else {
  273.             fprintf(stderr,"%s: excess arg %s\n", programName, argv[i]);
  274.             err = 1;
  275.          }
  276.       }
  277.    }
  278.  
  279.    if(err || inPath[0] == '\0') usage();  /* never returns */
  280.  
  281.    if(outPath[0] == '\0') {
  282.       strcpy(outPath, inPath);
  283.       strcat(outPath, DFLT_EXT);
  284.    }
  285.  
  286.    if ((musicin = fopen(inPath, "rb")) == NULL) {
  287.       printf("Could not find \"%s\".\n", inPath);
  288.       exit(1);
  289.    }
  290.  
  291.    open_bit_stream_w(&bs, outPath, BUFFER_SIZE);
  292.  
  293.    if (fseek(musicin, 0, SEEK_SET) != 0) {
  294.       printf("Could not seek to PCM sound data in \"%s\".\n", inPath);
  295.       exit(1);
  296.    }
  297.  
  298.    /* Declare sound file to have "infinite" number of samples. */
  299.    *num_samples = MAX_U_32_NUM;
  300.  
  301. }
  302.  
  303.  
  304.  
  305. /************************************************************************
  306.  *
  307.  * main
  308.  *
  309.  * PURPOSE:  MPEG I Encoder supporting layers 1 and 2, and
  310.  * psychoacoustic models 1 (MUSICAM) and 2 (AT&T)
  311.  *
  312.  * SEMANTICS:  One overlapping frame of audio of up to 2 channels are
  313.  * processed at a time in the following order:
  314.  * (associated routines are in parentheses)
  315.  *
  316.  * 1.  Filter sliding window of data to get 32 subband
  317.  * samples per channel.
  318.  * (window_subband,filter_subband)
  319.  *
  320.  * 2.  If joint stereo mode, combine left and right channels
  321.  * for subbands above #jsbound#.
  322.  * (*_combine_LR)
  323.  *
  324.  * 3.  Calculate scalefactors for the frame, and if layer 2,
  325.  * also calculate scalefactor select information.
  326.  * (*_scale_factor_calc)
  327.  *
  328.  * 4.  Calculate psychoacoustic masking levels using selected
  329.  * psychoacoustic model.
  330.  * (*_Psycho_One, psycho_anal)
  331.  *
  332.  * 5.  Perform iterative bit allocation for subbands with low
  333.  * mask_to_noise ratios using masking levels from step 4.
  334.  * (*_main_bit_allocation)
  335.  *
  336.  * 6.  If error protection flag is active, add redundancy for
  337.  * error protection.
  338.  * (*_CRC_calc)
  339.  *
  340.  * 7.  Pack bit allocation, scalefactors, and scalefactor select
  341.  * information (layer 2) onto bitstream.
  342.  * (*_encode_bit_alloc,*_encode_scale,II_transmission_pattern)
  343.  *
  344.  * 8.  Quantize subbands and pack them into bitstream
  345.  * (*_subband_quantization, *_sample_encoding)
  346.  *
  347.  ************************************************************************/
  348.  
  349. main(argc, argv)
  350. int     argc;
  351. char    **argv;
  352. {
  353. typedef double SBS[2][3][SCALE_BLOCK][SBLIMIT];
  354.     SBS  FAR        *sb_sample;
  355. typedef double JSBS[3][SCALE_BLOCK][SBLIMIT];
  356.     JSBS FAR        *j_sample;
  357. typedef double IN[2][HAN_SIZE];
  358.     IN   FAR        *win_que;
  359. typedef unsigned int SUB[2][3][SCALE_BLOCK][SBLIMIT];
  360.     SUB  FAR        *subband;
  361.  
  362.     frame_params fr_ps;
  363.     layer info;
  364.     char original_file_name[MAX_NAME_SIZE];
  365.     char encoded_file_name[MAX_NAME_SIZE];
  366.     short FAR **win_buf;
  367. static short FAR buffer[2][1152];
  368. static unsigned int bit_alloc[2][SBLIMIT], scfsi[2][SBLIMIT];
  369. static unsigned int scalar[2][3][SBLIMIT], j_scale[3][SBLIMIT];
  370. static double FAR ltmin[2][SBLIMIT], lgmin[2][SBLIMIT], max_sc[2][SBLIMIT];
  371.     FLOAT snr32[32];
  372.     short sam[2][1056];
  373.     int whole_SpF, extra_slot = 0;
  374.     double avg_slots_per_frame, frac_SpF, slot_lag;
  375.     int model, stereo, error_protection;
  376.     static unsigned int crc;
  377.     int i, j, k, adb;
  378.     unsigned long bitsPerSlot, samplesPerFrame, frameNum = 0;
  379.     unsigned long frameBits, sentBits = 0;
  380.     unsigned long num_samples;
  381.  
  382.     /* Most large variables are declared dynamically to ensure
  383.        compatibility with smaller machines */
  384.  
  385.     sb_sample = (SBS FAR *) mem_alloc(sizeof(SBS), "sb_sample");
  386.     j_sample = (JSBS FAR *) mem_alloc(sizeof(JSBS), "j_sample");
  387.     win_que = (IN FAR *) mem_alloc(sizeof(IN), "Win_que");
  388.     subband = (SUB FAR *) mem_alloc(sizeof(SUB),"subband");
  389.     win_buf = (short FAR **) mem_alloc(sizeof(short *)*2, "win_buf");
  390.  
  391.     /* clear buffers */
  392.     memset((char *) buffer, 0, sizeof(buffer));
  393.     memset((char *) bit_alloc, 0, sizeof(bit_alloc));
  394.     memset((char *) scalar, 0, sizeof(scalar));
  395.     memset((char *) j_scale, 0, sizeof(j_scale));
  396.     memset((char *) scfsi, 0, sizeof(scfsi));
  397.     memset((char *) ltmin, 0, sizeof(ltmin));
  398.     memset((char *) lgmin, 0, sizeof(lgmin));
  399.     memset((char *) max_sc, 0, sizeof(max_sc));
  400.     memset((char *) snr32, 0, sizeof(snr32));
  401.     memset((char *) sam, 0, sizeof(sam));
  402.  
  403.     fr_ps.header = &info;
  404.     fr_ps.tab_num = -1;             /* no table loaded */
  405.     fr_ps.alloc = NULL;
  406.     info.version = MPEG_AUDIO_ID;
  407.  
  408.     programName = argv[0];
  409.     parse_args(argc, argv, &fr_ps, &model, &num_samples,
  410.                original_file_name, encoded_file_name);
  411.  
  412.     hdr_to_frps(&fr_ps);
  413.     stereo = fr_ps.stereo;
  414.     error_protection = info.error_protection;
  415.  
  416.     if (info.lay == 1) { bitsPerSlot = 32; samplesPerFrame = 384;  }
  417.     else               { bitsPerSlot = 8;  samplesPerFrame = 1152; }
  418.     /* Figure average number of 'slots' per frame. */
  419.     /* Bitrate means TOTAL for both channels, not per side. */
  420.     avg_slots_per_frame = ((double)samplesPerFrame /
  421.                            s_freq[info.sampling_frequency]) *
  422.                           ((double)bitrate[info.lay-1][info.bitrate_index] /
  423.                            (double)bitsPerSlot);
  424.     whole_SpF = (int) avg_slots_per_frame;
  425.     frac_SpF  = avg_slots_per_frame - (double)whole_SpF;
  426.     slot_lag  = -frac_SpF;
  427.  
  428.     if (frac_SpF == 0) info.padding = 0;
  429.  
  430.     while (get_audio(musicin, buffer, num_samples, stereo, info.lay) > 0) {
  431.  
  432.        fprintf(stderr, "{%4lu}", frameNum++); fflush(stderr);
  433.        win_buf[0] = &buffer[0][0];
  434.        win_buf[1] = &buffer[1][0];
  435.        if (frac_SpF != 0) {
  436.           if (slot_lag > (frac_SpF-1.0) ) {
  437.              slot_lag -= frac_SpF;
  438.              extra_slot = 0;
  439.              info.padding = 0;
  440.              /*  printf("No padding for this frame\n"); */
  441.           }
  442.           else {
  443.              extra_slot = 1;
  444.              info.padding = 1;
  445.              slot_lag += (1-frac_SpF);
  446.              /*  printf("Padding for this frame\n");    */
  447.           }
  448.        }
  449.        adb = (whole_SpF+extra_slot) * bitsPerSlot;
  450.  
  451.        switch (info.lay) {
  452.  
  453. /***************************** Layer I **********************************/
  454.  
  455.           case 1 :
  456.              for (j=0;j<SCALE_BLOCK;j++)
  457.              for (k=0;k<stereo;k++) {
  458.                 window_subband(&win_buf[k], &(*win_que)[k][0], k);
  459.                 filter_subband(&(*win_que)[k][0], &(*sb_sample)[k][0][j][0]);
  460.              }
  461.  
  462.              I_scale_factor_calc(*sb_sample, scalar, stereo);
  463.              if(fr_ps.actual_mode == MPG_MD_JOINT_STEREO) {
  464.                 I_combine_LR(*sb_sample, *j_sample);
  465.                 I_scale_factor_calc(j_sample, &j_scale, 1);
  466.              }
  467.  
  468.              put_scale(scalar, &fr_ps, max_sc);
  469.  
  470.              if (model == 1) I_Psycho_One(buffer, max_sc, ltmin, &fr_ps);
  471.              else {
  472.                 for (k=0;k<stereo;k++) {
  473.                    psycho_anal(&buffer[k][0],&sam[k][0], k, info.lay, snr32,
  474.                                (FLOAT)s_freq[info.sampling_frequency]*1000);
  475.                    for (i=0;i<SBLIMIT;i++) ltmin[k][i] = (double) snr32[i];
  476.                 }
  477.              }
  478.  
  479.              I_main_bit_allocation(ltmin, bit_alloc, &adb, &fr_ps);
  480.  
  481.              if (error_protection) I_CRC_calc(&fr_ps, bit_alloc, &crc);
  482.  
  483.              encode_info(&fr_ps, &bs);
  484.  
  485.              if (error_protection) encode_CRC(crc, &bs);
  486.  
  487.              I_encode_bit_alloc(bit_alloc, &fr_ps, &bs);
  488.              I_encode_scale(scalar, bit_alloc, &fr_ps, &bs);
  489.              I_subband_quantization(scalar, *sb_sample, j_scale, *j_sample,
  490.                                     bit_alloc, *subband, &fr_ps);
  491.              I_sample_encoding(*subband, bit_alloc, &fr_ps, &bs);
  492.              for (i=0;i<adb;i++) put1bit(&bs, 0);
  493.           break;
  494.  
  495. /***************************** Layer 2 **********************************/
  496.  
  497.           case 2 :
  498.              for (i=0;i<3;i++) for (j=0;j<SCALE_BLOCK;j++)
  499.                 for (k=0;k<stereo;k++) {
  500.                    window_subband(&win_buf[k], &(*win_que)[k][0], k);
  501.                    filter_subband(&(*win_que)[k][0], &(*sb_sample)[k][i][j][0]);
  502.                 }
  503.  
  504.                 II_scale_factor_calc(*sb_sample, scalar, stereo, fr_ps.sblimit);
  505.                 pick_scale(scalar, &fr_ps, max_sc);
  506.                 if(fr_ps.actual_mode == MPG_MD_JOINT_STEREO) {
  507.                    II_combine_LR(*sb_sample, *j_sample, fr_ps.sblimit);
  508.                    II_scale_factor_calc(j_sample, &j_scale, 1, fr_ps.sblimit);
  509.                 }       /* this way we calculate more mono than we need */
  510.                         /* but it is cheap */
  511.  
  512.                 if (model == 1) II_Psycho_One(buffer, max_sc, ltmin, &fr_ps);
  513.                 else {
  514.                    for (k=0;k<stereo;k++) {
  515.                       psycho_anal(&buffer[k][0],&sam[k][0], k, 
  516.                                  info.lay, snr32,
  517.                                  (FLOAT)s_freq[info.sampling_frequency]*1000);
  518.                       for (i=0;i<SBLIMIT;i++) ltmin[k][i] = (double) snr32[i];
  519.                    }
  520.                 }
  521.  
  522.                 II_transmission_pattern(scalar, scfsi, &fr_ps);
  523.                 II_main_bit_allocation(ltmin, scfsi, bit_alloc, &adb, &fr_ps);
  524.  
  525.                 if (error_protection)
  526.                    II_CRC_calc(&fr_ps, bit_alloc, scfsi, &crc);
  527.  
  528.                 encode_info(&fr_ps, &bs);
  529.  
  530.                 if (error_protection) encode_CRC(crc, &bs);
  531.  
  532.                 II_encode_bit_alloc(bit_alloc, &fr_ps, &bs);
  533.                 II_encode_scale(bit_alloc, scfsi, scalar, &fr_ps, &bs);
  534.                 II_subband_quantization(scalar, *sb_sample, j_scale,
  535.                                       *j_sample, bit_alloc, *subband, &fr_ps);
  536.                 II_sample_encoding(*subband, bit_alloc, &fr_ps, &bs);
  537.                 for (i=0;i<adb;i++) put1bit(&bs, 0);
  538.           break;
  539.  
  540. /***************************** Layer 3 **********************************/
  541.  
  542.           case 3 : break;
  543.  
  544.        }
  545.  
  546.        frameBits = sstell(&bs) - sentBits;
  547.        if(frameBits%bitsPerSlot)   /* a program failure */
  548.           fprintf(stderr,"Sent %ld bits = %ld slots plus %ld\n",
  549.                   frameBits, frameBits/bitsPerSlot,
  550.                   frameBits%bitsPerSlot);
  551.        sentBits += frameBits;
  552.  
  553.     }
  554.  
  555.     close_bit_stream_w(&bs);
  556.  
  557.     if (fclose(musicin) != 0){
  558.        printf("Could not close \"%s\".\n", original_file_name);
  559.        exit(2);
  560.     }
  561.  
  562.     exit(0);
  563. }
  564.  
  565. /************************************************************************
  566.  *
  567.  * usage
  568.  *
  569.  * PURPOSE:  Writes command line syntax to the file specified by #stderr#
  570.  *
  571.  ************************************************************************/
  572.  
  573. void usage()  /* print syntax & exit */
  574. {
  575.     fprintf(stderr, "MPEG-1 Audio encoder 0.01\n\n");
  576.     fprintf(stderr, "Ported by Henrik Bjerregaard Pedersen, 14-May-1995\n\n");
  577.     fprintf(stderr, "Usage:   Encode  [switches] <infile> <outfile>\n");
  578.     fprintf(stderr, "Switches:\n");
  579.     fprintf(stderr, "  -m <mode>      mode (s for stereo, m for mono)\n");
  580.     fprintf(stderr, "  -s <freq>      input sample rate (32, 44.1 or 48)\n");
  581.     fprintf(stderr, "  -b <kbps>      output bit rate in kbps\n");
  582.     exit(1);
  583. }
  584.  
  585.