home *** CD-ROM | disk | FTP | other *** search
/ MPEG Toolkit / MPEG Toolkit.iso / dos / ampeg43 / source / decodsrc / musicout.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-01  |  24.0 KB  |  580 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, 708-538-5671, e-mail: pan@ukraine.corp.mot.com        *
  10.  *                                                                    *
  11.  * VERSION 4.3                                                        *
  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.  * 4/23/92  J. Pineda           Added code for layer III.  LayerIII   *
  58.  *          Amit Gulati         decoding is currently performed in    *
  59.  *                              two-passes for ease of sideinfo and   *
  60.  *                              maindata buffering and decoding.      *
  61.  *                              The second (computation) pass is      *
  62.  *                              activated with "decode -3 <outfile>"  *
  63.  * 10/25/92 Amit Gulati         Modified usage() for layerIII         *
  64.  * 12/10/92 Amit Gulati         Changed processing order of re-order- *
  65.  *                              -ing step.  Fixed adjustment of       *
  66.  *                              main_data_end pointer to exclude      *
  67.  *                              side information.                     *
  68.  *  9/07/93 Toshiyuki Ishino    Integrated Layer III with Ver 3.9.    *
  69.  *--------------------------------------------------------------------*
  70.  * 11/20/93 Masahiro Iwadare    Integrated Layer III with Ver 4.0.    *
  71.  *--------------------------------------------------------------------*
  72.  *  7/14/94 Juergen Koller      Bug fixes in Layer III code           *
  73.  *--------------------------------------------------------------------*
  74.  * 08/11/94 IIS                 Bug fixes in Layer III code           *
  75.  *--------------------------------------------------------------------*
  76.  * 11/04/94 Jon Rowlands        Prototype fixes                       *
  77.  **********************************************************************/
  78.  
  79. #include        "common.h"
  80. #include        "decoder.h"
  81.  
  82. /********************************************************************
  83. /*
  84. /*        This part contains the MPEG I decoder for Layers I & II.
  85. /*
  86. /*********************************************************************/
  87.  
  88. /****************************************************************
  89. /*
  90. /*        For MS-DOS user (Turbo c) change all instance of malloc
  91. /*        to _farmalloc and free to _farfree. Compiler model hugh
  92. /*        Also make sure all the pointer specified are changed to far.
  93. /*
  94. /*****************************************************************/
  95.  
  96. /*********************************************************************
  97. /*
  98. /* Core of the Layer II decoder.  Default layer is Layer II.
  99. /*
  100. /*********************************************************************/
  101.  
  102. /* Global variable definitions for "musicout.c" */
  103.  
  104. char *programName;
  105. int main_data_slots();
  106. int side_info_slots();
  107.  
  108. /* Implementations */
  109.  
  110. main(argc, argv)
  111. int argc;
  112. char **argv;
  113. {
  114. /*typedef short PCM[2][3][SBLIMIT];*/
  115. typedef short PCM[2][SSLIMIT][SBLIMIT];
  116.     PCM FAR *pcm_sample;
  117. typedef unsigned int SAM[2][3][SBLIMIT];
  118.     SAM FAR *sample;
  119. typedef double FRA[2][3][SBLIMIT];
  120.     FRA FAR *fraction;
  121. typedef double VE[2][HAN_SIZE];
  122.     VE FAR *w;
  123.  
  124.     Bit_stream_struc  bs;
  125.     frame_params      fr_ps;
  126.     layer             info;
  127.     FILE              *musicout;
  128.     unsigned long     sample_frames;
  129.  
  130.     int               i, j, k, x, stereo, done=FALSE, clip, sync;
  131.     int               error_protection, crc_error_count, total_error_count;
  132.     unsigned int      old_crc, new_crc;
  133.     unsigned int      bit_alloc[2][SBLIMIT], scfsi[2][SBLIMIT],
  134.                       scale_index[2][3][SBLIMIT];
  135.     unsigned long     bitsPerSlot, samplesPerFrame, frameNum = 0;
  136.     unsigned long     frameBits, gotBits = 0;
  137.     IFF_AIFF          pcm_aiff_data;
  138.     char              encoded_file_name[MAX_NAME_SIZE];
  139.     char              decoded_file_name[MAX_NAME_SIZE];
  140.     char          default_file_name[MAX_NAME_SIZE];
  141.     char              t[50];
  142.     int               need_aiff;
  143.     int               need_esps;        /* MI */
  144.     int topSb = 0;
  145.  
  146. III_scalefac_t III_scalefac;
  147. III_side_info_t III_side_info;
  148.  
  149. #ifdef  MACINTOSH
  150.     console_options.nrows = MAC_WINDOW_SIZE;
  151.     argc = ccommand(&argv);
  152. #endif
  153.  
  154.     /* Most large variables are declared dynamically to ensure
  155.        compatibility with smaller machines */
  156.  
  157.     pcm_sample = (PCM FAR *) mem_alloc((long) sizeof(PCM), "PCM Samp");
  158.     sample = (SAM FAR *) mem_alloc((long) sizeof(SAM), "Sample");
  159.     fraction = (FRA FAR *) mem_alloc((long) sizeof(FRA), "fraction");
  160.     w = (VE FAR *) mem_alloc((long) sizeof(VE), "w");
  161.  
  162.     fr_ps.header = &info;
  163.     fr_ps.tab_num = -1;                /* no table loaded */
  164.     fr_ps.alloc = NULL;
  165.     for (i=0;i<HAN_SIZE;i++) for (j=0;j<2;j++) (*w)[j][i] = 0.0;
  166.  
  167.     programName = argv[0];
  168.     if(argc==1) {        /* no command line args -> interact */
  169.        do {
  170.           printf ("Enter encoded file name <required>: ");
  171.           gets (encoded_file_name);
  172.           if (encoded_file_name[0] == NULL_CHAR)
  173.              printf ("Encoded file name is required. \n");
  174.        } while (encoded_file_name[0] == NULL_CHAR);
  175.        printf (">>> Encoded file name is: %s \n", encoded_file_name);
  176.  
  177.  
  178.  
  179.  
  180.  
  181.     x=0;
  182.     while (x <= MAX_NAME_SIZE)
  183.     {
  184.     default_file_name[x] = NULL_CHAR;
  185.     ++x;
  186.     }
  187.     x=0;
  188.     while (x <= 8)
  189.     {
  190.    default_file_name[x] = encoded_file_name[x];
  191.     if (encoded_file_name[++x] == '.') 
  192.     x = 9;
  193.   
  194.     }
  195.    
  196.     strcat(default_file_name,DFLT_OPEXT);
  197.  
  198.     printf("Enter MPEG decoded output file name <%s>: ",
  199.        default_file_name); /* 92-08-19 shn */
  200.     gets(decoded_file_name);
  201.     if (decoded_file_name[0] == NULL_CHAR) {
  202.  
  203.     /* replace old extension with new one, 92-08-19 shn */
  204.        strcpy(decoded_file_name,default_file_name);
  205.  
  206.     }
  207.  
  208.  
  209.  
  210.        printf (">>> MPEG decoded file name is: %s \n", decoded_file_name);
  211.  
  212.        printf(
  213.           "Do you wish to write an AIFF compatible sound file ? (y/<n>) : ");
  214.        gets(t);
  215.        if (*t == 'y' || *t == 'Y') need_aiff = TRUE;
  216.        else                        need_aiff = FALSE;
  217.        if (need_aiff)
  218.             printf(">>> An AIFF compatible sound file will be written\n");
  219.        else printf(">>> A non-headered PCM sound file will be written\n");
  220.  
  221.        printf(
  222.           "Do you wish to exit (last chance before decoding) ? (y/<n>) : ");
  223.        gets(t);
  224.        if (*t == 'y' || *t == 'Y') exit(0);
  225.     }
  226.     else {        /* interpret CL Args */
  227.        int i=0, err=0;
  228.  
  229.        need_aiff = FALSE;
  230.        need_esps = FALSE;    /* MI */
  231.        encoded_file_name[0] = '\0';
  232.        decoded_file_name[0] = '\0';
  233.  
  234.        while(++i<argc && err == 0) {
  235.           char c, *token, *arg, *nextArg;
  236.           int  argUsed;
  237.  
  238.           token = argv[i];
  239.           if(*token++ == '-') {
  240.              if(i+1 < argc) nextArg = argv[i+1];
  241.              else           nextArg = "";
  242.              argUsed = 0;
  243.              while(c = *token++) {
  244.                 if(*token /* NumericQ(token) */) arg = token;
  245.                 else                             arg = nextArg;
  246.                 switch(c) {
  247.                    case 's':  topSb = atoi(arg); argUsed = 1;
  248.                       if(topSb<1 || topSb>SBLIMIT) {
  249.                          fprintf(stderr, "%s: -s band %s not %d..%d\n",
  250.                                  programName, arg, 1, SBLIMIT);
  251.                          err = 1;
  252.                       }
  253.                       break;
  254.                    case 'A':  need_aiff = TRUE; break;
  255.                    case 'E':  need_esps = TRUE; break;    /* MI */
  256.                    default:   fprintf(stderr,"%s: unrecognized option %c\n",
  257.                                       programName, c);
  258.                       err = 1; break;
  259.                 }
  260.                 if(argUsed) {
  261.                    if(arg == token) token = ""; /* no more from token */
  262.                    else             ++i; /* skip arg we used */
  263.                    arg = ""; argUsed = 0;
  264.                 }
  265.              }
  266.           }
  267.           else {
  268.              if(encoded_file_name[0] == '\0')
  269.                 strcpy(encoded_file_name, argv[i]);
  270.              else
  271.                 if(decoded_file_name[0] == '\0')
  272.                    strcpy(decoded_file_name, argv[i]);
  273.                 else {
  274.                    fprintf(stderr,
  275.                            "%s: excess arg %s\n", programName, argv[i]);
  276.                    err = 1;
  277.                 }
  278.           }
  279.        }
  280.  
  281.        if(err || encoded_file_name[0] == '\0') usage();  /* never returns */
  282.  
  283.        if(decoded_file_name[0] == '\0') {
  284.           strcpy(decoded_file_name, encoded_file_name);
  285.           strcat(decoded_file_name, DFLT_OPEXT);
  286.        }
  287.  
  288.     }
  289.     /* report results of dialog / command line */
  290.     printf("Input file = '%s'  output file = '%s'\n",
  291.            encoded_file_name, decoded_file_name);
  292.     if(need_aiff) printf("Output file written in AIFF format\n");
  293.     if(need_esps) printf("Output file written in ESPS format\n"); /* MI */
  294.  
  295.     if ((musicout = fopen(decoded_file_name, "w+b")) == NULL) {
  296.        printf ("Could not create \"%s\".\n", decoded_file_name);
  297.        exit(1);
  298.     }
  299.  
  300.     open_bit_stream_r(&bs, encoded_file_name, BUFFER_SIZE);
  301.  
  302.     if (need_aiff)
  303.        if (aiff_seek_to_sound_data(musicout) == -1) {
  304.           printf("Could not seek to PCM sound data in \"%s\".\n",
  305.                  decoded_file_name);
  306.           exit(1);
  307.        }
  308.  
  309.     sample_frames = 0;
  310.  
  311.     while (!end_bs(&bs)) {
  312.  
  313.        sync = seek_sync(&bs, SYNC_WORD, SYNC_WORD_LNGTH);
  314.        frameBits = sstell(&bs) - gotBits;
  315.        if(frameNum > 0)        /* don't want to print on 1st loop; no lay */
  316.           if(frameBits%bitsPerSlot)
  317.              fprintf(stderr,"Got %ld bits = %ld slots plus %ld\n",
  318.                      frameBits, frameBits/bitsPerSlot, frameBits%bitsPerSlot);
  319.        gotBits += frameBits;
  320.  
  321.        if (!sync) {
  322.           printf("Frame cannot be located\n");
  323.           printf("Input stream may be empty\n");
  324.           done = TRUE;
  325.           /* finally write out the buffer */
  326.           if (info.lay != 1) out_fifo(*pcm_sample, 3, &fr_ps, done,
  327.                                       musicout, &sample_frames);
  328.           else               out_fifo(*pcm_sample, 1, &fr_ps, done,
  329.                                       musicout, &sample_frames);
  330.           break;
  331.        }
  332.  
  333.        decode_info(&bs, &fr_ps);
  334.        hdr_to_frps(&fr_ps);
  335.        stereo = fr_ps.stereo;
  336.        error_protection = info.error_protection;
  337.        crc_error_count = 0;
  338.        total_error_count = 0;
  339.        if(frameNum == 0) WriteHdr(&fr_ps, stdout);  /* printout layer/mode */
  340.  
  341. #ifdef ESPS
  342. if (frameNum == 0 && need_esps) {
  343. esps_write_header(musicout,(long) sample_frames, (double)
  344. s_freq[info.sampling_frequency] * 1000,
  345. (int) stereo, decoded_file_name );
  346. } /* MI */
  347. #endif
  348.  
  349.        fprintf(stderr, "{%4lu}", frameNum++); fflush(stderr);
  350.        if (error_protection) buffer_CRC(&bs, &old_crc);
  351.  
  352.        switch (info.lay) {
  353.  
  354.           case 1: {
  355.              bitsPerSlot = 32;        samplesPerFrame = 384;
  356.              I_decode_bitalloc(&bs,bit_alloc,&fr_ps);
  357.              I_decode_scale(&bs, bit_alloc, scale_index, &fr_ps);
  358.  
  359.              if (error_protection) {
  360.                 I_CRC_calc(&fr_ps, bit_alloc, &new_crc);
  361.                 if (new_crc != old_crc) {
  362.                    crc_error_count++;
  363.                    total_error_count++;
  364.                    recover_CRC_error(*pcm_sample, crc_error_count,
  365.                                      &fr_ps, musicout, &sample_frames);
  366.                    break;
  367.                 }
  368.                 else crc_error_count = 0;
  369.              }
  370.  
  371.              clip = 0;
  372.              for (i=0;i<SCALE_BLOCK;i++) {
  373.                 I_buffer_sample(&bs,(*sample),bit_alloc,&fr_ps);
  374.                 I_dequantize_sample(*sample,*fraction,bit_alloc,&fr_ps);
  375.                 I_denormalize_sample((*fraction),scale_index,&fr_ps);
  376.                 if(topSb>0)        /* clear channels to 0 */
  377.                    for(j=topSb; j<fr_ps.sblimit; ++j)
  378.                       for(k=0; k<stereo; ++k)
  379.                          (*fraction)[k][0][j] = 0;
  380.  
  381.                 for (j=0;j<stereo;j++) {
  382.                    clip += SubBandSynthesis (&((*fraction)[j][0][0]), j,
  383.                                              &((*pcm_sample)[j][0][0]));
  384.                 }
  385.                 out_fifo(*pcm_sample, 1, &fr_ps, done,
  386.                          musicout, &sample_frames);
  387.              }
  388.              if(clip > 0) printf("%d output samples clipped\n", clip);
  389.              break;
  390.           }
  391.  
  392.           case 2: {
  393.              bitsPerSlot = 8;        samplesPerFrame = 1152;
  394.              II_decode_bitalloc(&bs, bit_alloc, &fr_ps);
  395.              II_decode_scale(&bs, scfsi, bit_alloc, scale_index, &fr_ps);
  396.  
  397.              if (error_protection) { 
  398.                 II_CRC_calc(&fr_ps, bit_alloc, scfsi, &new_crc);
  399.                 if (new_crc != old_crc) {
  400.                    crc_error_count++;
  401.                    total_error_count++;
  402.                    recover_CRC_error(*pcm_sample, crc_error_count,
  403.                                      &fr_ps, musicout, &sample_frames);
  404.                    break;
  405.                 }
  406.                 else crc_error_count = 0;
  407.              }
  408.  
  409.              clip = 0;
  410.              for (i=0;i<SCALE_BLOCK;i++) {
  411.                 II_buffer_sample(&bs,(*sample),bit_alloc,&fr_ps);
  412.                 II_dequantize_sample((*sample),bit_alloc,(*fraction),&fr_ps);
  413.                 II_denormalize_sample((*fraction),scale_index,&fr_ps,i>>2);
  414.  
  415.                 if(topSb>0)        /* debug : clear channels to 0 */
  416.                    for(j=topSb; j<fr_ps.sblimit; ++j)
  417.                       for(k=0; k<stereo; ++k)
  418.                          (*fraction)[k][0][j] =
  419.                          (*fraction)[k][1][j] =
  420.                          (*fraction)[k][2][j] = 0;
  421.  
  422.                 for (j=0;j<3;j++) for (k=0;k<stereo;k++) {
  423.                    clip += SubBandSynthesis (&((*fraction)[k][j][0]), k,
  424.                                              &((*pcm_sample)[k][j][0]));
  425.                 }
  426.                 out_fifo(*pcm_sample, 3, &fr_ps, done, musicout,
  427.                          &sample_frames);
  428.              }
  429.              if(clip > 0) printf("%d samples clipped\n", clip);
  430.              break;
  431.           }
  432.  
  433.           case 3: {
  434.              int nSlots;
  435.              int gr, ch, ss, sb, main_data_end, flush_main ;
  436.          int  bytes_to_discard ;
  437.          static int frame_start = 0;
  438.              bitsPerSlot = 8;        samplesPerFrame = 1152;
  439.  
  440.              III_get_side_info(&bs, &III_side_info, &fr_ps);
  441.              nSlots = main_data_slots(fr_ps);
  442.              for (; nSlots > 0; nSlots--)  /* read main data. */
  443.                 hputbuf((unsigned int) getbits(&bs,8), 8);
  444.          main_data_end = hsstell() / 8; /*of privious frame*/
  445.              if ( flush_main=(hsstell() % bitsPerSlot) ) { 
  446.                 hgetbits((int)(bitsPerSlot - flush_main));
  447.         main_data_end ++;
  448.          }
  449.              bytes_to_discard = frame_start - main_data_end
  450.                          - III_side_info.main_data_begin ;
  451.              if( main_data_end > 4096 )
  452.              {   frame_start -= 4096;
  453.                  rewindNbytes( 4096 );
  454.              }
  455.  
  456.              frame_start += main_data_slots(fr_ps);
  457.              if (bytes_to_discard < 0) {
  458.          printf("Not enough main data to decode frame %d.  Frame discarded.\n", 
  459.                         frameNum - 1); break;
  460.              }
  461.              for (; bytes_to_discard > 0; bytes_to_discard--) hgetbits(8);
  462.  
  463.              clip = 0;
  464.              for (gr=0;gr<2;gr++) {
  465.                double lr[2][SBLIMIT][SSLIMIT],ro[2][SBLIMIT][SSLIMIT];
  466.  
  467.                for (ch=0; ch<stereo; ch++) {
  468.                  long int is[SBLIMIT][SSLIMIT];   /* Quantized samples. */
  469.                  int part2_start;
  470.                  part2_start = hsstell();
  471.                  III_get_scale_factors(III_scalefac,&III_side_info,gr,ch,
  472.             &fr_ps);
  473.                  III_hufman_decode(is, &III_side_info, ch, gr, part2_start,
  474.                                    &fr_ps);
  475.                  III_dequantize_sample(is, ro[ch], &III_scalefac,
  476.                                    &(III_side_info.ch[ch].gr[gr]), ch, &fr_ps);
  477.                }
  478.                III_stereo(ro,lr,III_scalefac,
  479.                             &(III_side_info.ch[0].gr[gr]), &fr_ps);
  480.                for (ch=0; ch<stereo; ch++) {
  481.                     double re[SBLIMIT][SSLIMIT];
  482.                     double hybridIn[SBLIMIT][SSLIMIT];/* Hybrid filter input */
  483.                     double hybridOut[SBLIMIT][SSLIMIT];/* Hybrid filter out */
  484.                     double polyPhaseIn[SBLIMIT];     /* PolyPhase Input. */
  485.  
  486.                     III_reorder (lr[ch],re,&(III_side_info.ch[ch].gr[gr]),
  487.                                   &fr_ps);
  488.                     III_antialias(re, hybridIn, /* Antialias butterflies. */
  489.                                   &(III_side_info.ch[ch].gr[gr]), &fr_ps);
  490.                     for (sb=0; sb<SBLIMIT; sb++) { /* Hybrid synthesis. */
  491.                         III_hybrid(hybridIn[sb], hybridOut[sb], sb, ch,
  492.                                    &(III_side_info.ch[ch].gr[gr]), &fr_ps);
  493.                     }
  494.                     for (ss=0;ss<18;ss++) /*Frequency inversion for polyphase.*/
  495.                        for (sb=0; sb<SBLIMIT; sb++)
  496.                           if ((ss%2) && (sb%2))
  497.                              hybridOut[sb][ss] = -hybridOut[sb][ss];
  498.                     for (ss=0;ss<18;ss++) { /* Polyphase synthesis */
  499.                         for (sb=0; sb<SBLIMIT; sb++)
  500.                             polyPhaseIn[sb] = hybridOut[sb][ss];
  501.                         clip += SubBandSynthesis (polyPhaseIn, ch,
  502.                                                   &((*pcm_sample)[ch][ss][0]));
  503.                         }
  504.                     }
  505.                 /* Output PCM sample points for one granule. */
  506.                 out_fifo(*pcm_sample, 18, &fr_ps, done, musicout,
  507.                          &sample_frames);
  508.              }
  509.              if(clip > 0) printf("%d samples clipped.\n", clip);
  510.              break;
  511.           }
  512.        }
  513.     }
  514.  
  515.     if (need_aiff) {
  516.        pcm_aiff_data.numChannels       = stereo;
  517.        pcm_aiff_data.numSampleFrames   = sample_frames;
  518.        pcm_aiff_data.sampleSize        = 16;
  519.        pcm_aiff_data.sampleRate        = s_freq[info.sampling_frequency]*1000;
  520. #ifdef IFF_LONG
  521.        pcm_aiff_data.sampleType        = IFF_ID_SSND;
  522. #else
  523.        strncpy(&pcm_aiff_data.sampleType,IFF_ID_SSND,4);
  524. #endif
  525.        pcm_aiff_data.blkAlgn.offset    = 0;
  526.        pcm_aiff_data.blkAlgn.blockSize = 0;
  527.  
  528.        if (aiff_write_headers(musicout, &pcm_aiff_data) == -1) {
  529.           printf("Could not write AIFF headers to \"%s\"\n",
  530.                  decoded_file_name);
  531.           exit(2);
  532.        }
  533.     }
  534.  
  535.     printf("Avg slots/frame = %.3f; b/smp = %.2f; br = %.3f kbps\n",
  536.            (FLOAT) gotBits / (frameNum * bitsPerSlot),
  537.            (FLOAT) gotBits / (frameNum * samplesPerFrame),
  538.            (FLOAT) gotBits / (frameNum * samplesPerFrame) *
  539.            s_freq[info.sampling_frequency]);
  540.  
  541.     close_bit_stream_r(&bs);
  542.     fclose(musicout);
  543.  
  544.     /* for the correct AIFF header information */
  545.     /*             on the Macintosh            */
  546.     /* the file type and the file creator for  */
  547.     /* Macintosh compatible Digidesign is set  */
  548.  
  549. #ifdef  MACINTOSH
  550.     if (need_aiff) set_mac_file_attr(decoded_file_name, VOL_REF_NUM,
  551.                                      CREATR_DEC_AIFF, FILTYP_DEC_AIFF);
  552.     else           set_mac_file_attr(decoded_file_name, VOL_REF_NUM,
  553.                                      CREATR_DEC_BNRY, FILTYP_DEC_BNRY);
  554. #endif
  555.  
  556.     printf("Decoding of \"%s\" is finished\n", encoded_file_name);
  557.     printf("The decoded PCM output file name is \"%s\"\n", decoded_file_name);
  558.     if (need_aiff)
  559.        printf("\"%s\" has been written with AIFF header information\n",
  560.               decoded_file_name);
  561.  
  562.     exit( 0 );
  563. }
  564.  
  565. static void usage()  /* print syntax & exit */
  566. {
  567.    fprintf(stderr,
  568.       "usage: %s                         queries for all arguments, or\n",
  569.        programName);
  570.    fprintf(stderr,
  571.       "       %s [-A][-s sb] inputBS [outPCM]\n", programName);
  572.    fprintf(stderr,"where\n");
  573.    fprintf(stderr," -A       write an AIFF output PCM sound file\n");
  574.    fprintf(stderr," -s sb    resynth only up to this sb (debugging only)\n");
  575.    fprintf(stderr," inputBS  input bit stream of encoded audio\n");
  576.    fprintf(stderr," outPCM   output PCM sound file (dflt inName+%s)\n",
  577.            DFLT_OPEXT);
  578.    exit(1);
  579. }
  580.