FFMPEG AAC
一、音频流AAC格式
1.1 总体介绍
ADTS可以在任意帧解码,每一帧都有头信息。下图绿色的是头,黄色的数据
1.2 固定头信息
头信息中包括橙色的固定头和紫色的可变头
(1) syncword:同步头 总是 0xFFF,all bits must be 1,代表着一个 ADTS 帧的开始
(2) ID:MPEG 标识符,0 表示使用 MPEG-4 标准,1 表示使用 MPEG-2 标准
(3) Layer:保留字段一直是0x00
(4) protection_absent:表示是否误码校验。如果没有CRC校验就设置为1,如果有就设置为0。有CRC校验,头长度就是9字节
(5) profile:表示使用哪个级别的 AAC,如 01 Low Complexity (LC)--- AAC LC。有些芯片只支持 AAC LC。
在 MPEG-2 AAC 中定义了 3 种:
- Main profile(主简档):是一种基础的 AAC 编码配置。
- Low Complexity profile (LC)(低复杂度简档):如其名,编码复杂度较低,在很多场景中被广泛使用,比如一些对编码效率要求较高的设备或应用。
- Scalable Sampling Rate profile (SSR)(可伸缩采样率简档):支持采样率的可伸缩性,能适应不同的音频采样率需求。
- (reserved):表示该索引目前是保留状态,暂未定义对应的 profile。
profile 的值等于 Audio Object Type 的值减 1
Audio Object Type定义在ffmpeg内了
#define FF_PROFILE_AAC_MAIN 0
#define FF_PROFILE_AAC_LOW 1
#define FF_PROFILE_AAC_SSR 2
#define FF_PROFILE_AAC_LTP 3
#define FF_PROFILE_AAC_HE 4
#define FF_PROFILE_AAC_HE_V2 28
#define FF_PROFILE_AAC_LD 22
#define FF_PROFILE_AAC_ELD 38
#define FF_PROFILE_MPEG2_AAC_LOW 128
#define FF_PROFILE_MPEG2_AAC_HE 131
(6) sampling_frequency_index:通过这个索引,去找到当前音频使用的采样率
(7) channel_configuration:声道数
1.3 可变头信息
(1) aac_frame_length = (protection_absent == 1 ? 7 : 9) + size(AACFrame)
(2) adts_buffer_fullness
假设你正在开发一个基于 AAC 音频编码格式的在线音乐播放应用,在这个应用中,需要实时处理从服务器传输过来的 ADTS 格式音频数据 。
正常恒定码率码流情况
当服务器发送的音频数据是恒定码率(CBR)的 ADTS 码流时,比如一首标准品质的 MP3 转码成 AAC 格式,其adts_buffer_fullness
的值不是0x7FF
,而是一个反映当前音频缓冲区填充程度的具体数值。假设这个数值是0x100
,表示当前音频缓冲区已填充了一定比例的数据,播放器可以按照预先设定好的固定速率(例如每秒播放 128kbps 的数据量)来读取和播放音频数据。
可变码率码流情况
如果是一段来自现场直播的音频流,音频内容复杂程度在不断变化,可能会采用可变码率(VBR)编码。当adts_buffer_fullness
的值为0x7FF
时, 播放器就知道这是一个可变码率的码流。
例如,在直播过程中,当主播只是在正常说话时,音频内容相对简单,编码器会降低码率以节省带宽,此时虽然缓冲区中数据量相对较少,但播放器依然能以较低的码率流畅播放;而当主播播放一段节奏强烈、乐器丰富的背景音乐时,音频内容复杂,编码器会提高码率来保证音质,此时音频缓冲区会快速填充数据 。
播放器检测到adts_buffer_fullness
为0x7FF
,就会采用自适应的播放策略。它会实时监测缓冲区的数据量,根据缓冲区的填充程度动态调整读取音频数据的速率。如果缓冲区快满了,就加快播放速度;如果缓冲区数据较少,就适当降低播放速度,以确保音频播放的流畅性,同时充分利用可变码率带来的灵活性和高效性。
二、从视频中提取音频保存成AAC
#include <iostream>extern "C"{#include "libavcodec/avcodec.h"#include "libavformat/avformat.h"
}using namespace std;const int sampling_frequencies[] = {96000, // 0x088200, // 0x164000, // 0x248000, // 0x344100, // 0x432000, // 0x524000, // 0x622050, // 0x716000, // 0x812000, // 0x911025, // 0xa8000 // 0xb// 0xc d e f是保留的
};/*
profile(AAC 配置文件 / 规格)含义:表示 AAC 音频的编码规格(Profile),决定了 AAC 的编码复杂度、音质和兼容性。FF_PROFILE_AAC_LOW(0):基础规格,最常用,适用于大多数场景(如音乐、语音)。FF_PROFILE_AAC_MAIN(1):主规格,支持更多复杂编码技术。FF_PROFILE_AAC_HIGH(2):高级规格,音质更好但编码复杂度更高。
samplerate(采样率)含义:音频信号的采样频率,即每秒对声音信号的采样次数(单位:Hz)。采样率从AVCodecParameters->sample_rate获取,需匹配ADTS头部支持的采样率列表ampling_frequencies
channels(声道数)常见取值:1:单声道(Mono) 2:立体声(Stereo) 4:四声道(如环绕声)
*/
int adts_header(char * const p_adts_header, const int data_length, const int profile, const int samplerate, const int channels) {int sampling_frequency_index = 3; // 默认使用48000hzint adtsLen = data_length + 7;int frequencies_size = sizeof(sampling_frequencies) / sizeof(sampling_frequencies[0]);int i = 0;for(i = 0; i < frequencies_size; i++){if(sampling_frequencies[i] == samplerate){sampling_frequency_index = i;break;}}if(i >= frequencies_size){printf("unsupport samplerate:%d\n", samplerate);return -1;}p_adts_header[0] = 0xff; //syncword:0xfff 高8bitsp_adts_header[1] = 0xf0; //syncword:0xfff 低4bitsp_adts_header[1] |= (0 << 3); //MPEG Version:0 for MPEG-4,1 for MPEG-2 1bitp_adts_header[1] |= (0 << 1); //Layer:0 2bitsp_adts_header[1] |= 1; //protection absent:1 1bitp_adts_header[2] = (profile)<<6; //profile:profile 2bitsp_adts_header[2] |= (sampling_frequency_index & 0x0f)<<2; //sampling frequency index:sampling_frequency_index 4bitsp_adts_header[2] |= (0 << 1); //private bit:0 1bitp_adts_header[2] |= (channels & 0x04)>>2; //channel configuration:channels 高1bitp_adts_header[3] = (channels & 0x03)<<6; //channel configuration:channels 低2bitsp_adts_header[3] |= (0 << 5); //original:0 1bitp_adts_header[3] |= (0 << 4); //home:0 1bitp_adts_header[3] |= (0 << 3); //copyright id bit:0 1bitp_adts_header[3] |= (0 << 2); //copyright id start:0 1bitp_adts_header[3] |= ((adtsLen & 0x1800) >> 11); //frame length:value 高2bitsp_adts_header[4] = (uint8_t)((adtsLen & 0x7f8) >> 3); //frame length:value 中间8bitsp_adts_header[5] = (uint8_t)((adtsLen & 0x7) << 5); //frame length:value 低3bitsp_adts_header[5] |= 0x1f; //buffer fullness:0x7ff 高5bitsp_adts_header[6] = 0xfc; //buffer fullness:0x7ff 低6bits// number_of_raw_data_blocks_in_frame:// 表示ADTS帧中有number_of_raw_data_blocks_in_frame + 1个AAC原始帧。return 0;
}int main(int argc, char **argv)
{int ret = -1;char errors[1024];AVFormatContext * ifmt_ctx = NULL;// 打开输入文件const char * in_filename = "believe.mp4";if((ret = avformat_open_input(&ifmt_ctx, in_filename, NULL, NULL)) < 0) {av_strerror(ret, errors, 1024);av_log(NULL, AV_LOG_DEBUG, "Could not open source file: %s, %d(%s)\n", in_filename, ret, errors);return -1;}// 打开输出文件const char * aac_filename = "output.aac";FILE * aac_fd = fopen(aac_filename, "wb");if (!aac_fd){av_log(NULL, AV_LOG_DEBUG, "Could not open destination file %s\n", aac_filename);return -1;}// 获取解码器信息if((ret = avformat_find_stream_info(ifmt_ctx, NULL)) < 0) {av_strerror(ret, errors, 1024);av_log(NULL, AV_LOG_DEBUG, "failed to find stream information: %s, %d(%s)\n", in_filename, ret, errors);return -1;}// dump媒体信息av_dump_format(ifmt_ctx, 0, in_filename, 0);// 初始化packetAVPacket pkt;av_init_packet(&pkt);// 查找audio对应的steam indexint audio_index = -1;audio_index = av_find_best_stream(ifmt_ctx, AVMEDIA_TYPE_AUDIO, -1, -1, NULL, 0);if(audio_index < 0) {av_log(NULL, AV_LOG_DEBUG, "Could not find %s stream in input file %s\n", av_get_media_type_string(AVMEDIA_TYPE_AUDIO), in_filename);return AVERROR(EINVAL);}// 打印AAC级别printf("audio profile:%d, FF_PROFILE_AAC_LOW:%d\n", ifmt_ctx->streams[audio_index]->codecpar->profile, FF_PROFILE_AAC_LOW);if (ifmt_ctx->streams[audio_index]->codecpar->codec_id != AV_CODEC_ID_AAC) {printf("the media file no contain AAC stream, it's codec_id is %d\n", ifmt_ctx->streams[audio_index]->codecpar->codec_id);goto failed;}// 读取媒体文件,并把aac数据帧写入到本地文件while(av_read_frame(ifmt_ctx, &pkt) >=0 ){if(pkt.stream_index == audio_index){char adts_header_buf[7] = {0};adts_header(adts_header_buf, pkt.size,ifmt_ctx->streams[audio_index]->codecpar->profile,ifmt_ctx->streams[audio_index]->codecpar->sample_rate,ifmt_ctx->streams[audio_index]->codecpar->ch_layout.nb_channels);fwrite(adts_header_buf, 1, 7, aac_fd); // 写adts header , ts流不适用,ts流分离出来的packet带了adts headerint len = fwrite( pkt.data, 1, pkt.size, aac_fd); // 写adts dataif(len != pkt.size){av_log(NULL, AV_LOG_DEBUG, "warning, length of writed data isn't equal pkt.size(%d, %d)\n", len, pkt.size);}}av_packet_unref(&pkt);}failed:// 关闭输入文件if(ifmt_ctx){avformat_close_input(&ifmt_ctx);}if (aac_fd){fclose(aac_fd);}system("pause");return 0;
}