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

📄 l3bitstream.c

📁 mp3文件格式与wav文件格式的音频文件转换工具
💻 C
📖 第 1 页 / 共 2 页
字号:
#include <stdlib.h>#include "l3bitstream.h" /* the public interface */#include "l3psy.h"#include "l3mdct.h"#include "l3loop.h"#include "formatBitstream.h" #include "huffman.h"#include "bitstream.h"#include "types.h"#include "tables.h"#include "error.h"/*static int stereo = 1;*/static bitstream_t *bs = NULL;BF_FrameData    *frameData    = NULL;BF_FrameResults *frameResults = NULL;int PartHoldersInitialized = 0;BF_PartHolder *headerPH;BF_PartHolder *frameSIPH;BF_PartHolder *channelSIPH[ MAX_CHANNELS ];BF_PartHolder *spectrumSIPH[ MAX_GRANULES ][ MAX_CHANNELS ];BF_PartHolder *scaleFactorsPH[ MAX_GRANULES ][ MAX_CHANNELS ];BF_PartHolder *codedDataPH[ MAX_GRANULES ][ MAX_CHANNELS ];BF_PartHolder *userSpectrumPH[ MAX_GRANULES ][ MAX_CHANNELS ];BF_PartHolder *userFrameDataPH;static int encodeSideInfo( L3_side_info_t  *si );static void encodeMainData(int l3_enc[2][2][576], L3_side_info_t  *si, L3_scalefac_t   *scalefac );static void write_ancillary_data( char *theData, int lengthInBits );static void drain_into_ancillary_data( int lengthInBits );static void Huffmancodebits( BF_PartHolder **pph, int *ix, gr_info *gi );void putMyBits( uint32 val, uint16 len ){    putbits( bs, val, len );}/*  L3_format_bitstream()    This is called after a frame of audio has been quantized and coded.  It will write the encoded audio to the bitstream. Note that  from a layer3 encoder's perspective the bit stream is primarily  a series of main_data() blocks, with header and side information  inserted at the proper locations to maintain framing. (See Figure A.7  in the IS).*/voidL3_format_bitstream( int              l3_enc[2][2][576],		      L3_side_info_t  *l3_side,		      L3_scalefac_t   *scalefac,		      bitstream_t *in_bs,		      double           (*xr)[2][576],		      char             *ancillary,		      int              ancillary_bits ){    int gr, ch, i;    bs = in_bs;        if ( frameData == NULL )    {	frameData = calloc( 1, sizeof(*frameData) );	if(!frameData) ERROR("ENOMEM !");    }    if ( frameResults == NULL )    {	frameResults = calloc( 1, sizeof(*frameData) );	if(!frameData) ERROR("ENOMEM !!");    }    if ( !PartHoldersInitialized )    {	headerPH = BF_newPartHolder( 12 );	frameSIPH = BF_newPartHolder( 12 );	for ( ch = 0; ch < MAX_CHANNELS; ch++ )	    channelSIPH[ch] = BF_newPartHolder( 8 );	for ( gr = 0; gr < MAX_GRANULES; gr++ )		    for ( ch = 0; ch < MAX_CHANNELS; ch++ )	    {		spectrumSIPH[gr][ch]   = BF_newPartHolder( 32 );		scaleFactorsPH[gr][ch] = BF_newPartHolder( 64 );		codedDataPH[gr][ch]    = BF_newPartHolder( 576 );		userSpectrumPH[gr][ch] = BF_newPartHolder( 4 );	    }	userFrameDataPH = BF_newPartHolder( 8 );	PartHoldersInitialized = 1;    }    for ( gr = 0; gr < config.mpeg.mode_gr; gr++ )	for ( ch =  0; ch < config.wave.channels; ch++ )	{	    int *pi = &l3_enc[gr][ch][0];	    double *pr = &xr[gr][ch][0];	    for ( i = 0; i < 576; i++, pr++, pi++ )	    {		if ( (*pr < 0) && (*pi > 0) )		    *pi *= -1;	    }	}    encodeSideInfo( l3_side );    encodeMainData( l3_enc, l3_side, scalefac );    write_ancillary_data( ancillary, ancillary_bits );    if ( l3_side->resvDrain )	drain_into_ancillary_data( l3_side->resvDrain );    /*      Put frameData together for the call      to BitstreamFrame()    */    frameData->putbits     = putMyBits;    frameData->frameLength = config.mpeg.bits_per_frame;    frameData->nGranules   = config.mpeg.mode_gr;    frameData->nChannels   = config.wave.channels;    frameData->header      = headerPH->part;    frameData->frameSI     = frameSIPH->part;    for ( ch = 0; ch < config.wave.channels; ch++ )	frameData->channelSI[ch] = channelSIPH[ch]->part;    for ( gr = 0; gr < config.mpeg.mode_gr; gr++ )	for ( ch = 0; ch < config.wave.channels; ch++ )	{	    frameData->spectrumSI[gr][ch]   = spectrumSIPH[gr][ch]->part;	    frameData->scaleFactors[gr][ch] = scaleFactorsPH[gr][ch]->part;	    frameData->codedData[gr][ch]    = codedDataPH[gr][ch]->part;	    frameData->userSpectrum[gr][ch] = userSpectrumPH[gr][ch]->part;	}    frameData->userFrameData = userFrameDataPH->part;    BF_BitstreamFrame( frameData, frameResults );    /* we set this here -- it will be tested in the next loops iteration */    l3_side->main_data_begin = frameResults->nextBackPtr;}void L3_FlushBitstream(){    if(!PartHoldersInitialized) ERROR("Not properly initialised !!");    BF_FlushBitstream( frameData, frameResults );}static unsigned slen1_tab[16] = { 0, 0, 0, 0, 3, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4 };static unsigned slen2_tab[16] = { 0, 1, 2, 3, 0, 1, 2, 3, 1, 2, 3, 1, 2, 3, 2, 3 };static void encodeMainData(int              l3_enc[2][2][576],	                   L3_side_info_t  *si,		           L3_scalefac_t   *scalefac ){    int i, gr, ch, sfb, window;    for ( gr = 0; gr < config.mpeg.mode_gr; gr++ )	for ( ch = 0; ch < config.wave.channels; ch++ )	    scaleFactorsPH[gr][ch]->part->nrEntries = 0;    for ( gr = 0; gr < config.mpeg.mode_gr; gr++ )	for ( ch = 0; ch < config.wave.channels; ch++ )	    codedDataPH[gr][ch]->part->nrEntries = 0;    if(config.mpeg.type==TYPE_MPEG_I)    {  /* MPEG 1 */	for ( gr = 0; gr < 2; gr++ )	{	    for ( ch = 0; ch < config.wave.channels; ch++ )	    {		BF_PartHolder **pph = &scaleFactorsPH[gr][ch];				gr_info *gi = &(si->gr[gr].ch[ch].tt);		unsigned slen1 = slen1_tab[ gi->scalefac_compress ];		unsigned slen2 = slen2_tab[ gi->scalefac_compress ];		int *ix = &l3_enc[gr][ch][0];		if ( (gi->window_switching_flag == 1) && (gi->block_type == 2) )		{		    if ( gi->mixed_block_flag )		    {			for ( sfb = 0; sfb < 8; sfb++ )			    *pph = BF_addEntry( *pph,  scalefac->l[gr][ch][sfb], slen1 );			for ( sfb = 3; sfb < 6; sfb++ )			    for ( window = 0; window < 3; window++ )				*pph = BF_addEntry( *pph,  scalefac->s[gr][ch][sfb][window], slen1 );			for ( sfb = 6; sfb < 12; sfb++ )			    for ( window = 0; window < 3; window++ )				*pph = BF_addEntry( *pph,  scalefac->s[gr][ch][sfb][window], slen2 );		    }		    else		    {			for ( sfb = 0; sfb < 6; sfb++ )			    for ( window = 0; window < 3; window++ )				*pph = BF_addEntry( *pph,  scalefac->s[gr][ch][sfb][window], slen1 );			for ( sfb = 6; sfb < 12; sfb++ )			    for ( window = 0; window < 3; window++ )				*pph = BF_addEntry( *pph,  scalefac->s[gr][ch][sfb][window], slen2 );		    }		}		else		{		    if ( (gr == 0) || (si->scfsi[ch][0] == 0) )			for ( sfb = 0; sfb < 6; sfb++ )			    *pph = BF_addEntry( *pph,  scalefac->l[gr][ch][sfb], slen1 );		    if ( (gr == 0) || (si->scfsi[ch][1] == 0) )			for ( sfb = 6; sfb < 11; sfb++ )			    *pph = BF_addEntry( *pph,  scalefac->l[gr][ch][sfb], slen1 );		    if ( (gr == 0) || (si->scfsi[ch][2] == 0) )			for ( sfb = 11; sfb < 16; sfb++ )			    *pph = BF_addEntry( *pph,  scalefac->l[gr][ch][sfb], slen2 );		    if ( (gr == 0) || (si->scfsi[ch][3] == 0) )			for ( sfb = 16; sfb < 21; sfb++ )			    *pph = BF_addEntry( *pph,  scalefac->l[gr][ch][sfb], slen2 );		}		Huffmancodebits( &codedDataPH[gr][ch], ix, gi );	    } /* for ch */	} /* for gr */    }    else    {  /* MPEG 2 */	gr = 0;	for ( ch = 0; ch < config.wave.channels; ch++ )	{	    BF_PartHolder **pph = &scaleFactorsPH[gr][ch];			    gr_info *gi = &(si->gr[gr].ch[ch].tt);	    int *ix = &l3_enc[gr][ch][0];	    int sfb_partition;	    if (! gi->sfb_partition_table ) ERROR("ERROR in part table");	    if ( (gi->window_switching_flag == 1) && (gi->block_type == 2) )	    {		if ( gi->mixed_block_flag )		{		    sfb_partition = 0;		    for ( sfb = 0; sfb < 8; sfb++ )			*pph = BF_addEntry( *pph,  scalefac->l[gr][ch][sfb], gi->slen[sfb_partition] );		    for ( sfb = 3, sfb_partition = 1; sfb_partition < 4; sfb_partition++ )		    {			int sfbs = gi->sfb_partition_table[ sfb_partition ] / 3;			int slen = gi->slen[ sfb_partition ];			for ( i = 0; i < sfbs; i++, sfb++ )			    for ( window = 0; window < 3; window++ )				*pph = BF_addEntry( *pph,  scalefac->s[gr][ch][sfb][window], slen );		    }		}		else		{		    for ( sfb = 0, sfb_partition = 0; sfb_partition < 4; sfb_partition++ )		    {			int sfbs = gi->sfb_partition_table[ sfb_partition ] / 3;			int slen = gi->slen[ sfb_partition ];			for ( i = 0; i < sfbs; i++, sfb++ )			    for ( window = 0; window < 3; window++ )				*pph = BF_addEntry( *pph,  scalefac->s[gr][ch][sfb][window], slen );		    }		}	    }	    else	    {		for ( sfb = 0, sfb_partition = 0; sfb_partition < 4; sfb_partition++ )		{		    int sfbs = gi->sfb_partition_table[ sfb_partition ];		    int slen = gi->slen[ sfb_partition ];		    for ( i = 0; i < sfbs; i++, sfb++ )			*pph = BF_addEntry( *pph,  scalefac->l[gr][ch][sfb], slen );		}	    }	    Huffmancodebits( &codedDataPH[gr][ch], ix, gi );	} /* for ch */    }} /* main_data */static unsigned int crc = 0;static int encodeSideInfo( L3_side_info_t  *si ){    int gr, ch, scfsi_band, region, window, bits_sent;        headerPH->part->nrEntries = 0;    headerPH = BF_addEntry( headerPH, 0xfff,                          12 );    headerPH = BF_addEntry( headerPH, config.mpeg.type,               1 );/* HEADER HARDCODED SHOULDN`T BE THIS WAY ! */    headerPH = BF_addEntry( headerPH, 1/*config.mpeg.layr*/,          2 );    headerPH = BF_addEntry( headerPH, !config.mpeg.crc,               1 );    headerPH = BF_addEntry( headerPH, config.mpeg.bitrate_index,      4 );    headerPH = BF_addEntry( headerPH, config.mpeg.samplerate_index,   2 );    headerPH = BF_addEntry( headerPH, config.mpeg.padding,            1 );    headerPH = BF_addEntry( headerPH, config.mpeg.ext,                1 );    headerPH = BF_addEntry( headerPH, config.mpeg.mode,               2 );    headerPH = BF_addEntry( headerPH, config.mpeg.mode_ext,           2 );    headerPH = BF_addEntry( headerPH, config.mpeg.copyright,          1 );    headerPH = BF_addEntry( headerPH, config.mpeg.original,           1 );    headerPH = BF_addEntry( headerPH, config.mpeg.emph,               2 );        bits_sent = 32;    if ( config.mpeg.crc )    {	headerPH = BF_addEntry( headerPH, crc, 16 );	bits_sent += 16;    }    frameSIPH->part->nrEntries = 0;    for (ch = 0; ch < config.wave.channels; ch++ )	channelSIPH[ch]->part->nrEntries = 0;    for ( gr = 0; gr < config.mpeg.mode_gr; gr++ )	for ( ch = 0; ch < config.wave.channels; ch++ )	    spectrumSIPH[gr][ch]->part->nrEntries = 0;    if (config.mpeg.type == TYPE_MPEG_I)    {  /* MPEG1 */	frameSIPH = BF_addEntry( frameSIPH, si->main_data_begin, 9 );	if ( config.wave.channels == 2 )	    frameSIPH = BF_addEntry( frameSIPH, si->private_bits, 3 );	else	    frameSIPH = BF_addEntry( frameSIPH, si->private_bits, 5 );		for ( ch = 0; ch < config.wave.channels; ch++ )	    for ( scfsi_band = 0; scfsi_band < 4; scfsi_band++ )	    {		BF_PartHolder **pph = &channelSIPH[ch];		*pph = BF_addEntry( *pph, si->scfsi[ch][scfsi_band], 1 );	    }	for ( gr = 0; gr < 2; gr++ )	    for ( ch = 0; ch < config.wave.channels ; ch++ )	    {		BF_PartHolder **pph = &spectrumSIPH[gr][ch];		gr_info *gi = &(si->gr[gr].ch[ch].tt);		*pph = BF_addEntry( *pph, gi->part2_3_length,        12 );		*pph = BF_addEntry( *pph, gi->big_values,            9 );		*pph = BF_addEntry( *pph, gi->global_gain,           8 );		*pph = BF_addEntry( *pph, gi->scalefac_compress,     4 );		*pph = BF_addEntry( *pph, gi->window_switching_flag, 1 );		if ( gi->window_switching_flag )		{   		    *pph = BF_addEntry( *pph, gi->block_type,       2 );		    *pph = BF_addEntry( *pph, gi->mixed_block_flag, 1 );		    for ( region = 0; region < 2; region++ )			*pph = BF_addEntry( *pph, gi->table_select[region],  5 );		    for ( window = 0; window < 3; window++ )			*pph = BF_addEntry( *pph, gi->subblock_gain[window], 3 );		}		else		{		    if( gi->block_type!=0 ) ERROR("ERROR; invalid blocktype");		    for ( region = 0; region < 3; region++ )			*pph = BF_addEntry( *pph, gi->table_select[region], 5 );		    *pph = BF_addEntry( *pph, gi->region0_count, 4 );		    *pph = BF_addEntry( *pph, gi->region1_count, 3 );		}		*pph = BF_addEntry( *pph, gi->preflag,            1 );		*pph = BF_addEntry( *pph, gi->scalefac_scale,     1 );		*pph = BF_addEntry( *pph, gi->count1table_select, 1 );	    }	if ( config.wave.channels == 2 )	    bits_sent += 256;	else	    bits_sent += 136;    }    else    {  /* MPEG2 */	frameSIPH = BF_addEntry( frameSIPH, si->main_data_begin, 8 );	if ( config.wave.channels  == 2 )	    frameSIPH = BF_addEntry( frameSIPH, si->private_bits, 2 );	else	    frameSIPH = BF_addEntry( frameSIPH, si->private_bits, 1 );		gr = 0;	for ( ch = 0; ch < config.wave.channels ; ch++ )	{	    BF_PartHolder **pph = &spectrumSIPH[gr][ch];	    gr_info *gi = &(si->gr[gr].ch[ch].tt);	    *pph = BF_addEntry( *pph, gi->part2_3_length,        12 );	    *pph = BF_addEntry( *pph, gi->big_values,            9 );	    *pph = BF_addEntry( *pph, gi->global_gain,           8 );	    *pph = BF_addEntry( *pph, gi->scalefac_compress,     9 );	    *pph = BF_addEntry( *pph, gi->window_switching_flag, 1 );	    if ( gi->window_switching_flag )	    {   		*pph = BF_addEntry( *pph, gi->block_type,       2 );		*pph = BF_addEntry( *pph, gi->mixed_block_flag, 1 );		for ( region = 0; region < 2; region++ )		    *pph = BF_addEntry( *pph, gi->table_select[region],  5 );		for ( window = 0; window < 3; window++ )		    *pph = BF_addEntry( *pph, gi->subblock_gain[window], 3 );	    }	    else	    {

⌨️ 快捷键说明

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