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

📄 loop.c

📁 MPEG 2的音频编码软件。喜欢多媒体的开发人员可以看看。
💻 C
📖 第 1 页 / 共 5 页
字号:
    fflush(log_output);
    }
#endif
        over = amp_scalefac_bands( &xr[gr][ch][0], xfsf, l3_xmin,
                                   l3_side, scalefac, gr, ch, iteration );

#if 1
        if ( (status = loop_break(scalefac, cod_info, gr, ch)) == 0 )
	{
	    if ( fr_ps->header->version == 1 )
		status = scale_bitcount( scalefac, cod_info, gr, ch );
	    else
		status = scale_bitcount_lsf( scalefac, cod_info, gr, ch );
	}
#else
        status = loop_break( scalefac, cod_info, gr, ch );
	if ( fr_ps->header->version == 1 )
	    status += scale_bitcount( scalefac, cod_info, gr, ch );
	else
	    status += scale_bitcount_lsf( scalefac, cod_info, gr, ch );
#endif

    }
    while ( (status == 0) && (over > 0) );

    cod_info->preflag = save_preflag;
    cod_info->scalefac_compress = save_compress;

    for ( sfb = 0; sfb < 21; sfb++ )
        scalefac->l[gr][ch][sfb] = scalesave_l[sfb];    

    for ( i = 0; i < 3; i++ )
        for ( sfb = 0; sfb < 12; sfb++ )
            scalefac->s[gr][ch][sfb][i] = scalesave_s[sfb][i];    

    cod_info->part2_length   = part2_length( scalefac, fr_ps, gr, ch, l3_side );
    cod_info->part2_3_length = cod_info->part2_length + bits;

#ifdef PERFORM 
    if(frameNum == PERFORM)
    {
    fprintf(log_output,"\n  Final Results\n");
    fprintf(log_output,"bits = %d  huff_bits= %d", bits,huff_bits);
    fprintf(log_output," max_bits = %d\n",max_bits);
    fprintf(log_output," Stepsize = %f ",cod_info->quantizerStepSize);
    fprintf(log_output, " scale_bits    = %d \n", cod_info->part2_length );  
    max_ratio = worst_xfsf_to_xmin_ratio(l3_xmin,xfsf,cod_info->block_type,gr,ch);
    print_scalefacs(log_output,scalefac,cod_info->block_type,gr,ch);
    fprintf(log_output,"max_ratio = %6.2f\n",max_ratio );
    print_ratios(log_output,l3_xmin,xfsf,cod_info->block_type,gr,ch);
    fflush(log_output);
    }
#endif    
    return cod_info->part2_3_length;
}




/***************************************************************************/ 
/*         inner_loop                                                      */ 
/***************************************************************************/ 
/* The code selects the best quantizerStepSize for a particular set
/* of scalefacs                                                            */
 
int
inner_loop( double xr[2][2][576], int l3_enc[2][2][576], int max_bits,
	    gr_info *cod_info, int gr, int ch )
{
    int bits, c1bits, bvbits;
    double *xrs;  /*  D576  *xr; */
    int *ix;  /*  I576  *ix; */
    xrs = &xr[gr][ch][0];
    ix = l3_enc[gr][ch];

    assert( max_bits >= 0 );
    cod_info->quantizerStepSize -= 1.0;;
    do
    {
        do
        {
            cod_info->quantizerStepSize += 1.0;
            quantize( xrs, ix, cod_info );
        }
        while ( ix_max(ix, 0, 576) > 8191 + 14 ); /* within table range? */

        calc_runlen( ix, cod_info );  /*rzero,count1,big_values*/
        bits = c1bits = count1_bitcount( ix, cod_info );  /*count1_table selection*/
        subdivide( cod_info );  /* bigvalues sfb division */
        bigv_tab_select( ix, cod_info );  /* codebook selection*/
        bits += bvbits = bigv_bitcount( ix, cod_info );  /* bit count */

#ifdef PERFORM
        if(frameNum == PERFORM)
	fprintf(log_output,"StepSize=%f bits = %d huff_bits = %d\n",
           cod_info->quantizerStepSize,bits,max_bits);
#endif

    }
    while ( bits > max_bits );

    return bits;
}



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

void calc_scfsi( double  xr[576], III_side_info_t *l3_side,
	    III_psy_xmin *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 */
#ifdef DEBUG
	    fprintf( stderr, "calc_scfsi: scfsi_set = 0x%02x\n", scfsi_set );
#endif
        } /* if condition == 6 */
        else
            for ( scfsi_band = 0; scfsi_band < 4; scfsi_band++ )
                l3_side->scfsi[ch][scfsi_band] = 0;
    } /* if gr == 1 */
}



/***************************************************************************/ 
/*        part2_length                                                     */ 
/***************************************************************************/ 

/* calculates the number of bits needed to encode the scalefacs in the     */
/* main data block                                                         */

int part2_length( III_scalefac_t *scalefac, frame_params *fr_ps,
	      int gr, int ch, III_side_info_t *si )
{
    int slen1, slen2, slen3, slen4, bits, sfb, window, partition;
    gr_info *gi = &si->gr[gr].ch[ch].tt;

    bits = 0;
    if ( fr_ps->header->version == 1 )
    {
	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 */
	assert( gi->sfb_partition_table );
	for ( partition = 0; partition < 4; partition++ )
	    bits += gi->slen[partition] * gi->sfb_partition_table[partition];
    }
    return bits;
}



/*************************************************************************/
/*            scale_bitcount                                             */
/*************************************************************************/

/* Also calculates the number of bits necessary to code the scalefactors. */

int scale_bitcount( III_scalefac_t *scalefac, gr_info *cod_info,
		int gr, int ch )
{
    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;
#ifdef DEBUG
    if ( ep != 0 ) 
        printf( "---WARNING !! Amplification of some bands over limits\n" );
#endif
    return ep;
}




/*************************************************************************/
/*            scale_bitcount_lsf                                         */
/*************************************************************************/

/* Also counts the number of bits to encode the scalefacs but for MPEG 2 */ 
/* Lower sampling frequencies  (24, 22.05 and 16 kHz.)                   */
 
/*  This is reverse-engineered from section 2.4.3.2 of the MPEG2 IS,     */
/* "Audio Decoding Layer III"                                            */

int scale_bitcount_lsf( III_scalefac_t *scalefac, gr_info *cod_info,
		    int gr, int ch )
{
    int table_number, row_in_table, partition, nr_sfb, window, over;
    int i, k, sfb, max_sfac[ 4 ];
    unsigned *partition_table;

    /*
      Set partition table. Note that should try to use table one,
      but do not yet...
    */
    if ( cod_info->preflag )
	table_number = 2;
    else
	table_number = 0;

    for ( i = 0; i < 4; i++ )
	max_sfac[i] = 0;

    if ( cod_info->window_switching_flag != 0 && cod_info->block_type == 2 )
    {
        if ( cod_info->mixed_block_flag == 0 ) 
        {
	    row_in_table = 1;
	    partition_table = &nr_of_sfb_block[table_number][row_in_table][0];
	    for ( sfb = 0, partition = 0; partition < 4; partition++ )
	    {
		nr_sfb = partition_table[ partition ] / 3;
		for ( i = 0; i < nr_sfb; i++, sfb++ )
		    for ( window = 0; window < 3; window++ )
			if ( scalefac->s[gr][ch][sfb][window] > max_sfac[partition] )
			    max_sfac[partition] = scalefac->s[gr][ch][sfb][window];
	    }
        }
        else
        {/* mixed_block_flag = 1 */
	    row_in_table = 2;
	    partition_table = &nr_of_sfb_block[table_number][row_in_table][0];
	    partition = 0;
	    nr_sfb = partition_table[ partition ];
	    for ( i = 0; i < nr_sfb; i++, sfb++ )
		if ( scalefac->l[gr][ch][sfb] > max_sfac[partition] )
		    max_sfac[partition] = scalefac->l[gr][ch][sfb];
	    
	    for ( sfb = 0, partition = 1; partition < 4; partition++ )
	    {
		nr_sfb = partition_table[ partition ] / 3;
		for ( i = 0; i < nr_sfb; i++, sfb++ )
		    for ( window = 0; window < 3; window++ )
			if ( scalefac->s[gr][ch][sfb][window] > max_sfac[partition] )
			    max_sfac[partition] = scalefac->s[gr][ch][sfb][window];
	    }
        }
    }
    else
    {
	row_in_table = 0;
	partition_table = &nr_of_sfb_block[table_number][row_in_table][0];
	partition = 0;
	for ( sfb = 0, partition = 0; partition < 4; partition++ )
	{
	    nr_sfb = partition_table[ partition ];
	    for ( i = 0; i < nr_sfb; i++, sfb++ )
		if ( scalefac->l[gr][ch][sfb] > max_sfac[partition] )
		    max_sfac[partition] = scalefac->l[gr][ch][sfb];
	}
    }

    for ( over = 0, partition = 0; partition < 4; partition++ )
    {
	if ( max_sfac[partition] > max_sfac_tab[table_number][partition] )
	    over++;
    }
    if ( !over )

⌨️ 快捷键说明

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