(1)在github上下载fdk aac.
(2)编译:
./autogen.sh
./configure --enable-example
make
make install
(3)在fdk-aac-master下,能看到aac-enc的测试程序,但指定aot(audio of type)是23(aac ld)时,提示Unable to initialize the encoder.
解决方法:
aac ld由于是low delay(低延时)的编码,适用于实时音视频通信,而aac-enc里默认的传输类型是TT_MP4_ADTS,适用于aac lc的编码,用于流媒体传输,而aac ld的传输类型需要改为TT_MP4_RAW,另外需要记录编码后的每一帧的长度,通过带外方法传输,以辅助解码.
二次开发,依赖项如下:
-I/usr/local/include/fdk-aac
-L/root/aac/fdk-aac-master/.libs
-lfdk-aac
运行需要动态链接libfdk-aac.so.2.0
测试代码如下:
#include
#include
#include
#include
#include "aacenc_lib.h"
#include "wavreader.h"
#include
using namespace std;
int main(int argc, char *argv[]) {
int bitrate;
const char *infile, *outfile,*lengthfile;
FILE *out;
FILE *lengthHandle;
void *wav;
int format, sample_rate, channels, bits_per_sample;
int input_size;
uint8_t* input_buf;
int16_t* convert_buf;
int aot;
int afterburner = 1;
int eld_sbr = 0;
int vbr = 0;
HANDLE_AACENCODER handle;
CHANNEL_MODE mode;
AACENC_InfoStruct info = { 0 };
bitrate = 64000;//target bitrate.
aot=23;//type eld 39 ld 23
infile="testfile32kHz.wav";//original file
outfile="encode.aac";//encode aac
lengthfile="length.txt";//record frame length
wav = wav_read_open(infile);
if (!wav) {
fprintf(stderr, "Unable to open wav file %s\n", infile);
return 1;
}
if (!wav_get_header(wav, &format, &channels, &sample_rate, &bits_per_sample, NULL)) {
fprintf(stderr, "Bad wav file %s\n", infile);
return 1;
}
if (format != 1) {
fprintf(stderr, "Unsupported WAV format %d\n", format);
return 1;
}
if (bits_per_sample != 16) {
fprintf(stderr, "Unsupported WAV sample depth %d\n", bits_per_sample);
return 1;
}
switch (channels) {
case 1: mode = MODE_1; break;
case 2: mode = MODE_2; break;
case 3: mode = MODE_1_2; break;
case 4: mode = MODE_1_2_1; break;
case 5: mode = MODE_1_2_2; break;
case 6: mode = MODE_1_2_2_1; break;
default:
fprintf(stderr, "Unsupported WAV channels %d\n", channels);
return 1;
}
if (aacEncOpen(&handle, 0, channels) != AACENC_OK) {
fprintf(stderr, "Unable to open encoder\n");
return 1;
}
if (aacEncoder_SetParam(handle, AACENC_AOT, aot) != AACENC_OK) {
fprintf(stderr, "Unable to set the AOT\n");
return 1;
}
if (aot == 39 && eld_sbr) {
if (aacEncoder_SetParam(handle, AACENC_SBR_MODE, 1) != AACENC_OK) {
fprintf(stderr, "Unable to set SBR mode for ELD\n");
return 1;
}
}
if (aacEncoder_SetParam(handle, AACENC_SAMPLERATE, sample_rate) != AACENC_OK) {
fprintf(stderr, "Unable to set the AOT\n");
return 1;
}
if (aacEncoder_SetParam(handle, AACENC_CHANNELMODE, mode) != AACENC_OK) {
fprintf(stderr, "Unable to set the channel mode\n");
return 1;
}
if (aacEncoder_SetParam(handle, AACENC_CHANNELORDER, 1) != AACENC_OK) {
fprintf(stderr, "Unable to set the wav channel order\n");
return 1;
}
if (vbr) {
if (aacEncoder_SetParam(handle, AACENC_BITRATEMODE, vbr) != AACENC_OK) {
fprintf(stderr, "Unable to set the VBR bitrate mode\n");
return 1;
}
} else {
if (aacEncoder_SetParam(handle, AACENC_BITRATE, bitrate) != AACENC_OK) {
fprintf(stderr, "Unable to set the bitrate\n");
return 1;
}
}
if (aacEncoder_SetParam(handle, AACENC_TRANSMUX,TT_MP4_RAW /*TT_MP4_ADTS TT_MP4_RAW*/) != AACENC_OK) {
fprintf(stderr, "Unable to set the ADTS transmux\n");
return 1;
}//TT_MP4_RAW TT_MP4_LATM_MCP1 TT_MP4_LATM_MCP0 TT_MP4_LOAS is ok.
if (aacEncoder_SetParam(handle, AACENC_AFTERBURNER, afterburner) != AACENC_OK) {
fprintf(stderr, "Unable to set the afterburner mode\n");
return 1;
}
if (aacEncEncode(handle, NULL, NULL, NULL, NULL) != AACENC_OK) {
fprintf(stderr, "Unable to initialize the encoder\n");
return 1;
}
if (aacEncInfo(handle, &info) != AACENC_OK) {
fprintf(stderr, "Unable to get the encoder info\n");
return 1;
}
out = fopen(outfile, "wb");
lengthHandle = fopen(lengthfile, "wb");
if (!out) {
perror(outfile);
return 1;
}
if (!lengthHandle) {
perror(lengthfile);
return 1;
}
input_size = channels*2*info.frameLength;
input_buf = (uint8_t*) malloc(input_size);
convert_buf = (int16_t*) malloc(input_size);
char lengthStr[2];
cout<<"info.frameLength"<
在以上代码中,读入testfile32kHz.wav作为测试序列,编码后的文件为encode.aac,每一帧的长度通过length.txt来记录,这样方便RTP直接按帧进行数据发送.
编码参数为16bit的采样,采样率32kHz,单声道,目标码率是64kps,原始帧长度为512个采样(512*16/8的字节数).
其中wavreader.h和wavreader.c需要引入工程,附代码如下:
wavreader.h
/* ------------------------------------------------------------------
* Copyright (C) 2009 Martin Storsjo
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied.
* See the License for the specific language governing permissions
* and limitations under the License.
* -------------------------------------------------------------------
*/
#ifndef WAVREADER_H
#define WAVREADER_H
#ifdef __cplusplus
extern "C" {
#endif
void* wav_read_open(const char *filename);
void wav_read_close(void* obj);
int wav_get_header(void* obj, int* format, int* channels, int* sample_rate, int* bits_per_sample, unsigned int* data_length);
int wav_read_data(void* obj, unsigned char* data, unsigned int length);
#ifdef __cplusplus
}
#endif
#endif
wavreader.c
/* ------------------------------------------------------------------
* Copyright (C) 2009 Martin Storsjo
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied.
* See the License for the specific language governing permissions
* and limitations under the License.
* -------------------------------------------------------------------
*/
#include "wavreader.h"
#include
#include
#include
#include
#define TAG(a, b, c, d) (((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
struct wav_reader {
FILE *wav;
uint32_t data_length;
int format;
int sample_rate;
int bits_per_sample;
int channels;
int byte_rate;
int block_align;
int streamed;
};
static uint32_t read_tag(struct wav_reader* wr) {
uint32_t tag = 0;
tag = (tag << 8) | fgetc(wr->wav);
tag = (tag << 8) | fgetc(wr->wav);
tag = (tag << 8) | fgetc(wr->wav);
tag = (tag << 8) | fgetc(wr->wav);
return tag;
}
static uint32_t read_int32(struct wav_reader* wr) {
uint32_t value = 0;
value |= fgetc(wr->wav) << 0;
value |= fgetc(wr->wav) << 8;
value |= fgetc(wr->wav) << 16;
value |= fgetc(wr->wav) << 24;
return value;
}
static uint16_t read_int16(struct wav_reader* wr) {
uint16_t value = 0;
value |= fgetc(wr->wav) << 0;
value |= fgetc(wr->wav) << 8;
return value;
}
static void skip(FILE *f, int n) {
int i;
for (i = 0; i < n; i++)
fgetc(f);
}
void* wav_read_open(const char *filename) {
struct wav_reader* wr = (struct wav_reader*) malloc(sizeof(*wr));
long data_pos = 0;
memset(wr, 0, sizeof(*wr));
if (!strcmp(filename, "-"))
wr->wav = stdin;
else
wr->wav = fopen(filename, "rb");
if (wr->wav == NULL) {
free(wr);
return NULL;
}
while (1) {
uint32_t tag, tag2, length;
tag = read_tag(wr);
if (feof(wr->wav))
break;
length = read_int32(wr);
if (!length || length >= 0x7fff0000) {
wr->streamed = 1;
length = ~0;
}
if (tag != TAG('R', 'I', 'F', 'F') || length < 4) {
fseek(wr->wav, length, SEEK_CUR);
continue;
}
tag2 = read_tag(wr);
length -= 4;
if (tag2 != TAG('W', 'A', 'V', 'E')) {
fseek(wr->wav, length, SEEK_CUR);
continue;
}
// RIFF chunk found, iterate through it
while (length >= 8) {
uint32_t subtag, sublength;
subtag = read_tag(wr);
if (feof(wr->wav))
break;
sublength = read_int32(wr);
length -= 8;
if (length < sublength)
break;
if (subtag == TAG('f', 'm', 't', ' ')) {
if (sublength < 16) {
// Insufficient data for 'fmt '
break;
}
wr->format = read_int16(wr);
wr->channels = read_int16(wr);
wr->sample_rate = read_int32(wr);
wr->byte_rate = read_int32(wr);
wr->block_align = read_int16(wr);
wr->bits_per_sample = read_int16(wr);
if (wr->format == 0xfffe) {
if (sublength < 28) {
// Insufficient data for waveformatex
break;
}
skip(wr->wav, 8);
wr->format = read_int32(wr);
skip(wr->wav, sublength - 28);
} else {
skip(wr->wav, sublength - 16);
}
} else if (subtag == TAG('d', 'a', 't', 'a')) {
data_pos = ftell(wr->wav);
wr->data_length = sublength;
if (!wr->data_length || wr->streamed) {
wr->streamed = 1;
return wr;
}
fseek(wr->wav, sublength, SEEK_CUR);
} else {
skip(wr->wav, sublength);
}
length -= sublength;
}
if (length > 0) {
// Bad chunk?
fseek(wr->wav, length, SEEK_CUR);
}
}
fseek(wr->wav, data_pos, SEEK_SET);
return wr;
}
void wav_read_close(void* obj) {
struct wav_reader* wr = (struct wav_reader*) obj;
if (wr->wav != stdin)
fclose(wr->wav);
free(wr);
}
int wav_get_header(void* obj, int* format, int* channels, int* sample_rate, int* bits_per_sample, unsigned int* data_length) {
struct wav_reader* wr = (struct wav_reader*) obj;
if (format)
*format = wr->format;
if (channels)
*channels = wr->channels;
if (sample_rate)
*sample_rate = wr->sample_rate;
if (bits_per_sample)
*bits_per_sample = wr->bits_per_sample;
if (data_length)
*data_length = wr->data_length;
return wr->format && wr->sample_rate;
}
int wav_read_data(void* obj, unsigned char* data, unsigned int length) {
struct wav_reader* wr = (struct wav_reader*) obj;
int n;
if (wr->wav == NULL)
return -1;
if (length > wr->data_length && !wr->streamed)
length = wr->data_length;
n = fread(data, 1, length, wr->wav);
wr->data_length -= length;
return n;
}
wav的文件格式,不多解释,理解为header+pcm即可.即带头信息的原始音频流.