G711转AAC代码总结【转】

来自:http://blog.csdn.net/qq_24551315/article/details/51134999

思路: 将G711转为PCM , 然后将PCM数据转为AAC,

G711转为PCM,可以使用上一篇中讲到的方式, 而PCM转AAC(ADTS),采用的是faac这个开源库

这里只讲怎么实现, 了解更详细的内容,则需要自己查找学习了.

直接上代码.

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. JNIEXPORT jint JNICALL Java_com_ff_aacdemo_jni_G711Coder_g711ToAAC  
  2.   (JNIEnv *env, jobject obj){  
  3.     unsigned long sampleRate = 8000;//采样率  
  4.     unsigned int channels = 1;//通道数  
  5.     unsigned int pcmBitSize = 16;// 量化位数  
  6.   
  7.     long inputSamples;  
  8.     long maxOutputBytes;  
  9.     faacEncHandle codeHandle = faacEncOpen(sampleRate, channels, &inputSamples, &maxOutputBytes);  
  10.     int maxInputBytes = inputSamples * pcmBitSize / 8;  
  11.     LOGD("inputSamples = %ld, maxOutputBytes = %ld, maxInputBytes = %d", inputSamples, maxOutputBytes, maxInputBytes);  
  12.   
  13.     faacEncConfigurationPtr pConfigPtr = faacEncGetCurrentConfiguration(codeHandle);  
  14.     pConfigPtr->inputFormat = FAAC_INPUT_16BIT;//输入数据类型  
  15.     pConfigPtr->outputFormat = 1; //0-Raw ; 1-ADTS  
  16.     pConfigPtr->useTns0;//瞬时噪声定形(temporal noise shaping,TNS)滤波器  
  17.     pConfigPtr->useLfe0;//低频效果  
  18.     pConfigPtr->aacObjectTypeLOW; //编码类型  
  19.     pConfigPtr->shortctl=SHORTCTL_NORMAL;  
  20.     pConfigPtr->quantqual=50; // 编码质量  
  21.     pConfigPtr->mpegVersion = MPEG2;  
  22.     faacEncSetConfiguration(codeHandle, pConfigPtr);  
  23.   
  24.     int m_nMaxInputBytes = inputSamples * pcmBitSize / 8;  
  25.     char pbPCMBuffer[m_nMaxInputBytes]; // 读取PCM数据  
  26.     char pbAACBuffer[maxOutputBytes];  
  27.     LOGD("g711topcm m_nMaxInputBytes = %d", m_nMaxInputBytes);  
  28.     LOGD("g711topcm maxOutputBytes = %d", maxOutputBytes);  
  29.   
  30.     FILE* fpIn = fopen("/storage/emulated/0/t/pcm_to_g711.g711","rb");  
  31.     FILE* fpOut = fopen("/storage/emulated/0/t/pcm_to_g711.aac", "wb");  
  32.   
  33.     size_t g711_BufferSize = m_nMaxInputBytes / 2;  
  34.     LOGD("g711topcm g711_BufferSize = %d", g711_BufferSize);  
  35.     char g711_Buffer[g711_BufferSize];//G711->PCM后,体积会变为原来的两倍,而PCM一次编码需要的最小字节数为m_nMaxInputBytes(如果小于它,编码后的数据不正常)  
  36.     size_t len;  
  37.   
  38.     LOGD("********************************");  
  39.     while((len = fread(g711_Buffer, 1, g711_BufferSize, fpIn)) > 0){  
  40.         LOGD("g711topcm length = %d", len);  
  41.         char pcmBuffer[len];  
  42.         int pcmbufsize = g711_decode(pcmBuffer, g711_Buffer, len); // g711 -> pcm  (g711 转为 pcm 后其体积会增加一倍)  
  43.         LOGD("g711topcm pcmbufsize = %d", pcmbufsize);  
  44.   
  45.         // pcm -> aac  
  46.         int inputSamples = pcmbufsize / (pcmBitSize / 8);  
  47.         LOGD("g711topcm inputSamples = %d", inputSamples);  
  48.         int nRetVal = faacEncEncode(codeHandle, (int*) pcmBuffer, inputSamples, pbAACBuffer, maxOutputBytes);  
  49.         LOGD("g711topcm nRetVal = %d", nRetVal);  
  50.         fwrite(pbAACBuffer, 1, nRetVal, fpOut);  
  51.         LOGD("----------------------------------");  
  52.     }  
  53.     fclose(fpIn);  
  54.     fclose(fpOut);  
  55.     faacEncClose(codeHandle);  
  56.     return 0;  
  57. }  

如果对AAC 的编解码不知道怎么弄的话, 可以看下一篇, 使用 faac, faad  实现 AAC 与 PCM 的互转
参考文章

http://blog.csdn.net/jwzhangjie/article/details/8782656
http://www.myexception.cn/program/1833150.html
http://iask.sina.com.cn/b/10699938.html
http://www.zhihu.com/question/20035259?utm_campaign=rss&utm_medium=rss&utm_source=rss&utm_content=title

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值