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

📄 l3loop.c

📁 mp3 source code decoder & encoder
💻 C
📖 第 1 页 / 共 4 页
字号:
          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)
                {
                    if ((line<0)||(line>576)) ERROR("Invalid line selected...");

                    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);
    }
}





int new_choose_table(int ix[576], 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==0) return 0;

    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;
	    }
	if (!choice[0]) ERROR("Can't choose a table ....");

	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;
	    }
	}
        if (!choice[0] || !choice[1]) ERROR("Hmmmppfff...");
	
	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 choose_table(int max)
/*************************************************************************/
/*            choose table                                               */
/*************************************************************************/
{
    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;
	    }
	}
    }
    if (!choice) ERROR("Can't decide what table to use...");
    return choice;
}




int bigv_bitcount(int ix[576], gr_info *gi)
/*************************************************************************/
/* Function: Count the number of bits necessary to code the bigvalues region.  */
/*************************************************************************/
{
    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;
        int (*ix_s)[192][3];

        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 = (int (*)[192][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 ];
            if (tableindex>=32) ERROR("No valid table...");

            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;
}



int count_bit(int ix[576], 
              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==0) 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!=0) sum++;
            if (y!=0) 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;
}






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

int count_bits(int *ix /*int[576]*/, 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 */
    return bits;
}



int bin_search_StepSize(int desired_rate, double start, int *ix,
                        double xrs[576], 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 + -