MP3_Layer3.c
上传用户:kingbiz
上传日期:2022-06-24
资源大小:2524k
文件大小:9k
- #include "Include.h"
- #include "mp3_common.h"
- #include "mp3_decode.h"
- /*============================================================================*/
- /*============================================================================*/
- U32 totpos=0;
- U8 *mp3_src;
- U32 mp3_dat_size;
- U32 frameNum=0;
- S8 bs_buf[BITSTREAM_BUFSIZE];
- float s_freq[4] = {44.1, 48, 32, 0};
- struct Granule grle[2][2];
- Bit_stream_struc bs;
- frame_params fr_ps;
- /*============================================================================*/
- static S16 *pcm_dat;
- static U16 *mp3_dat;
- static S16 pcm_sample[2][SSLIMIT][SBLIMIT];
- static S16 pcm_buf0[0x100000];
- static S16 pcm_buf1[0x100000];
- static int pcm_size0;
- static int pcm_size1;
- static int pcm_state0=0;
- static int pcm_state1=0;
- /*============================================================================*/
- #define IN_PLAY 1
- #define IN_DECODE 2
- /*============================================================================*/
- /*============================================================================*/
- /*============================================================================*/
- /*============================================================================*/
- /*============================================================================*/
- int MP3Decode(void *pPCM,void *pMp3,U32 size)
- {
- III_side_info_t III_side_info;
- III_scalefac_t III_scalefac;
- layer info;
- int i,j,k;
- ////
-
- U32 pcm_size;
- ////
-
- mp3_dat =pMp3;
- mp3_src =pMp3;
- // mp3_dat_size =0x40000;
- mp3_dat_size =size;
-
- // pcm_dat=(short *)pPCM;
- pcm_dat=pPCM;
-
- fr_ps.header = &info;
- open_bit_stream_r();
- frameNum =0;
- totpos =0;
- buf_byte_idx=0;
- pcm_size =0;
- ////
- DbgUart_Printf("MP3 Decode Startn");
- DbgUart_Printf("MP3 Decode FileSize =%dn",size);
- while(!bs.eobs)
- {
- //Sleep(1);
- if(frameNum&0x03)
- {
- Sleep(1);
- }
- ////
-
- //尝试帧同步
- if (!seek_sync(SYNC_WORD))
- {
- DbgUart_Printf("Frame cannot be locatedn");
- break;
- }
-
- //解码帧头
- decode_info();
-
- //将fr_ps.header中的信息解读到fr_ps的相关域中 fr_ps.header
- hdr_to_frps();
- //输出相关信息
-
- frameNum++;
-
- if((frameNum&0x0f)==0x0f)
- {
- Sleep(1);
- DbgUart_Putch('>');
- }
-
- if(frameNum>0xfffffff)
- {
- DbgUart_Printf("frm ovf:%dn", frameNum);
- break;
- }
- ////
-
- if (info.error_protection)
- {
- //DbgUart_Printf("crcn");
- 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)
- {
- DbgUart_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++)
- {
- SS hybridIn[2];
-
- //主解码
-
- for (ch=0; ch<fr_ps.stereo; ch++)
- {
- int is[SBLIMIT][SSLIMIT]; /*保存量化数据 频率线*/
- int part2_start;
- part2_start=totpos;
- ////
-
- //获取比例因子
- III_get_scale_factors(&III_scalefac,&III_side_info, gr, ch);
-
- //Huffman解码
- III_hufman_decode(&grle[ch][gr],part2_start,is);
- //逆量化采样
- III_dequantize_sample(is, hybridIn[ch], &III_scalefac, &grle[ch][gr], ch);
-
- }
- //两个声道处理完,进行立体声处理
-
- III_stereo(hybridIn, &III_scalefac, &grle[0][gr]);
- for (ch=0; ch<fr_ps.stereo; ch++)
- {
- //重新排序
- III_reorder(hybridIn[ch], &grle[ch][gr]);
-
- //反混叠处理
- III_antialias(hybridIn[ch],&grle[ch][gr]);
-
- //IMDCT 加窗 叠加
- Granule_imdct(&grle[ch][gr], ch, hybridIn[ch]);
-
- //多相频率倒置 X(-1)
- Granule_freqinverse(hybridIn[ch]);
- }
-
-
- //多相合成
- if(fr_ps.stereo == 2)
- {
- Granule_subband_synthesis2(hybridIn[0],hybridIn[1],pcm_sample);
- }
- else
- { // 单声道处理
- // Granule_subband_synthesis( 0,hybridIn[0],pcm_sample);
- Granule_subband_synthesis( 1,hybridIn[1],pcm_sample);
- }
-
- /* Output PCM sample points for one granule(颗粒). */
- // fwrite(pcm_sample,2,SBLIMIT*SSLIMIT*2,musicout);
-
-
- // pcm_dat =pcm_buf;
-
-
-
- for(i=0;i<2;i++)
- {
- for(j=0;j<SSLIMIT;j++)
- {
- for(k=0;k<SBLIMIT;k++)
- {
- *pcm_dat++=pcm_sample[i][j][k];
-
-
- }
- }
- }
-
-
- ////
-
- LabelPrintf(HDC_SCREEN,8,GUI_YSIZE-80,64,24,RGB565(0,0,24),RGB565(24,48,24),RGB565(0,0,0),CENTER,"%02d:%02d",(frameNum*261)/10000/60,(frameNum*261)/10000%60);
- // DbgUart_Printf("pcm_size=%dn",pcm_size);
-
- if(1)
- {
- char key;
- ////
-
- key=0;
- DbgUart_Getch(&key);
- if(key=='q')
- {
- DbgUart_Printf("MP3 Decode Abort!n");
- goto end;
- }
-
- }
- /////////////
-
-
- pcm_size +=2*SSLIMIT*SBLIMIT*sizeof(U16);
- if(frameNum>50)
- {
- static int i=0;
- ////
- if(i==0)
- {
- PCM_Output(pPCM,pcm_size);
- i=1;
- }
- else
- {
- PCM_SetSize(pcm_size);
-
- }
-
-
- }
- //////////////
-
-
- }
- }
- break;
- ////////
-
- default:
- DbgUart_Printf("Only layer III supported!n");
- goto end;
- break;
- ////////
-
- }
- }
- //*(unsigned int *)0x1d20044=0xff; //LED is Off
- //while(1);
- end:
- DbgUart_Printf("n");
- DbgUart_Printf("MP3 Decode FrameNum=%dn",frameNum);
- DbgUart_Printf("MP3 Decode PCM Size=%dn",pcm_size);
- DbgUart_Printf("MP3 Decode Endn");
- Sleep(100);
- return 1;
- }
- /*============================================================================*/
- /*============================================================================*/
- void MP3_Test(void)
- {
-
- char key;
- U32 fsize,i,hFile;
- U8 *pcm_buf,*mp3_buffer;
-
- ////
-
- hFile=FileOpen("a:\test.mp3","rw");
- if(hFile==-1)
- {
- DbgUart_Printf("Nof Find Filen");
- return;
- }
- mp3_buffer=MemMalloc(GetFileSize(hFile));
- i=FileRead(mp3_buffer,0,GetFileSize(hFile),hFile);
- DbgUart_Printf("Read:hFile=%d;size=%dn",hFile,i);
-
-
- fsize =GetFileSize(hFile);
- pcm_buf =(U8*)0x30008000;
- FileClose(hFile);
- ////
-
- DbgUart_Printf("MP3 Test Startn");
-
-
- PCM_DrvInit();
- PCM_SetVolume(0xc0);
- PCM_SetSampleRate(16,44100);
-
-
- TimerSet(0);
-
- MP3Decode(pcm_buf,mp3_buffer,fsize);
-
- i=TimerGet();
- DbgUart_Printf("MP3DecodeTime =%d ( %02d:%02d )n",i,(i>>6)/60,(i>>6)%60);
-
-
- PCM_DrvClose();
- ////
- DbgUart_Printf("MP3 Test End,Press 'q' Exitn");
- ////
- while(1)
- {
- key=0;
- DbgUart_Getch(&key);
- if(key=='q') break;
- Sleep(5);
- }
-
- MemFree(mp3_buffer);
-
- }
- /*============================================================================*/
- /*============================================================================*/