G711轉AAC程式碼總結【轉】
阿新 • • 發佈:2019-02-20
思路: 將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 =
- unsigned int pcmBitSize = 16;// 量化位數
- long inputSamples;
- long maxOutputBytes;
- faacEncHandle codeHandle = faacEncOpen(sampleRate, channels, &inputSamples, &maxOutputBytes);
- int maxInputBytes = inputSamples * pcmBitSize / 8;
-
LOGD("inputSamples = %ld, maxOutputBytes
- faacEncConfigurationPtr pConfigPtr = faacEncGetCurrentConfiguration(codeHandle);
- pConfigPtr->inputFormat = FAAC_INPUT_16BIT;//輸入資料型別
- pConfigPtr->outputFormat = 1; //0-Raw ; 1-ADTS
-
pConfigPtr->useTns
- 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