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

📄 loop.c

📁 功能非常完善的MP3编译码器,输入文件WAV或AIFF,能够方便的嵌入到你自己的系统当中.
💻 C
📖 第 1 页 / 共 4 页
字号:

    for ( sfb = 0; sfb < 13; sfb++ )
    {
      start = scalefac_band_short[ sfb ];
      end   = scalefac_band_short[ sfb+1 ];
      
      if ( start < 12 )
        pmax = &max1;
      else
        pmax = &max2;

      tMax = * pmax;

      for ( window = 0; window < 3; window++ )
        for ( line = start; line < end; line += 2 )
        {
          x = abs_ix[ (line * 3) + window ];
          y = abs_ix[ ((line + 1) * 3) + window ];

          if( x > tMax )
						tMax = x;

					if( y > tMax )
						tMax = y;
        }

			* pmax = tMax;
		}
    cod_info->table_select[0] = choose_table( max1 );
    cod_info->table_select[1] = choose_table( max2 );
  }
  else
  {
    if ( cod_info->address1 > 0 )
      cod_info->table_select[0] = new_choose_table_tj( abs_ix, 0, cod_info->address1 );

    if ( cod_info->address2 > cod_info->address1 )
      cod_info->table_select[1] = new_choose_table_tj( abs_ix, cod_info->address1, cod_info->address2 );

    if ( cod_info->big_values * 2 > cod_info->address2 )
      cod_info->table_select[2] = new_choose_table_tj( abs_ix, cod_info->address2, cod_info->big_values * 2 );
  }
}




/*************************************************************************/
/*            new_choose table                                           */
/*************************************************************************/

/*
  Choose the Huffman table that will encode ix[begin..end] with
  the fewest bits.

  Note: This code contains knowledge about the sizes and characteristics
  of the Huffman tables as defined in the IS (Table B.7), and will not work
  with any arbitrary tables.
*/


int new_choose_table_tj( int abs_ix[576], unsigned int begin, unsigned int end )
{
	uint max;
  int i;
  int choice[ 2 ];
  int sum[ 2 ];

  max = ix_max_tj( abs_ix, begin, end );

  if ( max == 0 )
    return 0;
    

  choice[ 0 ] = 0;
  choice[ 1 ] = 0;

  if ( max < 15 )
  {
    for ( i = 0; i < 14; i++ )
      if ( ht[i].xlen > max )
	    {
				choice[ 0 ] = i;
        break;
	    }
/*		assert( choice[0] ); */

		sum[ 0 ] = count_bit_tj( abs_ix, begin, end, choice[0] );

		switch ( choice[0] )
		{
			case 2:
				sum[ 1 ] = count_bit_tj( abs_ix, begin, end, 3 );
				if ( sum[1] <= sum[0] )
					choice[ 0 ] = 3;
				break;

			case 5:
				sum[ 1 ] = count_bit_tj( abs_ix, begin, end, 6 );
				if ( sum[1] <= sum[0] )
					choice[ 0 ] = 6;
				break;

			case 7:
				sum[ 1 ] = count_bit_tj( abs_ix, begin, end, 8 );
				if ( sum[1] <= sum[0] )
				{
					choice[ 0 ] = 8;
					sum[ 0 ] = sum[ 1 ];
				}
				sum[ 1 ] = count_bit_tj( abs_ix, begin, end, 9 );
				if ( sum[1] <= sum[0] )
					choice[ 0 ] = 9;
				break;

			case 10:
				sum[ 1 ] = count_bit_tj( abs_ix, begin, end, 11 );
				if ( sum[1] <= sum[0] )
				{
					choice[ 0 ] = 11;
					sum[ 0 ] = sum[ 1 ];
				}
				sum[ 1 ] = count_bit_tj( abs_ix, begin, end, 12 );
				if ( sum[1] <= sum[0] )
					choice[ 0 ] = 12;
				break;

			case 13:
				sum[ 1 ] = count_bit_tj( abs_ix, begin, end, 15 );
				if ( sum[1] <= sum[0] )
					choice[ 0 ] = 15;
				break;

			default:

				break;
		}
  }
  else
  {
	/* try tables with linbits */
		max -= 15;
	
		for ( i = 15; i < 24; i++ )
		{
	    if ( ht[i].linmax >= max )
	    {
				choice[ 0 ] = i;
				break;
	    }
		}
		for ( i = 24; i < 32; i++ )
		{
	    if ( ht[i].linmax >= max )
		  {
				choice[ 1 ] = i;
				break;
	    }
		}
/*		assert( choice[0] );
		assert( choice[1] ); */
	
		sum[ 0 ] = count_bit_tj( abs_ix, begin, end, choice[0] );
		sum[ 1 ] = count_bit_tj( abs_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;
	    	}
			}
    }
    return choice;
}


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

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


int bigv_bitcount_tj( int * abs_ix, 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_tj( abs_ix, 0, gi->address1, table );
      sfb = 2;
    }
    else
      sfb = 0;

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

    for ( ; sfb < 13; sfb++ )
    {
      unsigned tableindex = 100;
			unsigned idx;
			struct huffcodetab *h;

      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 ); */

			if ( tableindex != 0 )
			{
				h = &(ht[tableindex]);

				for ( window = 0; window < 3; window++ )
					for ( line = start; line < end; line += 2 )
					{
						int x = (*ix_s)[line][window];
						int y = (*ix_s)[line + 1][window];

						if ( tableindex > 15 )
						{ /* ESC-table is used */
							if ( x > 14 )
							{
								x = 15;
								bits += h->linbits;
							}
							if ( y > 14 )
							{
								y = 15;
								bits += h->linbits;
							}

						}

						idx = (x * h->ylen) + y;
						bits += h->hlen[ idx ];

						
						if ( x != 0 )
							bits += 1;

						if ( y != 0 )
							bits += 1;

					}
				}
    }
  }
  else
  {
		unsigned int table;
  
		if( (table=gi->table_select[0]) != 0 )  /* region0 */ 
			bits += count_bit_tj(abs_ix, 0, gi->address1, table );
		if( (table=gi->table_select[1]) != 0 )  /* region1 */ 
			bits += count_bit_tj(abs_ix, gi->address1, gi->address2, table );
		if( (table=gi->table_select[2]) != 0 )  /* region2 */ 
			bits += count_bit_tj(abs_ix, gi->address2, gi->address3, table );
	}
  return bits;
}



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

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


int count_bit_tj( int abs_ix[576], unsigned int start, unsigned int end, unsigned int table )
{
    int i, sum;
		unsigned idx;
		struct huffcodetab *h;
		int		x, y;

		if ( table == 0 )
			return 0;

		h = &(ht[table]);

    sum = 0;

		if( table > 15 )
		{
			for ( i = start; i < end; i += 2 )
			{
				x = abs_ix[i];
				y = abs_ix[i+1];

				if ( x > 14 )
				{

					x = 15;
					sum += h->linbits;
				}
				if ( y > 14 )
				{
					y = 15;
					sum += h->linbits;
				}

				idx = (x * h->ylen) + y;
				sum += h->hlen[ idx ];

				
				if ( x != 0 )
					sum += 1;

				if ( y != 0 )
					sum += 1;
    			
			}
		}
		else
		{
			for ( i = start; i < end; i += 2 )
			{
				x = abs_ix[i];
				y = abs_ix[i+1];

				idx = (x * h->ylen) + y;
				sum += h->hlen[ idx ];
				
				if ( x != 0 )
					sum += 1;

				if ( y != 0 )
					sum += 1;
    			
			}
		}

    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






/*************************************************************************/
/*            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.
*/

int count_bits( int * ix,gr_info * cod_info)
{
int bits;
  calc_runlen(ix,cod_info);		/*rzero,count1,big_values*/
  bits = count1_bitcount(ix, cod_info); /*count1_table selection*/
  subdivide(cod_info);			/* bigvalues sfb division */
  bigv_tab_select_tj(ix,cod_info);		/* codebook selection*/
  bits += bigv_bitcount_tj(ix,cod_info);	/* bit count */
return bits;

}



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;
		tjBitOverflow1 = FALSE;
		quantize_tj(xrs,ix,cod_info);
		if( tjBitOverflow1 == TRUE )
		  bit = 100000;
		else
		  bit = count_bits(ix,cod_info);
		if (bit>desired_rate) 
			top = next;
		else 
			bot = next;
  }
  while ((bit != desired_rate) && fabs(last - next) > 1.0);
return next;
}









⌨️ 快捷键说明

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