fdkaac是一个目前效率很高的aac编解码库。
使用该库编码aac的流程如下:
1,初始化aac库
HANDLE_AACENCODER m_aacEncHandle;
uint8_t m_aacOutbuf[20480];
if (aacEncOpen(&m_aacEncHandle, 0, channels) != AACENC_OK) {
printf("Unable to open fdkaac encoder\n");
return -1;
}
if (aacEncoder_SetParam(m_aacEncHandle, AACENC_AOT, 2) != AACENC_OK) { //aac lc
printf("Unable to set the AOT\n");
return -1;
}
if (aacEncoder_SetParam(m_aacEncHandle, AACENC_SAMPLERATE, sampleRate) != AACENC_OK) {
printf("Unable to set the AOT\n");
return -1;
}
if (aacEncoder_SetParam(m_aacEncHandle, AACENC_CHANNELMODE, MODE_2) != AACENC_OK) { //2 channle
printf("Unable to set the channel mode\n");
return -1;
}
if (aacEncoder_SetParam(m_aacEncHandle, AACENC_BITRATE, bitRate) != AACENC_OK) {
printf("Unable to set the bitrate\n");
return -1;
}
if (aacEncoder_SetParam(m_aacEncHandle, AACENC_TRANSMUX, 2) != AACENC_OK) { //0-raw 2-adts
printf("Unable to set the ADTS transmux\n");
return -1;
}
if (aacEncEncode(m_aacEncHandle, NULL, NULL, NULL, NULL) != AACENC_OK) {
printf("Unable to initialize the encoder\n");
return -1;
}
AACENC_InfoStruct info = { 0 };
if (aacEncInfo(m_aacEncHandle, &info) != AACENC_OK) {
printf("Unable to get the encoder info\n");
return -1;
}
AACENC_BufDesc in_buf = { 0 }, out_buf = { 0 };
AACENC_InArgs in_args = { 0 };
AACENC_OutArgs out_args = { 0 };
int in_identifier = IN_AUDIO_DATA;
int in_elem_size = 2;
in_args.numInSamples = size / 2; //size为pcm字节数
in_buf.numBufs = 1;
in_buf.bufs = &pData; //pData为pcm数据指针
in_buf.bufferIdentifiers = &in_identifier;
in_buf.bufSizes = &size;
in_buf.bufElSizes = &in_elem_size;
int out_identifier = OUT_BITSTREAM_DATA;
void *out_ptr = m_aacOutbuf;
int out_size = sizeof(m_aacOutbuf);
int out_elem_size = 1;
out_buf.numBufs = 1;
out_buf.bufs = &out_ptr;
out_buf.bufferIdentifiers = &out_identifier;
out_buf.bufSizes = &out_size;
out_buf.bufElSizes = &out_elem_size;
if ((aacEncEncode(m_aacEncHandle, &in_buf, &out_buf, &in_args, &out_args)) != AACENC_OK) {
fprintf(stderr, "Encoding aac failed\n");
return NVSTITCH_ERROR_GENERAL;
}
if (out_args.numOutBytes == 0)
return NVSTITCH_ERROR_GENERAL;
fwrite(outbuf, 1, out_args.numOutBytes, out); //编码后的aac数据存在outbuf中,大小为out_args.numOutBytes
3,最后销毁aac对象
aacEncClose(&m_aacEncHandle);