home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / Sound / LAME / Source / encode.c < prev    next >
C/C++ Source or Header  |  1999-05-31  |  9KB  |  347 lines

  1. /**********************************************************************
  2.  * ISO MPEG Audio Subgroup Software Simulation Group (1996)
  3.  * ISO 13818-3 MPEG-2 Audio Encoder - Lower Sampling Frequency Extension
  4.  *
  5.  * $Id: encode.c,v 1.2 1998/10/05 17:06:47 larsi Exp $
  6.  *
  7.  * $Log: encode.c,v $
  8.  * Revision 1.2  1998/10/05 17:06:47  larsi
  9.  * *** empty log message ***
  10.  *
  11.  * Revision 1.1.1.1  1998/10/05 14:47:17  larsi
  12.  *
  13.  * Revision 1.1  1996/02/14 04:04:23  rowlands
  14.  * Initial revision
  15.  *
  16.  * Received from Mike Coleman
  17.  **********************************************************************/
  18.  
  19.  
  20. #include "common.h"
  21. #include "encoder.h"
  22.  
  23.  
  24. /************************************************************************
  25. *
  26. * read_samples()
  27. *
  28. * PURPOSE:  reads the PCM samples from a file to the buffer
  29. *
  30. *  SEMANTICS:
  31. * Reads #samples_read# number of shorts from #musicin# filepointer
  32. * into #sample_buffer[]#.  Returns the number of samples read.
  33. *
  34. ************************************************************************/
  35.  
  36. unsigned long read_samples(musicin, sample_buffer, num_samples, frame_size)
  37. FILE *musicin;
  38. short sample_buffer[2304];
  39. unsigned long num_samples, frame_size;
  40. {
  41.     unsigned long samples_read;
  42.     static unsigned long samples_to_read;
  43.     static char init = TRUE;
  44.     extern int swapbytes; /* if TRUE then force swapping of byte order */
  45.  
  46.     if (init) {
  47.         samples_to_read = num_samples;
  48.         init = FALSE;
  49.     }
  50.     if (samples_to_read >= frame_size)
  51.         samples_read = frame_size;
  52.     else
  53.         samples_read = samples_to_read;
  54.     if ((samples_read =
  55.          fread(sample_buffer, sizeof(short), (int)samples_read, musicin)) == 0)
  56.       { /* printf("Hit end of audio data\n"); */ }
  57.     /*
  58.        Samples are big-endian. If this is a little-endian machine
  59.        we must swap
  60.      */
  61.     if ( NativeByteOrder == order_unknown )
  62.       {
  63.     NativeByteOrder = DetermineByteOrder();
  64.     if ( NativeByteOrder == order_unknown )
  65.       {
  66.         fprintf( stderr, "byte order not determined\n" );
  67.         exit( 1 );
  68.       }
  69.       }
  70.     if (!iswav && ( NativeByteOrder == order_littleEndian ))
  71.       SwapBytesInWords( sample_buffer, samples_read );
  72.  
  73.     if (swapbytes==TRUE)
  74.       SwapBytesInWords( sample_buffer, samples_read );
  75.  
  76.     samples_to_read -= samples_read;
  77.     if (samples_read < frame_size && samples_read > 0) {
  78.       /* printf("Insufficient PCM input for one frame - fillout with zeros\n"); */
  79.         for (; samples_read < frame_size; sample_buffer[samples_read++] = 0);
  80.         samples_to_read = 0;
  81.     }
  82.     return(samples_read);
  83. }
  84.  
  85. /************************************************************************
  86. *
  87. * get_audio()
  88. *
  89. * PURPOSE:  reads a frame of audio data from a file to the buffer,
  90. *   aligns the data for future processing, and separates the
  91. *   left and right channels
  92. *
  93. *
  94. ************************************************************************/
  95.  
  96. unsigned long get_audio( musicin, bufferL,bufferR, num_samples, stereo, info )
  97. FILE *musicin;
  98. /*
  99. short FAR buffer[2][1152];
  100. */
  101. short FAR bufferL[1152],bufferR[1152];
  102. unsigned long num_samples;
  103. int stereo;
  104. layer *info;
  105. {
  106.     int j;
  107.     short insamp[3176];
  108.     unsigned long samples_read;
  109.     int lay;
  110.     extern int autoconvert;
  111.  
  112.     lay = info->lay;
  113.  
  114.     /* pad with zeros in case we hit EOF */
  115.     memset((char *) insamp, 0, sizeof(insamp));
  116.  
  117.     if ( (lay == 3) && (info->version == 0) )  /* ie MPEG-2 LSF */
  118.     {
  119.     if ( stereo == 2 )
  120.     {
  121.         samples_read = read_samples( musicin, insamp, num_samples,
  122.                      (unsigned long) 1152 );
  123.         for ( j = 0; j < 576; j++ )
  124.         {
  125.         bufferL[j] = insamp[2 * j];
  126.         bufferR[j] = insamp[2 * j + 1];
  127.         }
  128.     }
  129.     else
  130.     {
  131.         samples_read = read_samples( musicin, insamp, num_samples,
  132.                      (unsigned long) 576 );
  133.         for ( j = 0; j < 576; j++ )
  134.         {
  135.         bufferL[j] = insamp[j];
  136.         bufferR[j] = 0;
  137.         }
  138.     }
  139.     }
  140.     else
  141.     /* MPEG 1 */
  142.       {
  143.         if(stereo == 2){ /* layer 2 (or 3), stereo */
  144.           samples_read = read_samples(musicin, insamp, num_samples,
  145.                       (unsigned long) 2304);
  146.           for(j=0;j<1152;j++) {
  147.         bufferL[j] = insamp[2*j];
  148.         bufferR[j] = insamp[2*j+1];
  149.         
  150.           }
  151.         }
  152.         else { /* layer 2 (or 3), mono */
  153.           if (autoconvert==TRUE) {  /* downconvert from a stereo file into a mono buffer */
  154.         samples_read = read_samples(musicin, insamp, num_samples,
  155.                         (unsigned long) 2304);
  156.         for(j=0;j<1152;j++){
  157.           bufferL[j] = insamp[2*j];
  158.           bufferR[j] = 0;
  159.         }    
  160.           }
  161.           else {
  162.         samples_read = read_samples(musicin, insamp, num_samples,
  163.                         (unsigned long) 1152);
  164.         for(j=0;j<1152;j++){
  165.             bufferL[j] = insamp[j];
  166.             bufferR[j] = 0;
  167.         }
  168.           }
  169.         }
  170.     }
  171.     return(samples_read);
  172. }
  173.  
  174. /************************************************************************
  175. *
  176. * window_subband()
  177. *
  178. * PURPOSE:  Overlapping window on PCM samples
  179. *
  180. * SEMANTICS:
  181. * 32 16-bit pcm samples are scaled to fractional 2's complement and
  182. * concatenated to the end of the window buffer #x#. The updated window
  183. * buffer #x# is then windowed by the analysis window #c# to produce the
  184. * windowed sample #z#
  185. *
  186. ************************************************************************/
  187.  
  188. extern double enwindow[];
  189.  
  190. void window_subband(buffer, z, k)
  191. short **buffer;
  192. double z[HAN_SIZE];
  193. int k;
  194. {
  195.     typedef double FAR XX[2][HAN_SIZE];
  196.     static XX FAR *x;
  197.     double *xk;
  198.     int i;
  199.     static int off[2] = {0,0};
  200.     static char init = 0;
  201.     if (!init) {
  202.         x = (XX FAR *) mem_alloc(sizeof(XX),"x");
  203.         memset(x, 0, 2*HAN_SIZE);
  204.         init = 1;
  205.     }
  206.     xk=(*x)[k];
  207.  
  208.     /* replace 32 oldest samples with 32 new samples */
  209.     for (i=0;i<32;i++) /*(*x)[k]*/xk[31-i+off[k]] = (double) *(*buffer)++/SCALE;
  210.     /* shift samples into proper window positions */
  211.     for (i=0;i<HAN_SIZE;i++) z[i] = xk[(i+off[k])&(HAN_SIZE-1)] * enwindow[i];
  212.     off[k] += 480;              /*offset is modulo (HAN_SIZE-1)*/
  213.     off[k] &= HAN_SIZE-1;
  214.  
  215. }
  216.  
  217. /************************************************************************
  218. *
  219. * filter_subband()
  220. *
  221. * PURPOSE:  Calculates the analysis filter bank coefficients
  222. *
  223. * SEMANTICS:
  224. *      The windowed samples #z# is filtered by the digital filter matrix #m#
  225. * to produce the subband samples #s#. This done by first selectively
  226. * picking out values from the windowed samples, and then multiplying
  227. * them by the filter matrix, producing 32 subband samples.
  228. *
  229. ************************************************************************/
  230.  
  231.  
  232. void
  233. create_dct_matrix( filter )
  234. double filter[16][32];
  235. {
  236.   register int i,k;
  237.  
  238.   for (i=0; i<16; i++)
  239.     for (k=0; k<32; k++) {
  240.        filter[i][k] = cos((double)((2*i+1)*k*PI64));
  241.     }
  242. }
  243.  
  244. void
  245. IDCT32( xin, xout )
  246. double *xin, *xout;
  247. {
  248.   int i,j;
  249.   double s0, s1;
  250.   typedef double MM[16][32];
  251.   static MM FAR * m = 0;
  252.   if( m==0 ) {
  253.     m = (MM FAR *)mem_alloc(sizeof(MM),"filter");
  254.     create_dct_matrix( *m );
  255.   }
  256.   for( i=0; i<16; i++ ) {
  257.     s0 = s1 = 0.0;
  258.     for( j=0; j<32; j+=2 ) {
  259.       s0 += (*m)[i][j]*xin[j+0];
  260.       s1 += (*m)[i][j+1]*xin[j+1];
  261.     }
  262.     xout[i] = s0+s1;
  263.     xout[31-i] = s0-s1;
  264.   }
  265. }
  266.  
  267. void filter_subband(z,s)
  268. double FAR z[HAN_SIZE], s[SBLIMIT];
  269. {
  270.    double y[64],yprime[32];
  271.    int i;
  272.    double *zi;
  273.  
  274.    zi = z;
  275.    for( i=0; i<64; i++ ) {
  276.       y[ i ] = *zi + zi[ 64 ] + zi[ 128 ] + zi[ 192 ] +
  277.                zi[ 256 ] + zi[ 320 ] + zi[ 384 ] + zi[ 448 ];
  278.       zi++;
  279.    }
  280.  
  281.    {
  282.      yprime[0] = y[16];
  283.      for( i=1; i<=16; i++ ) yprime[i] = y[i+16]+y[16-i];
  284.      for( i=17; i<=31; i++ ) yprime[i] = y[i+16]-y[80-i];
  285.      IDCT32( yprime, s );
  286.    }
  287. }
  288.  
  289.  
  290. /************************************************************************
  291. * encode_info()
  292. *
  293. * PURPOSE:  Puts the syncword and header information on the output
  294. * bitstream.
  295. *
  296. ************************************************************************/
  297.  
  298. void encode_info(fr_ps,bs)
  299. frame_params *fr_ps;
  300. Bit_stream_struc *bs;
  301. {
  302.         layer *info = fr_ps->header;
  303.  
  304.         putbits(bs,0xfff,12);                    /* syncword 12 bits */
  305.         put1bit(bs,info->version);               /* ID        1 bit  */
  306.         putbits(bs,4-info->lay,2);               /* layer     2 bits */
  307.         put1bit(bs,!info->error_protection);     /* bit set => no err prot */
  308.         putbits(bs,info->bitrate_index,4);
  309.         putbits(bs,info->sampling_frequency,2);
  310.         put1bit(bs,info->padding);
  311.         put1bit(bs,info->extension);             /* private_bit */
  312.         putbits(bs,info->mode,2);
  313.         putbits(bs,info->mode_ext,2);
  314.         put1bit(bs,info->copyright);
  315.         put1bit(bs,info->original);
  316.         putbits(bs,info->emphasis,2);
  317. }
  318.  
  319. /************************************************************************
  320. *
  321. * mod()
  322. *
  323. * PURPOSE:  Returns the absolute value of its argument
  324. *
  325. ************************************************************************/
  326.  
  327. double mod(a)
  328. double a;
  329. {
  330.     return (a > 0) ? a : -a;
  331. }
  332.  
  333.  
  334.  
  335. /************************************************************************
  336. *
  337. * encode_CRC
  338. *
  339. ************************************************************************/
  340.  
  341. void encode_CRC(crc, bs)
  342. unsigned int crc;
  343. Bit_stream_struc *bs;
  344. {
  345.    putbits(bs, crc, 16);
  346. }
  347.