1. 程式人生 > >ADTS格式解析

ADTS格式解析

ADTS介紹:
ADTS全稱是(Audio Data Transport Stream),是AAC的一種傳輸格式。
ADTS AAC
ADTS_header AAC ES ADTS_header AAC ES …ADTS_header AAC ES

2.ADTS內容及結構
ADTS 頭中相對有用的資訊 取樣率、聲道數、幀長度。想想也是,我要是解碼器的話,你給我一堆得AAC音訊ES流我也解不出來。每一個帶ADTS頭資訊的AAC流會清晰的告送解碼器他需要的這些資訊。

一般情況下ADTS的頭資訊都是7個位元組,分為2部分:

adts_fixed_header();

adts_variable_header();

“`
Struct Adts_Fixed_Head
{
int SyncWord:12;
int ID:1;
int Layer:2;
int Protection_absent:1;
int Profile:2;
int Sampling_Frequency_Index:4;
int Private_bit:1;
int Channel_configuration:3;
int Original_copy:1;
int Home:1;
};
syncword :同步頭 總是0xFFF, all bits must be 1,代表著一個ADTS幀的開始

ID:MPEG Version: 0 for MPEG-4, 1 for MPEG-2

Layer:always: ‘00’

profile:表示使用哪個級別的AAC,有些晶片只支援AAC LC 。在MPEG-2 AAC中定義了3種:
index | profile
0 Main profile
1 Low Complexity profie(LC)
2 Scalable Sampling Rate profile(SSR)
3 (reserved)
sampling_frequency_index:表示使用的取樣率下標,通過這個下標在 Sampling Frequencies[ ]陣列中查詢得知取樣率的值。
There are 13 supported frequencies:
0: 96000 Hz
1: 88200 Hz
2: 64000 Hz
3: 48000 Hz
4: 44100 Hz
5: 32000 Hz
6: 24000 Hz
7: 22050 Hz
8: 16000 Hz
9: 12000 Hz
10: 11025 Hz
11: 8000 Hz
12: 7350 Hz
13: Reserved
14: Reserved
15: frequency is written explictly
channel_configuration: 表示聲道數
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

Adts_Variable_Header
{
int CopyRight_Identification_Bit:1;
int CopyRight_Identification_Start:1;
int Aac_Frame_Length:13;
int Adts_Buffer_Fullness:11;
int Number_Of_Raw_Data_Blocks_In_Frame:2;
};
frame_length : 一個ADTS幀的長度包括ADTS頭和AAC原始流.

adts_buffer_fullness:0x7FF 說明是位元速率可變的碼流

3.AAC打包成ADTS格式
通過對ADTS格式的瞭解,把AAC打包成ADTS,我們只需得到封裝格式裡面關於音訊取樣率、聲道數、元資料長度、aac格式型別等資訊。然後在每個AAC原始流前面加上個ADTS頭就OK了。
ffmpeg中新增ADTS頭的程式碼如下:

int ff_adts_write_frame_header(ADTSContext *ctx,
uint8_t *buf, int size, int pce_size)
{
PutBitContext pb;

init_put_bits(&pb, buf, ADTS_HEADER_SIZE);  

/* adts_fixed_header */  
put_bits(&pb, 12, 0xfff);   /* syncword */  
put_bits(&pb, 1, 0);        /* ID */  
put_bits(&pb, 2, 0);        /* layer */  
put_bits(&pb, 1, 1);        /* protection_absent */  
put_bits(&pb, 2, ctx->objecttype); /* profile_objecttype */  
put_bits(&pb, 4, ctx->sample_rate_index);  
put_bits(&pb, 1, 0);        /* private_bit */  
put_bits(&pb, 3, ctx->channel_conf); /* channel_configuration */  
put_bits(&pb, 1, 0);        /* original_copy */  
put_bits(&pb, 1, 0);        /* home */  

/* adts_variable_header */  
put_bits(&pb, 1, 0);        /* copyright_identification_bit */  
put_bits(&pb, 1, 0);        /* copyright_identification_start */  
put_bits(&pb, 13, ADTS_HEADER_SIZE + size + pce_size); /* aac_frame_length */  
put_bits(&pb, 11, 0x7ff);   /* adts_buffer_fullness */  
put_bits(&pb, 2, 0);        /* number_of_raw_data_blocks_in_frame */  

flush_put_bits(&pb);  

return 0;  

}