www.pudn.com > wav_mp3_recorder.rar > layer3.c


/* layer3.c */ 
 
#include "types.h" 
 
//#define NO_RESERVOIR 
 
int *scalefac_band_long; 
//long          frames_processed; 
long          remain; 
long          bytes_per_frame; 
long          lag; 
int           sideinfo_len; 
static int    l3_enc[samp_per_frame2]; 
static long   l3_sb_sample[2][18][SBLIMIT]; 
static long   mdct_freq[samp_per_frame2]; 
static L3_side_info_t side_info; 
 
/* Scalefactor bands. */ 
static int sfBandIndex[4][3][23] = 
{ 
  { /* MPEG-2.5 11.025 kHz */ 
    {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576}, 
    /* MPEG-2.5 12 kHz */ 
    {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576}, 
    /* MPEG-2.5 8 kHz */ 
    {0,12,24,36,48,60,72,88,108,132,160,192,232,280,336,400,476,566,568,570,572,574,576} 
  }, 
  { 
    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} 
  }, 
  { /* Table B.2.b: 22.05 kHz */ 
    {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576}, 
    /* Table B.2.c: 24 kHz */ 
    {0,6,12,18,24,30,36,44,54,66,80,96,114,136,162,194,232,278,332,394,464,540,576}, 
    /* Table B.2.a: 16 kHz */ 
    {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576} 
  }, 
  { /* Table B.8.b: 44.1 kHz */ 
    {0,4,8,12,16,20,24,30,36,44,52,62,74,90,110,134,162,196,238,288,342,418,576}, 
    /* Table B.8.c: 48 kHz */ 
    {0,4,8,12,16,20,24,30,36,42,50,60,72,88,106,128,156,190,230,276,330,384,576}, 
    /* Table B.8.a: 32 kHz */ 
    {0,4,8,12,16,20,24,30,36,44,54,66,82,102,126,156,194,240,296,364,448,550,576} 
  } 
}; 
 
/* 
  L3_compress_initial() 
  */ 
void Mp3_Encode_Initial(void) 
{ 
  if(config.mpeg.type == MPEG1) 
  { 
    config.mpeg.granules = 2; 
    config.mpeg.samples_per_frame = samp_per_frame; 
    config.mpeg.resv_limit = ((1<<9)-1)<<3; 
    sideinfo_len = (config.mpeg.channels == 1) ? 168 : 288; 
  } 
  else /* mpeg 2/2.5 */ 
  { 
    config.mpeg.granules = 1; 
    config.mpeg.samples_per_frame = samp_per_frame2; 
    config.mpeg.resv_limit = ((1<<8)-1)<<3; 
    sideinfo_len = (config.mpeg.channels == 1) ? 104 : 168; 
  } 
  scalefac_band_long = sfBandIndex[config.mpeg.type][config.mpeg.samplerate_index]; 
 
#ifdef NO_RESERVOIR 
  config.mpeg.resv_limit = 0; 
#endif 
 
  { /* find number of whole bytes per frame and the remainder */ 
    long x = config.mpeg.samples_per_frame * config.mpeg.bitr * (1000/8); 
    bytes_per_frame = x / config.wave.samplerate; 
    remain  = x % config.wave.samplerate; 
  } 
 
  //config.mpeg.total_frames =  /* round up */ 
        //(config.wave.total_samples + config.mpeg.samples_per_frame - 1) / 
                    //  config.mpeg.samples_per_frame; 
  //printf("%ld frames\n",config.mpeg.total_frames); 
  lag = 0; 
  open_bit_stream(); 
} 
/* 
  L3_compress() 
  */ 
int Mp3_Encode(short* pcm_data, unsigned char* mp3_data) 
{ 
	int           mean_bits; 
  int           i; 
  int           num; 
  unsigned long *buffer; 
  buffer = wave_get(pcm_data); 
   // frames_processed++; 
    //if(((frames_processed & 7)==0) || (frames_processed >= config.mpeg.total_frames)) 
     // printf("\015[%ld] %ld%%", frames_processed,(frames_processed*100)/config.mpeg.total_frames); 
 
    /* sort out padding */ 
    config.mpeg.padding = (lag += remain) >= config.wave.samplerate; 
    if (config.mpeg.padding) 
      lag -= config.wave.samplerate; 
    config.mpeg.bits_per_frame = 8*(bytes_per_frame + config.mpeg.padding); 
 
    /* bits per channel per granule */ 
    mean_bits = (config.mpeg.bits_per_frame - sideinfo_len) >> 
                      (config.mpeg.granules + config.mpeg.channels - 2); 
 
    for(i=0;i<18;i++) 
       L3_window_filter_subband(&buffer, &l3_sb_sample[1][i][0] ,0); 
 
    L3_mdct_sub(l3_sb_sample, mdct_freq); 
 
    L3_iteration_loop(mdct_freq, &side_info, l3_enc, mean_bits); 
 
    num = L3_format_bitstream(l3_enc, &side_info, mp3_data); 
    return num; 
}