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

📄 l3loop.c

📁 mp3 source code decoder & encoder
💻 C
📖 第 1 页 / 共 4 页
字号:
                     */
}


/***************************************************************************/ 
/*        calc_scfsi                                                       */ 
/***************************************************************************/ 
/* calculation of the scalefactor select information (scfsi)        */

void calc_scfsi(double  xr[576], L3_side_info_t *l3_side,
	    L3_psy_xmin_t *l3_xmin, int ch, int gr)
{
    static int en_tot[2][2]; /* ch,gr */
    static int en[2][2][21];
    static int xm[2][2][21];
    static int xrmax[2][2];

    int en_tot_krit        = 10;
    int en_dif_krit        = 100;
    int en_scfsi_band_krit = 10;
    int xm_scfsi_band_krit = 10;

    int scfsi_band;
    unsigned scfsi_set;

    int sfb, start, end, i;
    int condition = 0;
    double temp, log2 = log(2.0);
    gr_info *cod_info = &l3_side->gr[gr].ch[ch].tt;

    xrmax[gr][ch] = xr_max(xr, 0, 576);
    scfsi_set = 0;

    /* the total energy of the granule */    
    for (temp = 0.0, i = 0; i < 576; i++)
        temp += xr[i] * xr[i];
    if (temp == 0.0)
        en_tot[gr][ch] = 0.0;
    else
        en_tot[gr][ch] = log(temp) / log2 ;

/* the energy of each scalefactor band, en */
/* the allowed distortion of each scalefactor band, xm */

    if (cod_info->window_switching_flag == 0 || cod_info->block_type!=2)
        for(sfb=0;sfb<21;sfb++)
        {
            start = scalefac_band_long[ sfb ];
            end   = scalefac_band_long[ sfb+1 ];

            for (temp = 0.0, i = start; i < end; i++)
                temp += xr[i] * xr[i];
            if (temp == 0.0)
                en[gr][ch][sfb] = 0.0;
            else
                en[gr][ch][sfb] = log(temp)/ log2;

            if (l3_xmin->l[gr][ch][sfb] == 0.0)
                xm[gr][ch][sfb] = 0.0;
            else
                xm[gr][ch][sfb] = log(l3_xmin->l[gr][ch][sfb]) / log2;
        }
    if (gr==1)
    {
        int gr2, tp;

        for(gr2=0;gr2<2;gr2++)
        {
/* The spectral values are not all zero */
            if (xrmax[ch][gr2]!=0.0)
                condition++;
/* None of the granules contains short blocks */
            if ((cod_info->window_switching_flag==0) || (cod_info->block_type!=2))
                condition++;
        }
        if (abs(en_tot[0]-en_tot[1])<en_tot_krit) condition++;
        for(tp=0,sfb=0;sfb<21;sfb++) 
            tp += abs(en[ch][0][sfb]-en[ch][1][sfb]);
        if (tp<en_dif_krit) 
            condition++;

        if (condition==6)
        {
            for(scfsi_band=0;scfsi_band<4;scfsi_band++)
            {
                int sum0 = 0, sum1 = 0;
                l3_side->scfsi[ch][scfsi_band] = 0;
                start = scfsi_band_long[scfsi_band];
                end   = scfsi_band_long[scfsi_band+1];
                for (sfb = start; sfb < end; sfb++)
                { 
                    sum0 += abs(en[ch][0][sfb] - en[ch][1][sfb]);
                    sum1 += abs(xm[ch][0][sfb] - xm[ch][1][sfb]);
                }

                if (sum0<en_scfsi_band_krit && sum1<xm_scfsi_band_krit)
		{
                    l3_side->scfsi[ch][scfsi_band] = 1;
		    scfsi_set |= (1 << scfsi_band);
		}
                else
                    l3_side->scfsi[ch][scfsi_band] = 0;
            } /* for scfsi_band */
        } /* if condition == 6 */
        else
            for(scfsi_band=0;scfsi_band<4;scfsi_band++)
                l3_side->scfsi[ch][scfsi_band] = 0;
    } /* if gr == 1 */
}




int part2_length(L3_scalefac_t *scalefac, 
                 int gr, int ch, 
                 L3_side_info_t *si)
/***************************************************************************/ 
/* calculates the number of bits needed to encode the scalefacs in the     */
/* main data block                                                         */
/***************************************************************************/ 
{
    int slen1, slen2, bits, partition;
    gr_info *gi = &si->gr[gr].ch[ch].tt;

    bits = 0;
    if (config.mpeg.type==TYPE_MPEG_I)
    {
	static int slen1_tab[16] = { 0, 0, 0, 0, 3, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4 };
	static int slen2_tab[16] = { 0, 1, 2, 3, 0, 1, 2, 3, 1, 2, 3, 1, 2, 3, 2, 3 };

	slen1 = slen1_tab[ gi->scalefac_compress ];
	slen2 = slen2_tab[ gi->scalefac_compress ];

	if ((gi->window_switching_flag == 1) && (gi->block_type == 2))
	{
	    if (gi->mixed_block_flag)
	    {
		bits += (8 * slen1) + (9 * slen1) + (18 * slen2);
	    }
	    else
	    {
		bits += (18 * slen1) + (18 * slen2);
	    }
	}
	else
	{
	    if ((gr == 0) || (si->scfsi[ch][0] == 0))
		bits += (6 * slen1);

	    if ((gr == 0) || (si->scfsi[ch][1] == 0))
		/*  bits += (6 * slen1);  This is wrong SS 19-12-96 */
		bits += (5 * slen1);

	    if ((gr == 0) || (si->scfsi[ch][2] == 0))
		/*  bits += (6 * slen1);   This is wrong SS 19-12-96 */
		bits += (5 * slen2);

	    if ((gr == 0) || (si->scfsi[ch][3] == 0))
		/* bits += (6 * slen1);   This is wrong SS 19-12-96 */
		bits += (5 * slen2);
	}
    }
    else
    {   /* MPEG 2 */
	if (!gi->sfb_partition_table) ERROR("(!gi->sfb_partition_table)");
	for (partition = 0; partition < 4; partition++)
	    bits += gi->slen[partition] * gi->sfb_partition_table[partition];
    }
    return bits;
}





int scale_bitcount(L3_scalefac_t *scalefac, 
                   gr_info *cod_info,
		   int gr, int ch)
/*************************************************************************/
/* Also calculates the number of bits necessary to code the scalefactors. */
/*************************************************************************/
{
    int i, k, sfb, max_slen1 = 0, max_slen2 = 0, /*a, b, */ ep = 2;

    static int slen1[16] = { 0, 0, 0, 0, 3, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4 };
    static int slen2[16] = { 0, 1, 2, 3, 0, 1, 2, 3, 1, 2, 3, 1, 2, 3, 2, 3 };
    static int pow2[5]   = { 1, 2, 4, 8, 16 };

    if (cod_info->window_switching_flag != 0 && cod_info->block_type == 2)
    {
        if (cod_info->mixed_block_flag == 0) 
        {
            /* a = 18; b = 18;  */
            for (i = 0; i < 3; i++)
            {
                for (sfb = 0; sfb < 6; sfb++)
                    if (scalefac->s[gr][ch][sfb][i] > max_slen1)
                        max_slen1 = scalefac->s[gr][ch][sfb][i];
                for (sfb = 6; sfb < 12; sfb++)
                    if (scalefac->s[gr][ch][sfb][i] > max_slen2)
                        max_slen2 = scalefac->s[gr][ch][sfb][i];
            }
        }
        else
        {/* mixed_block_flag = 1 */
            /* a = 17; b = 18;  */
            for (sfb = 0; sfb < 8; sfb++)
                if (scalefac->l[gr][ch][sfb] > max_slen1)
                    max_slen1 = scalefac->l[gr][ch][sfb];
            for (i = 0; i < 3; i++)
            {
                for (sfb = 3; sfb < 6; sfb++)
                    if (scalefac->s[gr][ch][sfb][i] > max_slen1)
                        max_slen1 = scalefac->s[gr][ch][sfb][i];
                for (sfb = 6; sfb < 12; sfb++)
                    if (scalefac->s[gr][ch][sfb][i] > max_slen2)
                        max_slen2 = scalefac->s[gr][ch][sfb][i];
            }
        }
    }
    else
    { /* block_type == 1,2,or 3 */
        /* a = 11; b = 10;   */
        for (sfb = 0; sfb < 11; sfb++)
            if (scalefac->l[gr][ch][sfb] > max_slen1)
                max_slen1 = scalefac->l[gr][ch][sfb];
        for (sfb = 11; sfb < 21; sfb++)
            if (scalefac->l[gr][ch][sfb] > max_slen2)
                max_slen2 = scalefac->l[gr][ch][sfb];
    }

    for (k = 0; k < 16; k++)
    {
        if ((max_slen1 < pow2[slen1[k]]) && (max_slen2 < pow2[slen2[k]]))
        { 
            ep = 0;
            break;
        } 
    }

    if (ep == 0) cod_info->scalefac_compress = k;
    return ep;
}



void calc_noise(double xr[576], 
                int ix[576], 
                gr_info *cod_info,
	            double xfsf[4][CBLIMIT])
/*************************************************************************/
/*   Function: Calculate the distortion introduced by the quantization   */
/*   in each scale factor band.                                          */
/*************************************************************************/
{
    int start, end, sfb, l, i;
    double sum,step,bw;
    double (*xr_s)[192][3]; /*D192_3 *xr_s;*/
    int    (*ix_s)[192][3]; /*I192_3 *ix_s;*/

    xr_s = (double (*)[192][3]) xr;
    ix_s = (int    (*)[192][3]) ix;

    step = pow(2.0, (cod_info->quantizerStepSize) * 0.25);
    for(sfb=0;sfb<cod_info->sfb_lmax;sfb++)
    {
        start = scalefac_band_long[sfb];
        end   = scalefac_band_long[sfb+1];
        bw    = end - start;

        for(sum=0.0,l=start;l<end;l++)
        {
            double temp;
            if (ix[l]<PRECALC_SIZE)
                temp = fabs(xr[l]) - pow43[ix[l]] * step;
            else
            {
               temp = fabs(xr[l]) - pow((double)ix[l],4.0/3.0)*step;
#if defined (_DEBUG)
                printf("EHHHHHHH !?!?! ---> %d\n",ix[l]);
#endif
            }
            sum += temp * temp; 
        }
        xfsf[0][sfb] = sum / bw;
    }

    for (i = 0; i < 3; i++)
    {
        step = pow(2.0, (cod_info->quantizerStepSize) * 0.25); /* subblock_gain ? */
        for (sfb = cod_info->sfb_smax; sfb < 12; sfb++)
        {
            start = scalefac_band_short[ sfb ];
            end   = scalefac_band_short[ sfb+1 ];
	    bw = end - start;
            

            for (sum = 0.0, l = start; l < end; l++)
            {

                double temp;
                if ((*ix_s)[l][i]<PRECALC_SIZE)
                    temp = fabs((*xr_s)[l][i]) - pow43[(*ix_s)[l][i]] * step;
                else
                {
                    temp = fabs((*xr_s)[l][i]) - pow((double)(*ix_s)[l][i],4.0/3.0)*step;
#if defined (_DEBUG)
                    printf("EHHHHHHH !?!?! ---> %d\n",(*ix_s)[l][i]);
#endif
                }
                sum += temp * temp;
            }       
            xfsf[i+1][sfb] = sum / bw;
        }
    }
}





void calc_xmin(double xr[2][2][576], 
               L3_psy_ratio_t *ratio,
	       gr_info *cod_info, 
               L3_psy_xmin_t *l3_xmin,
	       int gr, int ch)
/*************************************************************************/
/*  Calculate the allowed distortion for each scalefactor band,          */
/*  as determined by the psychoacoustic model.                           */
/*  xmin(sb) = ratio(sb) * en(sb) / bw(sb)                               */
/*************************************************************************/
{
    int start, end, sfb, l, b;
    double en, bw;

    double (*xr_s)[192][3]; /*D192_3 *xr_s;*/

    xr_s = (double (*)[192][3]) xr[gr][ch] ;

    for (sfb = cod_info->sfb_smax; sfb < SFB_SMAX - 1; sfb++)
    {
        start = scalefac_band_short[ sfb ];
        end   = scalefac_band_short[ sfb + 1 ];
	bw = end - start;
        for (b = 0; b < 3; b++)
        {
            for (en = 0.0, l = start; l < end; l++)
                en += (*xr_s)[l][b] * (*xr_s)[l][b];
            l3_xmin->s[gr][ch][sfb][b] = ratio->s[gr][ch][sfb][b] * en / bw;
        }
    }

    for (sfb = 0; sfb < cod_info->sfb_lmax; sfb++)
    {
        start = scalefac_band_long[ sfb ];
        end   = scalefac_band_long[ sfb+1 ];
	bw = end - start;

        for (en = 0.0, l = start; l < end; l++)
            en += xr[gr][ch][l] * xr[gr][ch][l];
        l3_xmin->l[gr][ch][sfb] = ratio->l[gr][ch][sfb] * en / bw;
    }
}



int loop_break(L3_scalefac_t *scalefac, 
               gr_info *cod_info,
	       int gr, int ch)
/*************************************************************************/
/*  Function: Returns zero if there is a scalefac which has not been     */
/*   amplified. Otherwise it returns one.                                */
/*************************************************************************/
{
    int i, sfb, temp = 1;

    for (sfb = 0; sfb < cod_info->sfb_lmax; sfb++)
        if (scalefac->l[gr][ch][sfb] == 0)
            temp = 0;

    for (sfb = cod_info->sfb_smax; sfb < 12; sfb++)
        for (i = 0; i < 3; i++)
            if (scalefac->s[gr][ch][sfb][i] == 0)
                temp = 0;
    return temp;
}





void preemphasis(double xr[576], 
                 double xfsf[4][CBLIMIT],
       	         L3_psy_xmin_t  *l3_xmin,
	         int gr, int ch, 
                 L3_side_info_t *l3_side)
/*************************************************************************/
/*  See ISO 11172-3  section  C.1.5.4.3.4                                */
/*************************************************************************/
{
    int i, sfb, start, end, scfsi_band, over;
    double ifqstep;
    gr_info *cod_info = &l3_side->gr[gr].ch[ch].tt;

    if (gr == 1)
    {
/*
If the second granule is being coded and scfsi is active in
at least one scfsi_band, the preemphasis in the second granule
is set equal to the setting in the first granule
*/
	for (scfsi_band = 0; scfsi_band < 4; scfsi_band++)
	    if (l3_side->scfsi[ch][scfsi_band])
	    {
		cod_info->preflag = l3_side->gr[0].ch[ch].tt.preflag;
		return;
	    }
	
    }

/*
Preemphasis is switched on if in all the upper four scalefactor
bands the actual distortion exceeds the threshold after the
first call of the inner loop
*/
    if (cod_info->block_type != 2 && cod_info->preflag == 0)
    {	
	over = 0;
	for (sfb = 17; sfb < 21; sfb++)
	    if (xfsf[0][sfb] > l3_xmin->l[gr][ch][sfb])
		over++;

	if (over == 4)
	{
	    cod_info->preflag = 1;
	    ifqstep = (cod_info->scalefac_scale == 0) ? SQRT2
		: pow(2.0, (0.5 * (1.0 + (double) cod_info->scalefac_scale)));

	    for (sfb = 0; sfb < cod_info->sfb_lmax; sfb++)
	    {
		l3_xmin->l[gr][ch][sfb] *= pow(ifqstep, 2.0 * (double) pretab[sfb]);
		start = scalefac_band_long[ sfb ];
		end   = scalefac_band_long[ sfb+1 ];
		for(i = start; i < end; i++)

⌨️ 快捷键说明

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