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

📄 loop.c

📁 MPEG 2的音频编码软件。喜欢多媒体的开发人员可以看看。
💻 C
📖 第 1 页 / 共 5 页
字号:
	sum[ 0 ] = count_bit( ix, begin, end, choice[0] );
	sum[ 1 ] = count_bit( ix, begin, end, choice[1] );
	if ( sum[1] < sum[0] )
	    choice[ 0 ] = choice[ 1 ];
    }
    return choice[ 0 ];
}



/*************************************************************************/
/*            choose table                                               */
/*************************************************************************/

int choose_table( int max )
{
    int  i, choice;

    if ( max == 0 )
        return 0;
    
    max = abs( max );    
    choice = 0;

    if ( max < 15 )
    {
        for ( i = 0; i < 15; i++ )
        {
            if ( ht[i].xlen > max )
            {
		choice = i;
		break;
            }
        }
    }
    else
    {	
	max -= 15;
	for (i = 15; i < 32; i++ )
	{
	    if ( ht[i].linmax >= max )
	    {
		choice = i;
		break;
	    }
	}
    }
    assert( choice );
    return choice;
}


/*************************************************************************/
/*            bigv_bitcount                                              */
/*************************************************************************/

/*
Function: Count the number of bits necessary to code the bigvalues region.
*/

int bigv_bitcount( int ix[576], gr_info *gi )
{
    int bits = 0;
    
    if ( gi->window_switching_flag && gi->block_type == 2 )
    {
        /*
          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...
          */
        int sfb, window, line, start, end;
        I192_3 *ix_s;

        if ( gi->mixed_block_flag )
        {
            unsigned int table;

            if ( (table = gi->table_select[0]) != 0 )
                bits += count_bit( ix, 0, gi->address1, table );
            sfb = 2;
        }
        else
            sfb = 0;

        ix_s = (I192_3 *) &ix[0];

        for ( ; sfb < 13; sfb++ )
        {
            unsigned tableindex = 100;

            start = scalefac_band_short[ sfb ];
            end   = scalefac_band_short[ sfb+1 ];

            if ( start < 12 )
                tableindex = gi->table_select[ 0 ];
            else
                tableindex = gi->table_select[ 1 ];
            assert( tableindex < 32 );

            for ( window = 0; window < 3; window++ )
                for ( line = start; line < end; line += 2 )
                {
                    unsigned int code, ext;
                    int cbits, xbits;
                    int x = (*ix_s)[line][window];
                    int y = (*ix_s)[line + 1][window];
                    bits += HuffmanCode( tableindex, x, y, &code, &ext, &cbits, &xbits );
                }
        }
    }
    else
    {
        unsigned int table;
        
        if( (table=gi->table_select[0]) != 0 )  /* region0 */ 
            bits += count_bit(ix, 0, gi->address1, table );
        if( (table=gi->table_select[1]) != 0 )  /* region1 */ 
            bits += count_bit(ix, gi->address1, gi->address2, table );
        if( (table=gi->table_select[2]) != 0 )  /* region2 */ 
            bits += count_bit(ix, gi->address2, gi->address3, table );
    }
    return bits;
}



/*************************************************************************/
/*            count_bit                                                  */
/*************************************************************************/

/*
 Function: Count the number of bits necessary to code the subregion. 
*/

int count_bit( int ix[576], unsigned int start, unsigned int end, unsigned int table )
{
    int i, sum;

    sum = 0;
    for ( i = start; i < end; i += 2 )
    {
        unsigned int code, ext;
        int cbits, xbits;
        sum += HuffmanCode( table, ix[i], ix[i+1], &code, &ext, &cbits, &xbits );
    }
    return sum;
}



#ifndef HAVE_NINT
int
nint( double in )
{
    int    temp;

    if( in < 0 )  temp = (int)(in - 0.5);
    else    temp = (int)(in + 0.5);

    return(temp);
}

double
aint(double in) {
	return((long) in);
}
#endif



/*
  Seymour's comment:  Jan 8 1995
  When mixed_block_flag is set, the low subbands 0-1 undergo the long
  window transform and are each split into 18 frequency lines, while
  the remaining 30 subbands undergo the short window transform and are
  each split into 6 frequency lines. A problem now arises, as neither
  the short or long scale factor bands apply to this mixed spectrum.
  The standard resolves this situation by using the first 8 long scale
  factor bands for the low spectrum and the short scale factor bands
  in the range of 3 to 11 (inclusive) for the remaining frequency lines.
  These scale factor bands do not match exactly to the 0-1 subbands
  for all sampling frequencies (32,44.1 and 48 kHz); however they
  were designed so that there would not be a frequency gap or overlap
  at the switch over point. (Note multiply short frequency lines by 3
  to account for wider frequency line.) 
  */



/*************************************************************************/
/*            gr_deco                                                    */
/*************************************************************************/

void gr_deco( gr_info *cod_info )
{
    if ( cod_info->window_switching_flag != 0 && cod_info->block_type == 2 )
        if ( cod_info->mixed_block_flag == 0 )
        {
            cod_info->sfb_lmax = 0; /* No sb*/
            cod_info->sfb_smax = 0;
        }
        else
        {
            cod_info->sfb_lmax = 8;
            cod_info->sfb_smax = 3;
        }
    else
    {
        cod_info->sfb_lmax = SFB_LMAX - 1;
        cod_info->sfb_smax = SFB_SMAX - 1;    /* No sb */
    }
}








/* The following optional code written by Seymour Shlien
   will speed up the outer_loop code which is called
   by iteration_loop. When BIN_SEARCH is defined, the
   outer_loop function precedes the call to the function inner_loop
   with a call to bin_search gain defined below, which
   returns a good starting quantizerStepSize.
*/

#if defined(BIN_SEARCH) || defined(PERFORM) 
int count_bits(ix,cod_info)  
int  *ix; /*  I576  *ix; */
gr_info *cod_info;
{
int bits,max;
  calc_runlen(ix,cod_info);		/*rzero,count1,big_values*/
  max = ix_max( ix, 0,576);
  if(max > 8192) return 100000;         /* report unsuitable quantizer */
  bits = count1_bitcount(ix, cod_info); /*count1_table selection*/
  subdivide(cod_info);			/* bigvalues sfb division */
  bigv_tab_select(ix,cod_info);		/* codebook selection*/
  bits += bigv_bitcount(ix,cod_info);	/* bit count */
/* printf("\nglobal_gain = %f  bits= %d ",cod_info->quantizerStepSize,bits);*/
return bits;
}
#endif


#ifdef BIN_SEARCH

int bin_search_StepSize(int desired_rate, double start, int *ix,
           double xrs[576], gr_info * cod_info)
{
double top,bot,next,last;
int bit;
top = start;
bot = 200;
next = start;
do
  {
  last = next;
  next = aint((top+bot)/2.0);
  cod_info->quantizerStepSize = next;
  quantize(xrs,ix,cod_info);
  bit = count_bits(ix,cod_info);
  if (bit>desired_rate) top = next;
  else bot = next;
/*  printf("\n%f %f %f %d %d",next, top,bot,bit,desired_rate);*/
  }
  while ((bit != desired_rate) && fabs(last - next) > 1.0);
return next;
}

#endif




#ifdef PERFORM

/* The following code is used for exposing some problems with
   the outer_loop code. PERFORM should be defined to the
   frame number you wish to have additional output recorded
   in the file encode.log  - Seymour Shlien 14-Jan-97
*/

/*
float worst_xfsf_to_xmin_ratio(l3_xmin,xfsf,block_type,gr,ch)
double xfsf[4][CBLIMIT];
III_psy_xmin *l3_xmin;
int block_type,gr,ch;
*/
float worst_xfsf_to_xmin_ratio(III_psy_xmin *l3_xmin, double xfsf[4][CBLIMIT]
                             ,int block_type,int gr,int ch)

{
float ratio,maxratio;
int i,j;
maxratio =-100.0;
if (block_type != 2)
  for(i=0;i<21;i++)
    {
    ratio = 10.*log10(xfsf[0][i] /l3_xmin->l[gr][ch][i]);
    if (ratio > maxratio) maxratio = ratio;
    }
else
  {
  for(j=0;j<3;j++)
/* for(i = cod_info->sfb_smax; i <SFB_SMAX; i++) */
     for(i = 0; i <11; i++)
    {
    ratio = 10.*log10(xfsf[j+1][i] /l3_xmin->s[gr][ch][i][j]);
    if (ratio > maxratio) maxratio = ratio;
    }
  }
return maxratio;
}
 

print_ratios(handle_out,l3_xmin,xfsf,block_type,gr,ch)
FILE *handle_out;
double xfsf[4][CBLIMIT];
III_psy_xmin *l3_xmin;
int gr,ch;
int block_type;
{
float ratio;
int i,j;
if(block_type !=2)
 for (i=0;i<21;i++)
  {
   ratio = 100.0; /* signals undefined value in output */
   if(l3_xmin->l[gr][ch][i] >1.0e-20)
     ratio = 10.*log10(xfsf[0][i] /l3_xmin->l[gr][ch][i]);
   fprintf(handle_out,"%6.2f ",ratio);
   if(i%5==4) fprintf(handle_out,"\n");
  }
else
  
  for(j=0;j<3;j++)
   { fprintf(handle_out,"\n  block %d\n",j);
     for(i = 0; i <11; i++)
      {
      ratio = 10.*log10(xfsf[j+1][i] /l3_xmin->s[gr][ch][i][j]);
      fprintf(handle_out,"%6.2f ",ratio);
      if(i%5==4) fprintf(handle_out,"\n");
      }
   }
fprintf(handle_out,"\n");
}


print_scalefacs(handle_out,scalefac,block_type,gr,ch)
FILE *handle_out;
III_scalefac_t *scalefac;
int gr,ch;
int block_type;
{
int sfb,j;

if(block_type !=2)
 for ( sfb = 0; sfb < 21; sfb++ )
   {
   fprintf(handle_out,"%6d ", scalefac->l[gr][ch][sfb]);    
   if(sfb%5==4) fprintf(handle_out,"\n");
   }
else
 for (j=0;j<3;j++)
  {
  fprintf(handle_out,"\n block %d\n",j);
  for (sfb=0;sfb<11;sfb++)
   {
   fprintf(handle_out,"%6d ",scalefac->s[gr][ch][sfb][j]);
   if(sfb%5==4) fprintf(handle_out,"\n");
   }
  }
fprintf(handle_out,"\n");
}


print_quantized_values(FILE *handle, int ix[576], gr_info *cod_info)
{
int sfb,start,end,i,bw;
for (sfb=0;sfb<cod_info->sfb_lmax;sfb++)
  {
  start = scalefac_band_long[sfb];
  end = scalefac_band_long[sfb+1];
  bw = end - start;
  fprintf(handle,"scalefac band %d from %d to %d\n",sfb,start,end);
  for (i=0;i<bw;i++)
    {
    fprintf(handle,"%8d",ix[start+i]);
    if(i%5==4) fprintf(handle,"\n");
    }
  fprintf(handle,"\n");
  }
}


test_inner_loop(double xr[2][2][576], int l3_enc[2][2][576], int max_bits,
            gr_info *cod_info, int gr, int ch, double xfsf[4][CBLIMIT], 
            III_psy_xmin  *l3_xmin) 
{
    int bits, c1bits, bvbits;
    double *xrs;  /*  D576  *xr; */
    int *ix;  /*  I576  *ix; */
    xrs = &xr[gr][ch][0];
    ix = l3_enc[gr][ch];
 
    assert( max_bits >= 0 );
    cod_info->quantizerStepSize -= 1.0;;
    do
    {
        do
        {
            cod_info->quantizerStepSize += 1.0;
            quantize( xrs, ix, cod_info );
        }  
        while ( ix_max(ix, 0, 576) > 8191 + 14 ); /* within table range? */
 
    bits = count_bits(ix,cod_info);       

        if(frameNum == PERFORM)
        {
          fprintf(log_output,"StepSize=%f bits = %d huff_bits = %d\n",
            cod_info->quantizerStepSize,bits,max_bits);
          calc_noise( &xr[gr][ch][0], &l3_enc[gr][ch][0], cod_info, xfsf );
                                             /* distortion calculation */
          print_ratios(log_output,l3_xmin,xfsf,cod_info->block_type,gr,ch);
          fprintf(log_output,"\n\n");
          }

    }
    while ( bits > max_bits );
 
    return bits;
}
 
#endif PERFORM 

⌨️ 快捷键说明

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