⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 util.c

📁 MP3编码程序和资料
💻 C
📖 第 1 页 / 共 2 页
字号:
  int minimum = bs->buf_byte_idx + 1;  if (minimum <= 0) return 0;  if (size!=0 && minimum>size) return -1; /* buffer is too small */  memcpy(buffer,bs->buf,(unsigned int)minimum);  bs->buf_byte_idx = -1;  bs->buf_bit_idx = 0;  return minimum;}void init_bit_stream_w(lame_internal_flags *gfc){  Bit_stream_struc* bs = &gfc->bs;   alloc_buffer(bs, BUFFER_SIZE);   gfc->h_ptr = gfc->w_ptr = 0;   gfc->header[gfc->h_ptr].write_timing = 0;   gfc->bs.buf_byte_idx = -1;   gfc->bs.buf_bit_idx = 0;   gfc->bs.totbit = 0;}/*open and initialize the buffer; */void alloc_buffer(Bit_stream_struc *bs,   /* bit stream structure */unsigned int size){   bs->buf = (unsigned char *)       malloc(size);   bs->buf_size = size;}/*empty and close mallocs in gfc */void freegfc(lame_internal_flags *gfc)   /* bit stream structure */{   free(gfc->bs.buf);   free(gfc);}int putmask[9]={0x0, 0x1, 0x3, 0x7, 0xf, 0x1f, 0x3f, 0x7f, 0xff};/*******************************************************************************  End of bit_stream.c package******************************************************************************//* reorder the three short blocks By Takehiro TOMINAGA *//*  Within each scalefactor band, data is given for successive  time windows, beginning with window 0 and ending with window 2.  Within each window, the quantized values are then arranged in  order of increasing frequency...*/void ireorder(int scalefac_band[],int ix_orig[576]) {  int i,sfb, window, j=0;  int ix[576];  for (sfb = 0; sfb < SBMAX_s; sfb++) {    int start = scalefac_band[sfb];    int end   = scalefac_band[sfb + 1];    for (window = 0; window < 3; window++) {      for (i = start; i < end; ++i) {	ix[j++] = ix_orig[3*i+window];      }    }  }  memcpy(ix_orig,ix,576*sizeof(int));}void iun_reorder(int scalefac_band[],int ix_orig[576]) {  int i,sfb, window, j=0;  int ix[576];  for (sfb = 0; sfb < SBMAX_s; sfb++) {    int start = scalefac_band[sfb];    int end   = scalefac_band[sfb + 1];    for (window = 0; window < 3; window++) {      for (i = start; i < end; ++i) {	ix[3*i+window] = ix_orig[j++];      }    }  }  memcpy(ix_orig,ix,576*sizeof(int));}void freorder(int scalefac_band[],FLOAT8 ix_orig[576]) {  int i,sfb, window, j=0;  FLOAT8 ix[576];  for (sfb = 0; sfb < SBMAX_s; sfb++) {    int start = scalefac_band[sfb];    int end   = scalefac_band[sfb + 1];    for (window = 0; window < 3; window++) {      for (i = start; i < end; ++i) {	ix[j++] = ix_orig[3*i+window];      }    }  }  memcpy(ix_orig,ix,576*sizeof(FLOAT8));}void fun_reorder(int scalefac_band[],FLOAT8 ix_orig[576]) {  int i,sfb, window, j=0;  FLOAT8 ix[576];  for (sfb = 0; sfb < SBMAX_s; sfb++) {    int start = scalefac_band[sfb];    int end   = scalefac_band[sfb + 1];    for (window = 0; window < 3; window++) {      for (i = start; i < end; ++i) {	ix[3*i+window] = ix_orig[j++];      }    }  }  memcpy(ix_orig,ix,576*sizeof(FLOAT8));}/* resampling via FIR filter, blackman window */INLINE double blackman(int i,double offset,double fcn,int l){  double bkwn;  double wcn = (PI * fcn);  double dly = l / 2.0;  double x = i-offset;  if (x<0) x=0;  if (x>l) x=l;  bkwn = 0.42 - 0.5 * cos((x * 2) * PI /l)    + 0.08 * cos((x * 4) * PI /l);  if (fabs(x-dly)<1e-9) return wcn/PI;  else     return  (sin( (wcn *  ( x - dly))) / (PI * ( x - dly)) * bkwn );}int fill_buffer_downsample(lame_global_flags *gfp,short int *outbuf,int desired_len,			 short int *inbuf,int len,int *num_used,int ch) {    lame_internal_flags *gfc=gfp->internal_flags;  FLOAT8 offset,xvalue;  int i,j=0,k,value;  int filter_l;  FLOAT8 fcn,intratio;  short int *inbuf_old;  intratio=( fabs(gfc->resample_ratio - floor(.5+gfc->resample_ratio)) < .0001 );  fcn = .90/gfc->resample_ratio;  if (fcn>.90) fcn=.90;  filter_l=19;  /* must be odd */  /* if resample_ratio = int, filter_l should be even */  filter_l += intratio;  assert(filter_l +5 < BLACKSIZE);    if (!gfc->fill_buffer_downsample_init) {    gfc->fill_buffer_downsample_init=1;    gfc->itime[0]=0;    gfc->itime[1]=0;    memset((char *) gfc->inbuf_old, 0, sizeof(short int)*2*BLACKSIZE);    /* precompute blackman filter coefficients */    for (j= 0; j<= 2*BPC; ++j) {      offset=(double)(j-BPC)/(double)(2*BPC);      for (i=0; i<=filter_l; ++i) {	gfc->blackfilt[j][i]=blackman(i,offset,fcn,filter_l);      }    }  }  inbuf_old=gfc->inbuf_old[ch];  /* time of j'th element in inbuf = itime + j/ifreq; */  /* time of k'th element in outbuf   =  j/ofreq */  for (k=0;k<desired_len;k++) {    FLOAT8 time0;    int joff;    time0 = k*gfc->resample_ratio;       /* time of k'th output sample */    j = floor( time0 -gfc->itime[ch]  );    if ((j+filter_l/2) >= len) break;    /* blackmon filter.  by default, window centered at j+.5(filter_l%2) */    /* but we want a window centered at time0.   */    offset = ( time0 -gfc->itime[ch] - (j + .5*(filter_l%2)));    assert(fabs(offset)<=.500001);    joff = floor((offset*2*BPC) + BPC +.5);    xvalue=0;    for (i=0 ; i<=filter_l ; ++i) {      int j2 = i+j-filter_l/2;      int y;      y = (j2<0) ? inbuf_old[BLACKSIZE+j2] : inbuf[j2];#define PRECOMPUTE#ifdef PRECOMPUTE      xvalue += y*gfc->blackfilt[joff][i];#else      if (intratio) 	xvalue += y*gfc->blackfilt[joff][i];      else	xvalue += y*blackman(i,offset,fcn,filter_l);  /* very slow! */#endif    }    value = floor(.5+xvalue);    if (value > 32767) outbuf[k]=32767;    else if (value < -32767) outbuf[k]=-32767;    else outbuf[k]=value;  }    /* k = number of samples added to outbuf */  /* last k sample used data from j,j+1, or j+1 overflowed buffer */  /* remove num_used samples from inbuf: */  *num_used = Min(len,j+filter_l/2);  gfc->itime[ch] += *num_used - k*gfc->resample_ratio;  for (i=0;i<BLACKSIZE;i++)    inbuf_old[i]=inbuf[*num_used + i -BLACKSIZE];  return k;}/* 4 point interpolation routine for upsampling */int fill_buffer_upsample(lame_global_flags *gfp,short int *outbuf,int desired_len,        short int *inbuf,int len,int *num_used,int ch) {  int i,j=0,k,linear,value;  lame_internal_flags *gfc=gfp->internal_flags;  short int *inbuf_old;  if (!gfc->fill_buffer_upsample_init) {    gfc->fill_buffer_upsample_init=1;    gfc->upsample_itime[0]=0;    gfc->upsample_itime[1]=0;    memset((char *) gfc->upsample_inbuf_old, 0, sizeof(short int)*2*OLDBUFSIZE);  }  inbuf_old=gfc->upsample_inbuf_old[ch];  /* if downsampling by an integer multiple, use linear resampling,   * otherwise use quadratic */  linear = ( fabs(gfc->resample_ratio - floor(.5+gfc->resample_ratio)) < .0001 );  /* time of j'th element in inbuf = itime + j/ifreq; */  /* time of k'th element in outbuf   =  j/ofreq */  for (k=0;k<desired_len;k++) {    int y0,y1,y2,y3;    FLOAT8 x0,x1,x2,x3;    FLOAT8 time0;    time0 = k*gfc->resample_ratio;       /* time of k'th output sample */    j = floor( time0 -gfc->upsample_itime[ch]  );    /* itime[ch] + j;    */            /* time of j'th input sample */    if (j+2 >= len) break;             /* not enough data in input buffer */    x1 = time0-(gfc->upsample_itime[ch]+j);    x2 = x1-1;    y1 = (j<0) ? inbuf_old[OLDBUFSIZE+j] : inbuf[j];    y2 = ((1+j)<0) ? inbuf_old[OLDBUFSIZE+1+j] : inbuf[1+j];    /* linear resample */    if (linear) {      outbuf[k] = floor(.5 +  (y2*x1-y1*x2) );    } else {      /* quadratic */      x0 = x1+1;      x3 = x1-2;      y0 = ((j-1)<0) ? inbuf_old[OLDBUFSIZE+(j-1)] : inbuf[j-1];      y3 = ((j+2)<0) ? inbuf_old[OLDBUFSIZE+(j+2)] : inbuf[j+2];      value = floor(.5 +			-y0*x1*x2*x3/6 + y1*x0*x2*x3/2 - y2*x0*x1*x3/2 +y3*x0*x1*x2/6			);      if (value > 32767) outbuf[k]=32767;      else if (value < -32767) outbuf[k]=-32767;      else outbuf[k]=value;      /*      printf("k=%i  new=%i   [ %i %i %i %i ]\n",k,outbuf[k],	     y0,y1,y2,y3);      */    }  }  /* k = number of samples added to outbuf */  /* last k sample used data from j,j+1, or j+1 overflowed buffer */  /* remove num_used samples from inbuf: */  *num_used = Min(len,j+2);  gfc->upsample_itime[ch] += *num_used - k*gfc->resample_ratio;  for (i=0;i<OLDBUFSIZE;i++)    inbuf_old[i]=inbuf[*num_used + i -OLDBUFSIZE];  return k;}/*************************************************************************  Message Output************************************************************************/#ifdef LAME_STD_PRINTvoidlame_errorf(const char * s, ...){  va_list args;  va_start(args, s);  vfprintf(stderr, s, args);  va_end(args);}#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -