主要程序是这样的: #include "basic_io.h" #include "SD_Card.h" #include <math.h> #include "system.h" #include "common.h" #include "decode.h" #include "sys/alt_flash.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 dataout[4096]; extern unsigned short *mp3_dat; extern unsigned int buf_byte_idx; int mp3(unsigned short *mp3data) { int a; III_side_info_t III_side_info; III_scalefac_t III_scalefac; layer info; short pcm_sample[2][SSLIMIT][SBLIMIT]; int i,j,k; mp3_dat=mp3data; fr_ps.header = &info; open_bit_stream_r(); frameNum=0; totpos=0; buf_byte_idx=0; while(!bs.eobs) {
if (!seek_sync(SYNC_WORD)) { break; } decode_info( ); hdr_to_frps(); frameNum++; 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; III_get_side_info(&III_side_info); nSlots = main_data_slots(); 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 ; 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) { break; } for (; bytes_to_discard > 0; bytes_to_discard--) hgetbits(8); for (gr=0;gr<2;gr++) { /* Hybrid filter input */ 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]); Granule_imdct(&grle[ch][gr], ch, hybridIn[ch]); 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); /* Output PCM sample points for one granule*/ for(i=0;i<2;i++) for(j=0;j<SSLIMIT;j++) for(k=0;k<SBLIMIT;k++) { dataout[a]=pcm_sample[j][k]; a++; } } } break; default: break; } } return 0; }
/////////////////////////// int main(void) { unsigned short voicedata[512]={0}; int m,j; alt_flash_fd *fd; fd = alt_flash_open_dev(CFI_FLASH_0_NAME); int ret_code; if (fd) {printf("open flash device successfully\n"); for(j=0;j<4;j++) {ret_code=alt_read_flash(fd, j*1024, voicedata, 1024); if(!ret_code) { printf("read flash success!"); mp3(voicedata); printf("decode is over!"); for(m=0;m<4096;m++ ) { while(IORD(AUD_FULL_BASE,0)); IOWR(AUDIO_0_BASE,0,dataout[m]); IOWR(SEG7_DISPLAY_BASE,0,m); } } alt_flash_close_dev(fd); } } else printf("Can't open flash!"); return 0; } |