這一節,我們學習常用的音頻的格式 AAC,重點是掌握 AAC的傳輸格式 ADTS 頭部的信息,目的是 : 當音頻數據有問題的時候,如果是AAC的編碼,在分析 頭部信息的時候能夠根據頭部信息 判斷問題是否出現在 頭部。
AAC ADTS格式分析
AAC?頻格式:Advanced Audio Coding(?級?頻解碼),是?種由MPEG-4標準定義的有損?頻壓縮格式,由Fraunhofer發展,Dolby, Sony和AT&T是主要的貢獻者。
AAC 的封裝格式有兩種 ADIF 和 ADTS
ADIF:已經基本棄用-ADIF只有?個統?的頭,所以必須得到所有的數據后解碼。
Audio Data Interchange Format ?頻數據交換格式。這種格式的特征是可以確定的找到這個?頻數據的開始,不需進?在?頻數據流中間開始的解碼,即它的解碼必須在明確定義的開始處進?。故這種格式常?在磁盤?件中。
ADTS ADTS可以在任意幀解碼,也就是說它每?幀都有頭信息,這個是重點
Audio Data Transport Stream。是AAC?頻的傳輸流格式。AAC?頻格式在MPEG-2(ISO-13318-7 2003)中有定義。AAC后來?被采?到MPEG-4標準中。這種格式的特征是它是?個有同步字的?特流,解碼可以在這個流中任何位置開始。它的特征類似于mp3數據流格式。
可能遇見的問題1:
有的時候當你編碼AAC裸流的時候,會遇到寫出來的AAC?件并不能在PC和?機上播放,很?的可能就是AAC?件的每?幀?缺少了ADTS頭信息?件的包裝拼接。
只需要加?頭?件ADTS即可。?個AAC原始數據塊?度是可變的,對原始幀加:上ADTS頭進?ADTS的封裝,就形成了ADTS幀。
ADTS 格式下 AAC?頻?件格式
每?幀由ADTS Header和AAC Audio Data(在圖中,是 AAC ES)組成。結構體如下:
也就是說,,一個AAC 幀,包含了一個ADTS header 和 一堆具體的數據。另外AAC的一幀一般包含了1024個采樣點。
頭文件 ADTS Header 的組成
每?幀的ADTS的頭?件都包含了?頻的采樣率,聲道,幀?度等信息,這樣解碼器才能解析讀取。
?般情況下ADTS的頭信息都是7個字節,分為2部分:
adts_fixed_header();
adts_variable_header();
其?為固定頭信息,緊接著是可變頭信息。固定頭信息中的數據每?幀都相同,?可變頭信息則在幀與幀之間可變。
頭文件固定部分:adts_fixed_header()
syncword :同步頭 總是0xFFF, all bits must be 1,代表著?個ADTS幀的開始 12bits
ID: MPEG標識符,0標識MPEG-4,1標識MPEG-2 1bits
Layer: always: '00' 2 bits
protection_absent:表示是否誤碼校驗。1代表 header 有 7個字節,0代表 header有9個字節,一般情況下都是7個字節。Warning, set to 1 if there is no CRC and 0 if there is CRC 1bits
profile:表示使?哪個級別的AAC,如01 Low Complexity(LC)--- AAC LC。有些芯?只?持AAC LC 。 2bits。。 通過前面的ID,我們可以設置是?MPEG-4, 還是?MPEG-2
在MPEG-2 中,有明確的指出?profile這個值是多少。且只有三種 參見下表
在MPEG-4中,profile的計算要通過? MPEG-4 Audio Object Type - 1
profile = MPEG-4 Audio Object Type - 1
如下的MPEG-4中關于 aac audio Object Type的說明
對應的profile的值
sampling_frequency_index:表示使?的采樣率下標,通過這個下標在 Sampling Frequencies[ ]數組中查找得知采樣率的值。4bits
channel_configuration: 表示聲道數,?如2表示?體聲雙聲道 3bits,
MPEG-4 中規定的值如下:
0: Defined in AOT Specifc Config
1: 1 channel: front-center
2: 2 channels: front-left, front-right
3: 3 channels: front-center, front-left, front-right
4: 4 channels: front-center, front-left, front-right, back-center
5: 5 channels: front-center, front-left, front-right, back-left, back-right
6: 6 channels: front-center, front-left, front-right, back-left, back-right, LFE-channel
7: 8 channels: front-center, front-left, front-right, side-left, side-right,back-left, back-right, LFE-channel
8-15: Reserved
還有3個沒有介紹:都占1bite,
private_bits:
original:
home:
頭文件adts_variable_header()
copyright_identification_bits: 未知, 占1bits
copyright_identification_start: 未知,占1bits
aac_frame_length : ?個ADTS幀的?度 包括ADTS頭和AAC原始流. 單位是bytes
aac_frame_length = (protection_absent == 1 ? 7 : 9) + size(AACFrame) 13 bits
protection_absent=0時, header length=9bytes
protection_absent=1時, header length=7bytes
adts_buffer_fullness:0x7FF 說明是碼率可變的碼流。一般都是寫的0x7FF這個值 11bits
例子:
下?是ADTS的AAC?件部分:?字節開始算
第?幀的幀頭7個字節為:0xFF 0xF1 0x4C 0x40 0x20 0xFF 0xFC
我們將這7個字節拿出來,轉化成2進制
0xFF? ? ? ? ?0xF1? ? ? ? ? 0x4C? ? ? ? ? 0x40? ? ? ? ? ? ?0x20? ? ? ? ? ? 0xFF? ? ? ? ? 0xFC
11111111? ?11110001? ?01001100? ?0100 0000? ?0010 0000? ?1111 1111? ?1111 1100
分析各個關鍵數值:
0xFF 0xF1 表示如下的部分
111111111111 syncword :同步頭 總是0xFFF, all bits must be 1,代表著?個ADTS幀的開始 12bits
0 ID: MPEG標識符,0標識MPEG-4,1標識MPEG-2 1bits
00 Layer: always: '00' 2 bits
1 protection_absent:表示是否誤碼校驗。1代表 header 有 7個字節,0代表 header有9個字節,一般情況下都是7個字節。Warning, set to 1 if there is no CRC and 0 if there is CRC 1bits
0x4C 全部, 0x40中的4 表示部分如下
01 profile:表示使?哪個級別的AAC,如01 Low Complexity(LC)--- AAC LC。有些芯片只支持AAC LC 。 2bits
0011 ,sampling_frequency_index : 通過這個下標在 Sampling Frequencies[ ]數組中找采樣率的值 4bits
0 private_bits: 1 bits
001 channel_configuration: 表示聲道數,比如2表示立體聲雙聲道 3bits
0 original: 1bits
0 home: 1bits
0x40中的4 ,,, 0x20 0xFF 0xFC 全部表示如下
0 copyright_identification_bits: 未知, 占1bits
0 copyright_identification_start: 未知,占1bits
0000100000111(幀?度) aac_frame_length 占 13 bits
11111111111 adts_buffer_fullness:0x7FF 說明是碼率可變的碼流。 11bits
00 number_of_raw_data_blocks_in_frame:
表示ADTS幀中有number_of_raw_data_blocks_in_frame + 1個AAC原始幀。 占2bits
一般一個
計算幀?度:將?進制 0000100000111 轉換成?進制為263。觀察第?幀的?度確實為263個字節。紅色部分的為幀頭部的固定部分?0xFF 0xF1 0x4C 0x40 0x20 0xFF 0xFC , 該幀長度所屬位置為藍色部分,其中 第一個0的后兩個0, 0000
得到幀長度的計算?法:(幀?度為13位,使?unsigned int來存儲幀?數值)
unsigned int getFrameLength(unsigned char* str)
{if ( !str ){return 0;}unsigned int len = 0;int f_bit = str[3];int m_bit = str[4];int b_bit = str[5];len += (b_bit>>5);len += (m_bit<<3);len += ((f_bit&3)<<11);return len;
}
核心編碼:注意的在生成 ADTS Header 的代碼
// 讀取媒體文件,并把aac數據幀寫入到本地文件,注意,從mp4文件中讀取到的aac就只有 aac data 的部分,沒有頭的部分,頭的部分我們需要自己添加,使用的方法為自定義的adts_header方法
// av_read_frame方法的一些說明
// 對于音頻,如果每個幀具有已知的固定大小(例如PCM或ADPCM數據),則它包含整數個幀。// 如果音頻幀具有可變大小(例如MPEG音頻),則它包含一個幀。//當前走到這里,讀取的一定是AAC數據,那么av_read_frame讀取到pkt中的一定是一幀的大小,因此adts_header方法中,傳遞的第二個參數就是一幀的大小。int ret1 =0;while((ret1 = av_read_frame(ifmt_ctx, &pkt)) >=0 ){if(pkt.stream_index == audio_index){char adts_header_buf[7] = {0};//這里我們自己寫的時候,頭部占7bytes,意味著 校驗位的值是1,表示不用校驗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->channels);fwrite(adts_header_buf, 1, 7, aac_fd); // 寫adts header , ts流不適用,ts流分離出來的packet帶了adts headerlen = 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);}
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; //?11111100? //buffer fullness:0x7ff 低6bits// number_of_raw_data_blocks_in_frame:// 表示ADTS幀中有number_of_raw_data_blocks_in_frame + 1個AAC原始幀。return 0;
}
全部代碼:
#include <stdio.h>
#include <libavutil/log.h>
#include <libavformat/avio.h>
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>#define ADTS_HEADER_LEN 7;const int sampling_frequencies[] = {96000, // 0x088200, // 0x164000, // 0x248000, // 0x344100, // 0x432000, // 0x524000, // 0x622050, // 0x716000, // 0x812000, // 0x911025, // 0xa8000 // 0xb// 0xc d e f是保留的
};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; //?11111100? //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];char *in_filename = NULL;char *aac_filename = NULL;FILE *aac_fd = NULL;int audio_index = -1;int len = 0;AVFormatContext *ifmt_ctx = NULL;AVPacket pkt;// 設置打印級別av_log_set_level(AV_LOG_DEBUG);if(argc < 3){av_log(NULL, AV_LOG_DEBUG, "the count of parameters should be more than three!\n");return -1;}in_filename = argv[1]; // 輸入文件aac_filename = argv[2]; // 輸出文件if(in_filename == NULL || aac_filename == NULL){av_log(NULL, AV_LOG_DEBUG, "src or dts file is null, plz check them!\n");return -1;}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_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;}// 獲取解碼器信息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);// 初始化packetav_init_packet(&pkt);// 查找audio對應的steam indexaudio_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 headerlen = 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);}return 0;
}