使用fdkaac编码

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;
	}


2,对每一帧音频数据(pcm格式)调用编码函数

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);


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值