aac 解析代码

Posted micoblog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了aac 解析代码相关的知识,希望对你有一定的参考价值。

参考资料:

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

 

  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             fix_header.private_bit = (*(p+2) & 0x02) >> 1;
 63             fix_header.channel_configuration = (((*(p+2) & 0x01)) << 2 ) | ((*(p+3) & 0xc0) >> 6);
 64             fix_header.original_copy = (*(p+3) & 0x20) >> 5;
 65             fix_header.home = (*(p+3) & 0x10) >> 4;
 66 
 67             var_header.copyright_identification_bit = (*(p+3) & 0x08)>>3;
 68             var_header.copyright_identification_start = (*(p+3) & 0x04)>>2;
 69             var_header.aac_frame_length = ((*(p+3) & 0x03) << 11) | (*(p+4) << 3) | (*(p+5) & 0xe0 >> 5);
 70             var_header.adts_buffer_fullness = (*(p+5) & 0x1f << 6) | (*(p+6) & 0xfc >> 2);
 71             var_header.number_of_raw_data_blocks_in_frame = *(p+6) & 0x03;
 72             p = p+7;
 73             while(p < pend) {
 74                 if (*p == 0xff && (*(p+1) & 0xf0 == 0xf0) ){
 75                     *pnext = p;
 76                     return res;
 77                 }
 78                 p++;    
 79             }
 80             if (p == pend) {
 81                 return res;
 82             }
 83         }
 84         p++;
 85     }
 86     return NULL;
 87 }
 88 
 89 int aac_analyse(char *url) {
 90     int file_size=0, adts_len=0, read_len=0;
 91     unsigned char *buf, *padts, *pnext, *p, *pend;
 92     static int num =0;
 93     FILE *fp =fopen(url, "rb+");
 94     if (fp == NULL) {
 95         return -1;
 96     }
 97     fseek(fp, 0, SEEK_END);
 98     file_size = ftell(fp);
 99     fseek(fp, 0, SEEK_SET);
100     printf("file_size:%d
", file_size);
101     buf = malloc(file_size);
102     if (buf == NULL) {
103         return -1;
104     }
105     p = buf;
106     pend = buf + file_size;
107 
108     fread(buf, 1, file_size, fp);
109     printf("-----+- ADTS Frame Table -+------+
");
110     printf(" NUM | Profile | Frequency| Size |
");
111     printf("-----+---------+----------+------+
");
112     while(1) {
113         padts = getADTS(p, &pnext, pend);
114         if (padts == NULL) {
115             break;
116         }
117         
118         printf("%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d
",
119             ++num,
120             fix_header.ID,
121             fix_header.layer,
122             fix_header.protection_absent,
123             fix_header.profile ,
124             fix_header.sampling_frequency_index,
125             fix_header.private_bit,
126             fix_header.channel_configuration,
127             fix_header.original_copy ,
128             fix_header.home ,
129 
130             var_header.copyright_identification_bit,
131             var_header.copyright_identification_start,
132             var_header.aac_frame_length ,
133             var_header.adts_buffer_fullness,
134             var_header.number_of_raw_data_blocks_in_frame);
135         if (pnext == NULL) {
136             break;
137         }
138         p = pnext;
139     }
140 
141     fclose(fp);
142     free(buf);
143     return 0;
144 }
145 
146 int main(){
147     aac_analyse("nocturne.aac");
148     return 0;
149 }

 

以上是关于aac 解析代码的主要内容,如果未能解决你的问题,请参考以下文章

数据与封装格式AAC码流格式与解析

AAC_LC用LATM封装header信息解析

片段(Java) | 机试题+算法思路+考点+代码解析 2023

Android 逆向使用 Python 解析 ELF 文件 ( Capstone 反汇编 ELF 文件中的机器码数据 | 创建反汇编解析器实例对象 | 设置汇编解析器显示细节 )(代码片段

视音频数据处理入门:AAC音频码流解析

转载:AAC文件解析及解码