MP3解码--怎样解码mp3音频流,MP3Decoder(MP3流解码代码库)-《零度屏幕录像软件》

客官别急!
天气变冷,先来张美女图片暖暖身。。。。。。。

OK,进入主题:
工作需要,需要用到mp3流,注意是流不是文件,文件解码我已经有啦。好吧,没有现成的,网上搜吧,首先想到csdn网站,虽然现在csdn现在下载东西要积分了,(555,没钱充年卡啊),还是果断搜了搜,居然没有相关下载。好吧,自己写封装吧,不就是libmad库吗,应该很easy的。很快找到了以前下载保存的一个MP3解码的动态库,才100多K,不大啊,为什么要用动态库,静态的多好啊,于是,果断改成静态库,处女座的我终于舒服了。。。

来点代码:

/*
* This source code is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.
*       
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free 
* File Name: libmp3dec.h
*
* Reference
* Description:
*
*
*
*/
#ifndef __LIBMP3DEC_H__
#define __LIBMP3DEC_H__


#ifdef __cplusplus
extern "C" {
#endif


//typedef signed char  int8_t;
//typedef signed short int16_t;
//typedef signed int   int32_t;
//typedef unsigned char  uint8_t;
//typedef unsigned short uint16_t;
//typedef unsigned int   uint32_t;
//
//typedef signed __int64   int64_t;
//typedef unsigned __int64 uint64_t;
//
//#define SWAP32(val) (uint32_t)((((uint32_t)(val)) & 0x000000FF)<<24|\
//(((uint32_t)(val)) & 0x0000FF00)<<8 |\
//(((uint32_t)(val)) & 0x00FF0000)>>8 |\
//(((uint32_t)(val)) & 0xFF000000)>>24)
//
//#define HMP3DEC void*


void*MP3_decode_init();
uint32_t MP3_GetFrameSize(uint32_t head);
intMP3_GetAudioInfo(uint32_t* pnSampleRate, uint32_t *nChannels, uint32_t head);
intMP3_decode_frame(void* hDec, void *data, int *data_size, uint8_t *buf, int buf_size);
voidMP3_decode_close(void* hDec); 




#ifdef __cplusplus
}
#endif


#endif






class CMp3Decoder
{
private:
void* mp3;
BYTE *m_pOutPCM;




public:
CMp3Decoder(void)
{
mp3=0;
mp3=MP3_decode_init();
m_pOutPCM=0;
m_pOutPCM=new BYTE[1024*500];
}
~CMp3Decoder(void)
{
if(mp3)
{
MP3_decode_close(mp3);
mp3=0;
}
if(m_pOutPCM)
{
delete []m_pOutPCM;
m_pOutPCM=0;
}
}


void GetAudioInfo(int* pnSampleRate, int *nChannels, BYTE *buf)
{
uint32_t nSampleRate,nChannel;
MP3_GetAudioInfo(&nSampleRate, &nChannel, *(uint32_t*)buf);
*pnSampleRate=nSampleRate;
*nChannels=nChannel;
}
BYTE* Decode(BYTE *buf, int nMp3Len,int *pOutSize)
{
int nOutSize=0;
int nDecodeLen=0;
while(nDecodeLen<nMp3Len)
{
int nPCMLen=0;
int nFrameSize = MP3_GetFrameSize(*(uint32_t*)(buf+nDecodeLen));
MP3_decode_frame(mp3,m_pOutPCM+nDecodeLen,&nPCMLen,buf+nDecodeLen,nFrameSize);
nOutSize+=nPCMLen;
nDecodeLen+=nFrameSize;
}
*pOutSize=nOutSize;
return m_pOutPCM;
}

};

看到了吧,哥哥好贴心,都直接封装好了,可以自己用,CMp3Decoder这个类啊
函数1、void GetAudioInfo(int* pnSampleRate, int *nChannels, BYTE *buf)
这个函数很简单 啊,就是取得MP3信息的,后面那个参数是输入的MP3压缩数据,前4个byte就有这些信息。
函数2、BYTE* Decode(BYTE *buf, int nMp3Len,int *pOutSize)
直接返回PCM数据,buf是输入的MP3数据流,第二个参数是长度,第3个参数是输出的PCM长度。看下实现代码:
BYTE* Decode(BYTE *buf, int nMp3Len,int *pOutSize)

{
int nOutSize=0;
int nDecodeLen=0;
while(nDecodeLen<nMp3Len)
{
int nPCMLen=0;
int nFrameSize = MP3_GetFrameSize(*(uint32_t*)(buf+nDecodeLen));
MP3_decode_frame(mp3,m_pOutPCM+nDecodeLen,&nPCMLen,buf+nDecodeLen,nFrameSize);
nOutSize+=nPCMLen;
nDecodeLen+=nFrameSize;
}
*pOutSize=nOutSize;
return m_pOutPCM;
}
ps:为什么要用一个while语句判断,是因为实际工作中,编码出来的不止一帧数据,可能是2帧或者更多,这样就要判断是否把数据解码完了。根据nFrameSize 来判断需要解码的长度,然后移动指针,OK,完了,是不是很意外,很惊喜,很happy,直接拿去用吧,不需要很多积分哦!

哦,忘了贴下载地址了:
http://download.csdn.net/download/xjb2006/10042824

猜你喜欢

转载自blog.csdn.net/xjb2006/article/details/78375510
mp3