参考资料:
aac格式:https://www.cnblogs.com/caosiyang/archive/2012/07/16/2594029.html http://en.wikipedia.org/wiki/Advanced_Audio_Coding
视音频数据处理入门:AAC音频码流解析 https://blog.csdn.net/leixiaohua1020/article/details/50535042
功能:解析出来aacADTS头中各个字段
缺点:打印出来的格式有点乱。
1 #include <stdio.h> 2 3 int sampling_frequency[] = {96000, 88200, 64000, 48000, 44100,32000, 4 24000, 22050,16000,12000,11025,8000, 7350,0,0,-1}; 5 6 typedef struct { 7 unsigned syncword:12; // FFF 8 unsigned ID:1; //MPEG标识符,0标识MPEG-4,1标识MPEG-2 9 unsigned layer:2; // always: '00' 10 unsigned protection_absent:1; //表示是否误码校验。Warning, set to 1 if there is no CRC and 0 if there is CRC 11 unsigned profile:2; //表示使用哪个级别的AAC,如01 Low Complexity(LC)--- AAC LC。有些芯片只支持AAC LC 。 12 unsigned sampling_frequency_index:4;//表示使用的采样率下标,通过这个下标在 Sampling Frequencies[ ]数组中查找得知采样率的值。 13 unsigned private_bit:1; 14 unsigned channel_configuration:3; /* 15 0: Defined in AOT Specifc Config 16 1: 1 channel: front-center 17 2: 2 channels: front-left, front-right 18 3: 3 channels: front-center, front-left, front-right 19 4: 4 channels: front-center, front-left, front-right, back-center 20 5: 5 channels: front-center, front-left, front-right, back-left, back-right 21 6: 6 channels: front-center, front-left, front-right, back-left, back-right, LFE-channel 22 7: 8 channels: front-center, front-left, front-right, side-left, side-right, back-left, back-right, LFE-channel 23 8-15: Reserved 24 */ 25 unsigned original_copy:1; 26 unsigned home:1; 27 } adts_fixed_header; // 28bits 28 29 typedef struct { 30 unsigned copyright_identification_bit:1; 31 unsigned copyright_identification_start:1; 32 /* 33 aac_frame_length = (protection_absent == 1 ? 7 : 9) + size(AACFrame) 34 protection_absent=0时, header length=9bytes 35 protection_absent=1时, header length=7bytes 36 */ 37 unsigned aac_frame_length:13; 38 unsigned adts_buffer_fullness:11; //0x7FF 说明是码率可变的码流。 39 /* 40 表示ADTS帧中有 number_of_raw_data_blocks_in_frame + 1个AAC原始帧。 41 所以说number_of_raw_data_blocks_in_frame == 0 表示说ADTS帧中有一个AAC数据块。 42 */ 43 unsigned number_of_raw_data_blocks_in_frame:2; 44 } adts_variable_header; 45 46 adts_fixed_header fix_header; 47 adts_variable_header var_header; 48 49 unsigned char * 50 getADTS (unsigned char *p, unsigned char **pnext, unsigned char *pend){ 51 unsigned char *pin = p, *res; 52 *pnext = NULL; 53 54 while(p < pend) { 55 if (p[0] == 0xff && (p[1] & 0xf0) == 0xf0) { 56 res = p; 57 fix_header.ID = (p[1] & 0x08)>>3; 58 fix_header.layer = (p[1] & 0x06) >> 1; 59 fix_header.protection_absent = (p[1] & 0x01); 60 fix_header.profile = (p[2] & 0xc0) >> 6; 61 fix_header.sampling_frequency_index = (p[2] & 0x3c) >> 2; 62 if (fix_header.sampling_frequency_index != 4) { 63 printf("error frequency "); 64 } 65 fix_header.private_bit = (p[2] & 0x02) >> 1; 66 fix_header.channel_configuration = (((p[2] & 0x01)) << 2 ) | ((p[3] & 0xc0) >> 6); 67 fix_header.original_copy = (p[3] & 0x20) >> 5; 68 fix_header.home = (p[3] & 0x10) >> 4; 69 70 var_header.copyright_identification_bit = (p[3] & 0x08)>>3; 71 var_header.copyright_identification_start = (p[3] & 0x04)>>2; 72 var_header.aac_frame_length = ((p[3] & 0x03) << 11) | (p[4] << 3) | (p[5] & 0xe0 >> 5); 73 var_header.adts_buffer_fullness = (p[5] & 0x1f << 6) | (p[6] & 0xfc >> 2); 74 var_header.number_of_raw_data_blocks_in_frame = p[6] & 0x03; 75 p = p+7; 76 while(p < pend) { 77 if (*p == 0xff && (p[1] & 0xf0 == 0xf0) ){ 78 *pnext = p; 79 return res; 80 } 81 p++; 82 } 83 if (p == pend) { 84 return res; 85 } 86 } 87 p++; 88 } 89 return NULL; 90 } 91 92 int aac_analyse(char *url) { 93 FILE *fp; 94 static int num =0; 95 int file_size=0, adts_len=0, read_len=0; 96 unsigned char *buf, *padts, *pnext, *p, *pend; 97 98 fp = fopen(url, "rb+"); 99 if (fp == NULL) { 100 return -1; 101 } 102 fseek(fp, 0, SEEK_END); 103 file_size = ftell(fp); 104 fseek(fp, 0, SEEK_SET); 105 printf("file_size:%d ", file_size); 106 buf = malloc(file_size); 107 if (buf == NULL) { 108 return -1; 109 } 110 p = buf; 111 pend = buf + file_size; 112 113 fread(buf, 1, file_size, fp); 114 printf("-----+- ADTS Frame Table -+------+ "); 115 printf("-----+---------+----------+------+ "); 116 while(1) { 117 padts = getADTS(p, &pnext, pend); 118 if (padts == NULL) { 119 break; 120 } 121 122 printf("--num+id+layer+protection_absent+profile+sampling_frequency_index+private_bit+channel_configuration+original_copy+home "); 123 printf("%d, %d, %d, %d, %d, %d, %d, %d, %d, %d ", 124 ++num, 125 fix_header.ID, 126 fix_header.layer, 127 fix_header.protection_absent, 128 fix_header.profile , 129 fix_header.sampling_frequency_index, 130 fix_header.private_bit, 131 fix_header.channel_configuration, 132 fix_header.original_copy , 133 fix_header.home); 134 printf("-- copyright_identification_bit+copyright_identification_start+aac_frame_length+adts_buffer_fullness+number_of_raw_data_blocks_in_frame "); 135 136 printf(" %d, %d, %d, %d, %d ", 137 var_header.copyright_identification_bit, 138 var_header.copyright_identification_start, 139 var_header.aac_frame_length , 140 var_header.adts_buffer_fullness, 141 var_header.number_of_raw_data_blocks_in_frame); 142 if (pnext == NULL) { 143 break; 144 } 145 p = pnext; 146 } 147 148 fclose(fp); 149 free(buf); 150 return 0; 151 } 152 153 int main(){ 154 aac_analyse("nocturne.aac"); 155 return 0; 156 }