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

  1. /**********************************************************************
  2. Copyright (c) 1991 MPEG/audio software simulation group, All Rights Reserved
  3. encode.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.  * 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.  *                                      efilter to enfilter           *
  18.  *                                      ewindow to enwindow           *
  19.  *                              integrated "quantizer", "scalefactor",*
  20.  *                              and "transmission" files              *
  21.  *                              update routine "window_subband"       *
  22.  * 3/31/91  Bill Aspromonte     replaced read_filter by               *
  23.  *                              create_an_filter                      *
  24.  * 5/10/91  W. Joseph Carter    Ported to Macintosh and Unix.         *
  25.  *                              Incorporated Jean-Georges Fritsch's   *
  26.  *                              "bitstream.c" package.                *
  27.  *                              Incorporated Bill Aspromonte's        *
  28.  *                              filterbank coefficient matrix         *
  29.  *                              calculation routines and added        *
  30.  *                              roundoff to coincide with specs.      *
  31.  *                              Modified to strictly adhere to        *
  32.  *                              encoded bitstream specs, including    *
  33.  *                              "Berlin changes".                     *
  34.  *                              Modified PCM sound file handling to   *
  35.  *                              process all incoming samples and fill *
  36.  *                              out last encoded frame with zeros     *
  37.  *                              (silence) if needed.                  *
  38.  *                              Located and fixed numerous software   *
  39.  *                              bugs and table data errors.           *
  40.  * 19jun91  dpwe (Aware)        moved "alloc_*" reader to common.c    *
  41.  *                              Globals sblimit, alloc replaced by new*
  42.  *                              struct 'frame_params' passed as arg.  *
  43.  *                              Added JOINT STEREO coding, layers I,II*
  44.  *                              Affects: *_bit_allocation,            *
  45.  *                              subband_quantization, encode_bit_alloc*
  46.  *                              sample_encoding                       *
  47.  * 6/10/91  Earle Jennings      modified II_subband_quantization to   *
  48.  *                              resolve type cast problem for MS_DOS  *
  49.  * 6/11/91  Earle Jennings      modified to avoid overflow on MS_DOS  *
  50.  *                              in routine filter_subband             *
  51.  * 7/10/91  Earle Jennings      port to MsDos from MacIntosh version  *
  52.  * 8/ 8/91  Jens Spille         Change for MS-C6.00                   *
  53.  *10/ 1/91  S.I. Sudharsanan,   Ported to IBM AIX platform.           *
  54.  *          Don H. Lee,                                               *
  55.  *          Peter W. Farrett                                          *
  56.  *10/ 3/91  Don H. Lee          implemented CRC-16 error protection   *
  57.  *                              newly introduced function encode_CRC  *
  58.  *11/ 8/91  Kathy Wang          Documentation of code                 *
  59.  *                              All variablenames are referred to     *
  60.  *                              with surrounding pound (#) signs      *
  61.  * 2/11/92  W. Joseph Carter    Ported new code to Macintosh.  Most   *
  62.  *                              important fixes involved changing     *
  63.  *                              16-bit ints to long or unsigned in    *
  64.  *                              bit alloc routines for quant of 65535 *
  65.  *                              and passing proper function args.     *
  66.  *                              Removed "Other Joint Stereo" option   *
  67.  *                              and made bitrate be total channel     *
  68.  *                              bitrate, irrespective of the mode.    *
  69.  *                              Fixed many small bugs & reorganized.  *
  70.  * 6/16/92  Shaun Astarabadi    Changed I_scale_factor_calc() and     *
  71.  *                              II_scale_factor_calc() to use scale   *
  72.  *                              factor 0 thru 62 only and not to      *
  73.  *                              encode index 63 into the bit stream.  *
  74.  * 7/27/92  Mike Li             (re-)Port to MS-DOS                   *
  75.  * 9/22/92  jddevine@aware.com  Fixed _scale_factor_calc() defs       *
  76.  * 3/31/93  Giogio Dimino       changed II_a_bit_allocation() from:   *
  77.  *                              if( ad > ...) to if(ad >= ...)        *
  78.  * 8/05/93  TEST                changed I_a_bit_allocation() from:    *
  79.  *                              if( ad > ...) to if(ad >= ...)        *
  80.  **********************************************************************/
  81.  
  82. #include "common.h"
  83. #include "encoder.h"
  84.  
  85. #ifdef MS_DOS
  86. extern unsigned _stklen = 16384;
  87. #endif
  88.  
  89.  
  90. /*=======================================================================\
  91. |                                                                       |
  92. | This segment contains all the core routines of the encoder,           |
  93. | except for the psychoacoustic models.                                 |
  94. |                                                                       |
  95. | The user can select either one of the two psychoacoustic              |
  96. | models. Model I is a simple tonal and noise masking threshold         |
  97. | generator, and Model II is a more sophisticated cochlear masking      |
  98. | threshold generator. Model I is recommended for lower complexity      |
  99. | applications whereas Model II gives better subjective quality at low  |
  100. | bit rates.                                                            |
  101. |                                                                       |
  102. | Layers I and II of mono, stereo, and joint stereo modes are supported.|
  103. | Routines associated with a given layer are prefixed by "I_" for layer |
  104. | 1 and "II_" for layer 2.                                              |
  105. \=======================================================================*/
  106.  
  107. /************************************************************************/
  108. /*
  109. /* read_samples()
  110. /*
  111. /* PURPOSE:  reads the PCM samples from a file to the buffer
  112. /*
  113. /*  SEMANTICS:
  114. /* Reads #samples_read# number of shorts from #musicin# filepointer
  115. /* into #sample_buffer[]#.  Returns the number of samples read.
  116. /*
  117. /************************************************************************/
  118.  
  119. unsigned long read_samples(musicin, sample_buffer, num_samples, frame_size)
  120. FILE *musicin;
  121. short sample_buffer[2304];
  122. unsigned long num_samples, frame_size;
  123. {
  124.     unsigned long samples_read;
  125.     static unsigned long samples_to_read;
  126.     static char init = TRUE;
  127.  
  128.     if (init) {
  129.         samples_to_read = num_samples;
  130.         init = FALSE;
  131.     }
  132.     if (samples_to_read >= frame_size)
  133.         samples_read = frame_size;
  134.     else
  135.         samples_read = samples_to_read;
  136.     if ((samples_read =
  137.          fread(sample_buffer, sizeof(short), (int)samples_read, musicin)) == 0)
  138.         printf("Hit end of audio data\n");
  139.     samples_to_read -= samples_read;
  140.     if (samples_read < frame_size && samples_read > 0) {
  141.         printf("Insufficient PCM input for one frame - fillout with zeros\n");
  142.         for (; samples_