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

📄 l3loop.c

📁 MP3 encode source code
💻 C
📖 第 1 页 / 共 2 页
字号:
        cod_info->count1 = 0 ;
        for ( ; i > 3; i -= 4 )
            if ( abs(ix[i-1]) <= 1
              && abs(ix[i-2]) <= 1
              && abs(ix[i-3]) <= 1
              && abs(ix[i-4]) <= 1 )
                cod_info->count1++;
            else
                break;
        
        cod_info->big_values = i>>1;

}




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

    for(i=cod_info->big_values<<1, k=0; k<cod_info->count1; i+=4, k++)
    {
        v = abs(ix[i]);
        w = abs(ix[i+1]);
        x = abs(ix[i+2]);
        y = abs(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)
    {
        cod_info->count1table_select = 0;
        return sum0;
    }
    else
    {
        cod_info->count1table_select = 1;
        return sum1;
    }
}






void subdivide(gr_info *cod_info)
/*************************************************************************/
/* presumable subdivides the bigvalue region which will use separate Huffman tables.  */
/*************************************************************************/
{
    int scfb_anz = 0;
    int bigvalues_region;
    
    if ( !cod_info->big_values)
    { /* no big_values region */
        cod_info->region0_count = 0;
        cod_info->region1_count = 0;
    }
    else
    {
        bigvalues_region = 2 * cod_info->big_values;

        { 
            int thiscount, index;
            /* Calculate scfb_anz */
            while ( scalefac_band_long[scfb_anz] < bigvalues_region )
                scfb_anz++;

            cod_info->region0_count = subdv_table[scfb_anz].region0_count;
            thiscount = cod_info->region0_count;
            index = thiscount + 1;
            while ( thiscount && (scalefac_band_long[index] > bigvalues_region) )
            {
                thiscount--;
                index--;
            }
            cod_info->region0_count = thiscount;

            cod_info->region1_count = subdv_table[scfb_anz].region1_count;
            index = cod_info->region0_count + cod_info->region1_count + 2;
            thiscount = cod_info->region1_count;
            while ( thiscount && (scalefac_band_long[index] > bigvalues_region) )
            {
                thiscount--;
                index--;
            }
            cod_info->region1_count = thiscount;
            cod_info->address1 = scalefac_band_long[cod_info->region0_count+1];
            cod_info->address2 = scalefac_band_long[cod_info->region0_count
                                                    + cod_info->region1_count + 2 ];
            cod_info->address3 = bigvalues_region;
        }
    }
}






void bigv_tab_select( int ix[samp_per_frame2], gr_info *cod_info )
/*************************************************************************/
/*   Function: Select huffman code tables for bigvalues regions */
/*************************************************************************/
{
    cod_info->table_select[0] = 0;
    cod_info->table_select[1] = 0;
    cod_info->table_select[2] = 0;
    
    {
        if ( cod_info->address1 > 0 )
            cod_info->table_select[0] = new_choose_table( ix, 0, cod_info->address1 );

        if ( cod_info->address2 > cod_info->address1 )
            cod_info->table_select[1] = new_choose_table( ix, cod_info->address1, cod_info->address2 );

        if ( cod_info->big_values<<1 > cod_info->address2 )
            cod_info->table_select[2] = new_choose_table( ix, cod_info->address2, cod_info->big_values<<1 );
    }
}





int new_choose_table( int ix[samp_per_frame2], unsigned int begin, unsigned int end )
/*************************************************************************/
/*  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 i, max;
    int choice[2];
    int sum[2];

    max = ix_max(ix,begin,end);
    if(!max)
		return 0;

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

    if(max<15)
    {
	/* try tables with no linbits */
        for ( i =14; i--; )
            if ( ht[i].xlen > max )
	    {
		choice[ 0 ] = i;
                break;
	    }

	sum[ 0 ] = count_bit( ix, begin, end, choice[0] );

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

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

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

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

	  case 13:
	    sum[ 1 ] = count_bit( ix, begin, end, 15 );
	    if ( sum[1] <= sum[0] )
		choice[ 0 ] = 15;
	    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;
	    }
	}
	
	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];
}



int bigv_bitcount(int ix[samp_per_frame2], gr_info *gi)
/*************************************************************************/
/* Function: Count the number of bits necessary to code the bigvalues region.  */
/*************************************************************************/
{
    int bits = 0;
    
        unsigned int table;
        
        if( (table=gi->table_select[0]))  /* region0 */ 
            bits += count_bit(ix, 0, gi->address1, table );
        if( (table=gi->table_select[1]))  /* region1 */ 
            bits += count_bit(ix, gi->address1, gi->address2, table );
        if( (table=gi->table_select[2]))  /* region2 */ 
            bits += count_bit(ix, gi->address2, gi->address3, table );
    return bits;
}



int count_bit(int ix[samp_per_frame2], 
              unsigned int start, 
              unsigned int end,
              unsigned int table )
/*************************************************************************/
/* Function: Count the number of bits necessary to code the subregion. */
/*************************************************************************/
{
    unsigned            linbits, ylen;
    register int        i, sum;
    register int        x,y;
    struct huffcodetab *h;

    if(!table)
		return 0;

    h   = &(ht[table]);
    sum = 0;

    ylen    = h->ylen;
    linbits = h->linbits;

    if(table>15)
    { // ESC-table is used 
        for(i=start;i<end;i+=2)
        {
            x = ix[i];
            y = ix[i+1];
            if(x>14)
            {
                x = 15;
                sum += linbits;
            }
            if(y>14)
            {
                y = 15;
                sum += linbits;
            }

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

            if(x)
				sum++;
            if(y)
				sum++;
        }
    }
    else
    { /* No ESC-words */
        for(i=start;i<end;i+=2)
        {
            x = ix[i];
            y = ix[i+1];

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

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

    return sum;
}









/* 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 /*int[samp_per_frame2]*/, gr_info *cod_info)
{
    int bits,max;

    calc_runlen(ix,cod_info);		  /* rzero,count1,big_values*/

    max = ix_max( ix, 0,samp_per_frame2);
    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 */
    return bits;
}



int bin_search_StepSize(int desired_rate, double start, int *ix,
                        double xrs[samp_per_frame2], gr_info * cod_info)
{
    int top,bot,next,last;
    int bit;

    top  = start;
    next = start;
    bot  = 200;

    do
    {
        last = next;
        next = ((long)(top+bot) >> 1);

        cod_info->quantizerStepSize = next;
        quantize(xrs,ix,cod_info);
        bit = count_bits(ix,cod_info);

        if (bit>desired_rate)
			top = next;
        else
			bot = next;
    }
    while((bit!=desired_rate) && abs(last-next)>1);

    return next;
}


⌨️ 快捷键说明

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