AAC基本结构

详细

由图可见,ADTS frame (Audio Data Transport Stream) 之间由syncword(同步字进行分离),即0xFFF。

名称 备注
syncword 总是0xFFF, 代表一个ADTS帧的开始
ID MPEG Version: 0 for MPEG-4, 1 for MPEG-2
layer always: ‘00’
protection_absent Warning, set to 1 if there is no CRC and 0 if there is CRC
profile
sampling_frequency_index
channel_configuration
aac_frame_length 一个ADTS帧的长度包括ADTS头和AAC原始流,aac_frame_length = (protection_absent == 1 ? 7 : 9) + size(AACFrame)
adts_buffer_fullness 0x7FF 说明是码率可变的码流
number_of_raw_data_blocks_in_frame 有number_of_raw_data_blocks_in_frame+1个AAC原始帧(一个AAC原始帧包含一段时间内1024个采样及相关数据)

源码解析

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
int getADTSframe(unsigned char* buffer, int buf_size, unsigned char* data ,int* data_size){
int size = 0;

if(!buffer || !data || !data_size ){
return -1;
}

while(1){
if(buf_size < 7 ){
return -1;
}
//找出同步码 然后找到aac_frame_length的大小 保存在size
if((buffer[0] == 0xff) && ((buffer[1] & 0xf0) == 0xf0) ){//同步码 实际为0xF0FF?
size |= ((buffer[3] & 0x03) <<11); //high 2 bit
size |= buffer[4]<<3; //middle 8 bit
size |= ((buffer[5] & 0xe0)>>5); //low 3bit
break;
}
--buf_size;
++buffer;
}

if(buf_size < size){//此段不完整 buf_size为剩下的 size为应该的大小
return 1;
}

memcpy(data, buffer, size);
*data_size = size;

return 0;
}

int aacParser(char *url){
int data_size = 0;
int size = 0;
int cnt=0;
int offset=0;

unsigned char *aacframe=(unsigned char *)malloc(1024*5);
unsigned char *aacbuffer=(unsigned char *)malloc(1024*1024);

FILE *fp=fopen(url,"rb");

while(!feof(fp)){
data_size = fread(aacbuffer+offset, 1, 1024*1024-offset, ifile);
unsigned char* input_data = aacbuffer;

while(1)
{
int ret=getADTSframe(input_data, data_size, aacframe, &size);
if(ret==-1){//段中没有完整的ADTS了
break;
}else if(ret==1){
memcpy(aacbuffer,input_data,data_size);
offset=data_size;
break;
}
char profile_str[10]={0};
char frequence_str[10]={0};

unsigned char profile=aacframe[2]&0xC0;
profile=profile>>6;
switch(profile){
case 0: sprintf(profile_str,"Main");break;
case 1: sprintf(profile_str,"LC");break;
case 2: sprintf(profile_str,"SSR");break;
default:sprintf(profile_str,"unknown");break;
}

unsigned char sampling_frequency_index=aacframe[2]&0x3C;
sampling_frequency_index=sampling_frequency_index>>2;
switch(sampling_frequency_index){
case 0: sprintf(frequence_str,"96000Hz");break;
case 1: sprintf(frequence_str,"88200Hz");break;
case 2: sprintf(frequence_str,"64000Hz");break;
case 3: sprintf(frequence_str,"48000Hz");break;
case 4: sprintf(frequence_str,"44100Hz");break;
case 5: sprintf(frequence_str,"32000Hz");break;
case 6: sprintf(frequence_str,"24000Hz");break;
case 7: sprintf(frequence_str,"22050Hz");break;
case 8: sprintf(frequence_str,"16000Hz");break;
case 9: sprintf(frequence_str,"12000Hz");break;
case 10: sprintf(frequence_str,"11025Hz");break;
case 11: sprintf(frequence_str,"8000Hz");break;
default:sprintf(frequence_str,"unknown");break;
}


fprintf(myout,"%5d| %8s| %8s| %5d|\n",cnt,profile_str ,frequence_str,size);
data_size -= size;
input_data += size;
cnt++;
}
}
fclose(ifile);
free(aacbuffer);
free(aacframe);

return 0;
}