www.pudn.com > S3C44B0X.mp3.rar > Layer3.c


/*********************************************** 
copyright by Haia Tech 
www.haia2004.com 
************************************************/ 
 
 
//#include  
//#include  
#include "common.h" 
#include "decode.h" 
 
unsigned int   totpos=0; 
unsigned char  buf[BITSTREAM_BUFSIZE]; 
unsigned int   frameNum=0,x4; 
extern Bit_stream_struc  bs; 
frame_params fr_ps;  
struct Granule grle[2][2]; 
short *pcm_dat; 
short led; 
 
extern unsigned short *mp3_dat; 
extern unsigned int buf_byte_idx; 
 
 
//FILE *file_p; 
 
void mp3() 
{ 
//	FILE *musicout; 
	III_side_info_t III_side_info; 
	III_scalefac_t III_scalefac; 
	layer info; 
	short pcm_sample[2][SSLIMIT][SBLIMIT]; 
	int i,j,k; 
 
/* 
	char *filename1="d:\\arm_app\\encode_mp3\\10.mp3"; 
	char *filename2="d:\\arm_app\\encode_mp3\\out.pcm" ; 
 
     
	 
	if ((musicout = fopen(filename2, "w+b")) == NULL) { 
		printf ("Could not create \"%s\".\n", filename2); 
		exit(1); 
	} 
 
	if ((file_p = fopen(filename1, "rb")) == NULL) { 
		printf("Could not find \"%s\".\n", filename1); 
		exit(1); 
	} 
*/ 
 
    mp3_dat=(unsigned short *)0x0c030000; 
    pcm_dat=(short *)0x0c100000; 
	fr_ps.header = &info;  
    open_bit_stream_r(); 
    frameNum=0; 
    totpos=0; 
    buf_byte_idx=0; 
    
	while(!bs.eobs) { 
		//尝试帧同步  
		if (!seek_sync(SYNC_WORD)) { 
//			printf("\nFrame cannot be located\n"); 
			break; 
		} 
		//解码帧头 
		decode_info( ); 
		//将fr_ps.header中的信息解读到fr_ps的相关域中 
		hdr_to_frps(); 
		//输出相关信息 
//		printf("\r%05lu", frameNum); 
        frameNum++; 
		if(led) 
		{ 
		  led=0; 
      	  *(unsigned int *)0x1d20044=0xff;    //LED is Off 
      	} 
		else 
		{ 
		  led=1; 
      	  *(unsigned int *)0x1d20044=0xf7;    //LED is ON 
      	} 
		 
		 
		if(frameNum>500) break; 
		 
		if (info.error_protection) 
			buffer_CRC(); 
		switch (info.lay) { 
		case 3: 
		{ 
			int nSlots, main_data_end, flush_main; 
			int bytes_to_discard, gr, ch; 
 
			static int frame_start = 0;     
 
 
			//取Side信息 
			III_get_side_info(&III_side_info); 
			nSlots = main_data_slots(); 
 
			 //读主数据(Audio Data) 
			for (; nSlots > 0; nSlots--)  /* read main data. */ 
				hputbuf((unsigned char) getbit(8)); 
			main_data_end = totpos / 8; /*of privious frame*/ 
			if ( flush_main=(totpos % bitsPerSlot) ) 
			{ 
				hgetbits((int)(bitsPerSlot - flush_main)); 
				main_data_end ++; 
			} 
			bytes_to_discard = frame_start - main_data_end - III_side_info.main_data_begin ; 
			/*    最多=1  */ 
			if( main_data_end > BITSTREAM_BUFSIZE ) 
			{ 
				frame_start -= BITSTREAM_BUFSIZE; 
				rewindNbytes( BITSTREAM_BUFSIZE);               //  totpos-=4096*8 
			} 
 
			frame_start += main_data_slots(); 
			if (bytes_to_discard < 0) { 
//				printf("Not enough main data to decode frame %d.  Frame discarded.\n", 
//						frameNum - 1);  
						break; 
			} 
			for (; bytes_to_discard > 0; bytes_to_discard--) 
				hgetbits(8); 
 
			for (gr=0;gr<2;gr++) 
			{ 
//				double hybridIn[2][SBLIMIT][SSLIMIT];/* Hybrid filter input */ 
				SS  hybridIn[2]; 
  			    //主解码 
				for (ch=0; ch