思路: 将G711转为PCM , 然后将PCM数据转为AAC,
G711转为PCM,可以使用上一篇中讲到的方式, 而PCM转AAC(ADTS),采用的是faac这个开源库
这里只讲怎么实现, 了解更详细的内容,则需要自己查找学习了.
直接上代码.
JNIEXPORT jint JNICALL Java_com_ff_aacdemo_jni_G711Coder_g711ToAAC (JNIEnv *env, jobject obj){ unsigned long sampleRate = 8000;//采样率 unsigned int channels = 1;//通道数 unsigned int pcmBitSize = 16;// 量化位数 long inputSamples; long maxOutputBytes; faacEncHandle codeHandle = faacEncOpen(sampleRate, channels, &inputSamples, &maxOutputBytes); int maxInputBytes = inputSamples * pcmBitSize / 8; LOGD("inputSamples = %ld, maxOutputBytes = %ld, maxInputBytes = %d", inputSamples, maxOutputBytes, maxInputBytes); faacEncConfigurationPtr pConfigPtr = faacEncGetCurrentConfiguration(codeHandle); pConfigPtr->inputFormat = FAAC_INPUT_16BIT;//输入数据类型 pConfigPtr->outputFormat = 1; //0-Raw ; 1-ADTS pConfigPtr->useTns= 0;//瞬时噪声定形(temporal noise shaping,TNS)滤波器 pConfigPtr->useLfe= 0;//低频效果 pConfigPtr->aacObjectType= LOW; //编码类型 pConfigPtr->shortctl=SHORTCTL_NORMAL; pConfigPtr->quantqual=50; // 编码质量 pConfigPtr->mpegVersion = MPEG2; faacEncSetConfiguration(codeHandle, pConfigPtr); int m_nMaxInputBytes = inputSamples * pcmBitSize / 8; char pbPCMBuffer[m_nMaxInputBytes]; // 读取PCM数据 char pbAACBuffer[maxOutputBytes]; LOGD("g711topcm m_nMaxInputBytes = %d", m_nMaxInputBytes); LOGD("g711topcm maxOutputBytes = %d", maxOutputBytes); FILE* fpIn = fopen("/storage/emulated/0/t/pcm_to_g711.g711","rb"); FILE* fpOut = fopen("/storage/emulated/0/t/pcm_to_g711.aac", "wb"); size_t g711_BufferSize = m_nMaxInputBytes / 2; LOGD("g711topcm g711_BufferSize = %d", g711_BufferSize); char g711_Buffer[g711_BufferSize];//G711->PCM后,体积会变为原来的两倍,而PCM一次编码需要的最小字节数为m_nMaxInputBytes(如果小于它,编码后的数据不正常) size_t len; LOGD("********************************"); while((len = fread(g711_Buffer, 1, g711_BufferSize, fpIn)) > 0){ LOGD("g711topcm length = %d", len); char pcmBuffer[len]; int pcmbufsize = g711_decode(pcmBuffer, g711_Buffer, len); // g711 -> pcm (g711 转为 pcm 后其体积会增加一倍) LOGD("g711topcm pcmbufsize = %d", pcmbufsize); // pcm -> aac int inputSamples = pcmbufsize / (pcmBitSize / 8); LOGD("g711topcm inputSamples = %d", inputSamples); int nRetVal = faacEncEncode(codeHandle, (int*) pcmBuffer, inputSamples, pbAACBuffer, maxOutputBytes); LOGD("g711topcm nRetVal = %d", nRetVal); fwrite(pbAACBuffer, 1, nRetVal, fpOut); LOGD("----------------------------------"); } fclose(fpIn); fclose(fpOut); faacEncClose(codeHandle); return 0; }
如果对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