loop.c

来自「ISO mp3 sources (distribution 10) Layer」· C语言 代码 · 共 2,221 行 · 第 1/5 页

C
2,221
字号
int ix_max( int ix[576], unsigned int begin, unsigned int end ){    int i, max = 0;    for ( i = begin; i < end; i++ )    {        int x = abs( ix[i] );        if ( x > max )            max = x;    }    return max;}/*************************************************************************//*            xr_max                                                     *//*************************************************************************//*  Function: Calculate the maximum of xr[576]  from 0 to 575*/double xr_max( double xr[576], unsigned int begin, unsigned int end ){    int i;    double max = 0.0, temp;    for ( i = begin; i < end; i++ )        if( (temp = fabs(xr[i])) > max )	    max = temp;    return max;}/*        Noiseless coding  -- Huffman coding   *//*************************************************************************//*            calc_runlen                                                *//*************************************************************************//*Function: Calculation of rzero, count1, big_values(Partitions ix into big values, quadruples and zeros).*/void calc_runlen( int ix[576], gr_info *cod_info ){    int i;    int rzero = 0;     if ( cod_info->window_switching_flag && (cod_info->block_type == 2) )    {  /* short blocks */        cod_info->count1 = 0;        cod_info->big_values = 288;    }    else    {        for ( i = 576; i > 1; i -= 2 )            if ( ix[i-1] == 0 && ix[i-2] == 0 )                rzero++;            else                break;                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/2;    }    assert( (2 * rzero + 4 * cod_info->count1 + 2 * cod_info->big_values) == 576 );}/*************************************************************************//*            count1_bitcount                                            *//*************************************************************************//*  Determines the number of bits to encode the quadruples.*/int count1_bitcount( int ix[ 576 ], gr_info *cod_info ){    int abs_and_sign( int *x );    int p, i, k, bitsum_count1;    int v, w, x, y, signbits;    int sum0 = 0, sum1 = 0;    for ( i = cod_info->big_values * 2, k = 0; k < cod_info->count1; i += 4, k++ )    {        v = ix[ i ];        w = ix[ i + 1 ];        x = ix[ i + 2 ];        y = ix[ i + 3 ];                abs_and_sign( &v );        abs_and_sign( &w );        abs_and_sign( &x );        abs_and_sign( &y );        p = v + (w << 1) + (x << 2) + (y << 3);                signbits = 0;        if ( v != 0 )            signbits += 1;        if ( w != 0 )            signbits += 1;        if ( x != 0 )            signbits += 1;        if ( y != 0 )            signbits += 1;        sum0 += signbits;        sum1 += signbits;        sum0 += ht[ 32 ].hlen[ p ];        sum1 += ht[ 33 ].hlen[ p ];    }    if ( sum0 < sum1 )    {        bitsum_count1 = sum0;        cod_info->count1table_select = 0;    }    else    {        bitsum_count1 = sum1;        cod_info->count1table_select = 1;    }    return( bitsum_count1 );}struct{    unsigned region0_count;    unsigned region1_count;} subdv_table[ 23 ] ={{0, 0}, /* 0 bands */{0, 0}, /* 1 bands */{0, 0}, /* 2 bands */{0, 0}, /* 3 bands */{0, 0}, /* 4 bands */{0, 1}, /* 5 bands */{1, 1}, /* 6 bands */{1, 1}, /* 7 bands */{1, 2}, /* 8 bands */{2, 2}, /* 9 bands */{2, 3}, /* 10 bands */{2, 3}, /* 11 bands */{3, 4}, /* 12 bands */{3, 4}, /* 13 bands */{3, 4}, /* 14 bands */{4, 5}, /* 15 bands */{4, 5}, /* 16 bands */{4, 6}, /* 17 bands */{5, 6}, /* 18 bands */{5, 6}, /* 19 bands */{5, 7}, /* 20 bands */{6, 7}, /* 21 bands */{6, 7}, /* 22 bands */};/*************************************************************************//*            subdivide                                                  *//*************************************************************************//* presumable subdivides the bigvalue region which will   use separate Huffman tables.*/void subdivide( gr_info *cod_info ){    int scfb_anz = 0;    int bigvalues_region;        if ( cod_info->big_values == 0 )    { /* no big_values region */        cod_info->region0_count = 0;        cod_info->region1_count = 0;    }    else    {        bigvalues_region = 2 * cod_info->big_values;        if ( (cod_info->window_switching_flag == 0) )        { /* long blocks */            int thiscount, index;            /* Calculate scfb_anz */            while ( scalefac_band_long[scfb_anz] < bigvalues_region )                scfb_anz++;            assert( scfb_anz < 23 );            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 -= 1;                index -= 1;            }            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 -= 1;                index -= 1;            }            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;        }        else        {            if ( (cod_info->block_type == 2) && (cod_info->mixed_block_flag == 0) )            {                 cod_info->region0_count =  8;                cod_info->region1_count =  36;                cod_info->address1 = 36;                cod_info->address2 = bigvalues_region;                cod_info->address3 = 0;              }            else            {                cod_info->region0_count = 7;                cod_info->region1_count = 13;                cod_info->address1 = scalefac_band_long[cod_info->region0_count+1];                cod_info->address2 = bigvalues_region;                cod_info->address3 = 0;            }        }    }}/*************************************************************************//*            bigv_tab_select                                            *//*************************************************************************//*/*  Function: Select huffman code tables for bigvalues regions */void bigv_tab_select( int ix[576], gr_info *cod_info ){    /* int max; */    cod_info->table_select[0] = 0;    cod_info->table_select[1] = 0;    cod_info->table_select[2] = 0;        if ( cod_info->window_switching_flag && (cod_info->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, max1, max2, x, y;        int region1Start;        int *pmax;        region1Start = 12;        max1 = max2 = 0;        for ( sfb = 0; sfb < 13; sfb++ )        {            start = scalefac_band_short[ sfb ];            end   = scalefac_band_short[ sfb+1 ];                        if ( start < region1Start )                pmax = &max1;            else                pmax = &max2;                        for ( window = 0; window < 3; window++ )                for ( line = start; line < end; line += 2 )                {                    assert( line >= 0 );                    assert( line < 576 );                    x = abs( ix[ (line * 3) + window ] );                    y = abs( ix[ ((line + 1) * 3) + window ]);                    *pmax = *pmax > x ? *pmax : x;                    *pmax = *pmax > y ? *pmax : y;                }        }        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( 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 * 2 > cod_info->address2 )            cod_info->table_select[2] = new_choose_table( 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( int ix[576], unsigned int begin, unsigned int end ){    int i, max;    int choice[ 2 ];    int sum[ 2 ];    max = ix_max( ix, begin, end );    if ( max == 0 )        return 0;        max = abs( max );    choice[ 0 ] = 0;    choice[ 1 ] = 0;    if ( max < 15 )    {	/* try tables with no linbits */        for ( i = 0; i < 14; i++ )            if ( ht[i].xlen > max )	    {		choice[ 0 ] = i;                break;	    }	assert( choice[0] );	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;	  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] );	

⌨️ 快捷键说明

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