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

📄 loop.c

📁 shine定点库
💻 C
📖 第 1 页 / 共 2 页
字号:
/* loop.c */

#include "types.h"
#include "table2.h"

//#define EXCESS_TO_PART3

#define PART_2_3_LIMIT ((1<<12)-1)     /* 12 bit part_2_3_length */
#define DEC_BUFF_LIMIT 7680            /* decoder buffer limit */

extern int *scalefac_band_long;
extern int cutoff;

int count_bit(int ix[samp_per_frame2], unsigned int start, unsigned int end, unsigned int table );
int bigv_bitcount(int ix[samp_per_frame2], gr_info *gi);
int new_choose_table( int ix[samp_per_frame2], unsigned int begin, unsigned int end );
void bigv_tab_select( int ix[samp_per_frame2], gr_info *gi );
void subdivide(gr_info *gi);
int count1_bitcount( int ix[ samp_per_frame2 ], gr_info *gi );
void calc_runlen( int ix[samp_per_frame2], gr_info *gi );
int quantize(int ix[samp_per_frame2], int stepsize);
int ix_max( int ix[samp_per_frame2], unsigned int begin, unsigned int end );

static long
  *xr,                    /* magnitudes of the spectral values */
  xrabs[samp_per_frame2], /* xr absolute */
  xrmax;                  /* maximum of xrabs array */

/*
 * inner_loop:
 * ----------
 * Selects the quantizer stepsize that allows encoding of the entire
 * spectrum with as many bits as possible up to the maximum allowed
 * of max_bits. Bits used must never exceed max_bits.
 * The lower the value of stepsize, the more bits are required.
 * Each increment of stepsize is an increase of global gain by 1.5dB (2**0.25).
 * The bit calculation functions are executed 7 or 8 times per call.
 */
static int inner_loop(int ix[samp_per_frame2],int max_bits,gr_info *gi)
{
  int step, stepsize, bits;

  step = stepsize = 64;

  /* This loop ends within 1 step of max_bits.
   * It takes a fixed 7 times around because the tablesize is 128=2**7.
   */
  while(step)
  {
    if(!quantize(ix,stepsize))
      bits = 0x7fffffff;     /* outside (our) table range, early fail exit */
    else
    {
      calc_runlen(ix,gi);              /* rzero,count1,big_values */
      bits = count1_bitcount(ix,gi);   /* count1_table selection */
      subdivide(gi);                   /* bigvalues sfb division */
      bigv_tab_select(ix,gi);          /* codebook selection */
      bits += bigv_bitcount(ix,gi);    /* bit count */
    }

    step >>= 1;

    if(bits > max_bits)
      stepsize += step;
    else
      stepsize -= step;
  };

  /* this loop takes it over to the right side of max_bits.
   * it usually only has to go through once, sometimes not
   * at all. The while is just for safety.
   */
  while(bits > max_bits)
  {
    quantize(ix,++stepsize);         /* update ix */
    calc_runlen(ix,gi);              /* rzero,count1,big_values */
    bits = count1_bitcount(ix,gi);   /* count1_table selection */
    subdivide(gi);                   /* bigvalues sfb division */
    bigv_tab_select(ix,gi);          /* codebook selection */
    bits += bigv_bitcount(ix,gi);    /* bit count */
  }

  gi->quantizerStepSize = stepsize;

  return bits;
}

/*
 * L3_iteration_loop:
 * ------------------
 */
void L3_iteration_loop(long            mdct_freq_org[samp_per_frame2],
                       L3_side_info_t *side_info,
                       int             l3_enc[samp_per_frame2],
                       int             mean_bits)
{
  gr_info *gi;
  int ch, gr, i;
  int *ix;
  int max_bits;
  int extra_bits;
  int reservoir;
  int resv_max;
  static int main_data_begin;

  reservoir = main_data_begin << 3;  /* calculate reservoir at the frame start */
  side_info->main_data_begin = main_data_begin; /* set next frames back pointer */

  //for(gr=0; gr<config.mpeg.granules; gr++)
    //for(ch=config.mpeg.channels; ch--; )
   // {
      /* setup pointers */
      gr = 0;
      ch = 0;
      ix = l3_enc;
      xr = mdct_freq_org;
      gi = &side_info->gr[gr].ch[ch].tt;

      /* calculate absolute and maximum */
      for (i=cutoff, xrmax=0; i--;)
        if(xrmax < (xrabs[i] = abs(xr[i])))
          xrmax = xrabs[i];

      gi->part2_3_length    = 0;
      gi->big_values        = 0;
      gi->count1            = 0;
      gi->table_select[0]   = 0;
      gi->table_select[1]   = 0;
      gi->table_select[2]   = 0;
      gi->region0_count     = 0;
      gi->region1_count     = 0;
      gi->count1table_select= 0;

      /* calculate the available bits for the main data */
      extra_bits = reservoir >> 1; /* simple scheme, give half each time */
      reservoir -= extra_bits;
      max_bits = mean_bits + extra_bits;
      if(max_bits > PART_2_3_LIMIT)
      {
        reservoir += max_bits - PART_2_3_LIMIT;
        max_bits = PART_2_3_LIMIT;
      }

      /* quantize the spectrum unless all spectral values zero */
      if(xrmax)
        gi->part2_3_length = inner_loop(ix, max_bits, gi);

      reservoir += max_bits - gi->part2_3_length;  /* adjust for unused bits */

      gi->global_gain = gi->quantizerStepSize + 89;  /* StepSize = 0..127 (1.5dB steps) */

      /* restore sign of quantized spectral values */
      for ( i = 0; i < cutoff; i++ )
        if (xr[i] < 0)
          ix[i] *= -1;
   // }

  /* Sort out reservoir at frame end, limit the size and stuff the excess */
  resv_max = DEC_BUFF_LIMIT - config.mpeg.bits_per_frame;
  if(resv_max > config.mpeg.resv_limit)
    resv_max = config.mpeg.resv_limit;
  else if(resv_max < 0)
    resv_max = 0;
  main_data_begin = (reservoir < resv_max) ? (reservoir >> 3) : (resv_max >> 3);
  extra_bits = reservoir - (main_data_begin << 3);

#ifdef EXCESS_TO_PART3
  /* distribute excess bits throughout granules/channels */
  for(gr=0; gr<config.mpeg.granules; gr++)
    for(ch=0; ch<config.mpeg.channels; ch++)
    {
      int spare;
      if (!extra_bits)
        break;
      gi = &side_info->gr[gr].ch[ch].tt;
      spare = PART_2_3_LIMIT - gi->part2_3_length;
      if(spare > extra_bits)
        spare = extra_bits;
      gi->part2_3_length += spare;
      extra_bits -= spare;
    }
#endif

  side_info->resv_drain = extra_bits; /* remaining bits to ancillary data */
}

/*
 * quantize:
 * ---------
 * Function: Quantization of the vector xr ( -> ix).
 * Returns 1 if ixmax < 1000. (our table size)
 */
int quantize(int ix[samp_per_frame2], int stepsize )
{
  /* the -1 together with the 3 bit table shift gives a 4 bit shift.
   * This compensates for using mulr instead of mulsr. The 1 bit shift
   * is equal to a four bit shift in the fourth power of two table.
   */
  int i;
  long scale = pow2_4[stepsize-1]; /* 2**(-stepsize/4) */

  /* A quick check to see if ixmax will be less than 1000 (our table
   * size is 10000) this speeds up the early calls
   */
  if(mulr(xrmax, scale) > 9999)
    return 0; /* no point in continuing, stepsize not big enough */

  for(i=0;i<cutoff;i++)
    /* ix = (|xr| * 2**(-stepsize/4)) ** (3/4)
     * The multiply must round it's result to emulate the 'nearest int'
     * (nint) function in the spec.
     */
    ix[i] = pow3_4[mulr(xrabs[i], scale)];

  return 1;
 }

/*
 * ix_max:
 * -------
 * Function: Calculate the maximum of ix from 'begin' to 'end-1'
 */
int ix_max( int ix[samp_per_frame2], unsigned int begin, unsigned int end )
{
  register int i;
  register int max = 0;

  for(i=begin;i<end;i++)
    if(max < ix[i])
      max = ix[i];
  return max;
}

/*
 * calc_runlen:
 * ------------
 * Function: Calculation of rzero, count1, big_values
 * (Partitions ix into big values, quadruples and zeros).
 */
void calc_runlen( int ix[samp_per_frame2], gr_info *gi )
{
  int i;

  /* zeros */
  for ( i = cutoff; i > 1; i -= 2 )
    if (ix[i-1] | ix[i-2])
      break;

  /* quadruples */
  gi->count1 = 0 ;
  for ( ; i > 3; i -= 4 )
    if (   ix[i-1] <= 1
        && ix[i-2] <= 1
        && ix[i-3] <= 1
        && ix[i-4] <= 1 )
      gi->count1++;
    else
      break;

  /* what's left are big values */
  gi->big_values = i>>1;
}

/*
 * count1_bitcount:
 * ----------------
 * Determines the number of bits to encode the quadruples.
 */
int count1_bitcount(int ix[samp_per_frame2], gr_info *gi)
{
  int p, i, k;
  int v, w, x, y, signbits;
  int sum0 = 0,
      sum1 = 0;

  for(i=gi->big_values<<1, k=0; k<gi->count1; i+=4, k++)
  {
    v = ix[i];
    w = ix[i+1];
    x = ix[i+2];
    y = ix[i+3];

    p = v + (w<<1) + (x<<2) + (y<<3);

    signbits = 0;
    if(v!=0) signbits++;
    if(w!=0) signbits++;
    if(x!=0) signbits++;
    if(y!=0) signbits++;

    sum0 += signbits;
    sum1 += signbits;

    sum0 += ht[32].hlen[p];
    sum1 += ht[33].hlen[p];
  }

  if(sum0<sum1)
  {
    gi->count1table_select = 0;
    return sum0;
  }

⌨️ 快捷键说明

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