G711转AAC

来源:互联网 发布:气象数据网 编辑:程序博客网 时间:2024/05/19 13:27

在嵌入式系统中 ,因资源限制,一般音频采用G711压缩编码,但在上层应用中却很少直接支持G711,一般都市WMA,AAC,MP3等。

如视频系统 存储文件采用MP4格式存储 就不直接支持 G711,故需要转换成 AAC,普通播放器才能播放 。

分为2步

1.将 G711转成 PCM ------解码成原是流

int G711::ALawDecode(uint8_t alaw)
 {
 alaw ^= 0x55;  // A-law has alternate bits inverted for transmission

 unsigned sign = alaw&0x80;

 int linear = alaw&0x1f;
 linear <<= 4;
 linear += 8;  // Add a 'half' bit (0x08) to place PCM value in middle of range

 alaw &= 0x7f;
 if(alaw>=0x20)
  {
  linear |= 0x100;  // Put in MSB
  unsigned shift = (alaw>>4)-1;
  linear <<= shift;
  }

 if(!sign)
  return -linear;
 else
  return linear;
 }

unsigned G711::ALawDecode(int16_t* dst, const uint8_t* src, size_t srcSize)
 {
 int16_t* end = dst+srcSize;
 while(dst<end)
  *dst++ = ALawDecode(*src++);
 return srcSize<<1;
 }

data --------G711 编码数据 datalen 对应G711数据长度

 int sizePCM = G711::ALawDecode((short *)pbPCMBuffer,data,dataLen);


2.将PCM 转成 AAC--------编码成AAC

采用libfaac  http://www.audiocoding.com/downloads.html

定义变量:

faacEncHandle m_hEncoder;
unsigned long maxBytesOutput =0;
unsigned long nInputSamples = 0;
UINT nPCMBitSize = 16;
int  nPCMBufferSize;
BYTE* pbPCMBuffer = NULL;
BYTE* pbAACBuffer = NULL;

配置

 m_hEncoder = faacEncOpen(8000,1,&nInputSamples,&maxBytesOutput);
 pbAACBuffer = new unsigned char[maxBytesOutput];
 memset(pbAACBuffer,0,maxBytesOutput);

 nPCMBufferSize = nInputSamples * nPCMBitSize / 8;
    pbPCMBuffer = new BYTE [nPCMBufferSize];

 faacEncConfigurationPtr CurFormat=faacEncGetCurrentConfiguration(m_hEncoder);
 CurFormat->inputFormat=FAAC_INPUT_16BIT;
 faacEncSetConfiguration(m_hEncoder, CurFormat);

编码

 nInputSamples = (sizePCM) / (nPCMBitSize / 8);
 int nRet = faacEncEncode(m_hEncoder,
  (int*) pbPCMBuffer, nInputSamples, pbAACBuffer, maxBytesOutput);



0 0