AAC音频格式ADTS头详解
Posted 迷茫的蜉蝣
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AAC音频格式ADTS头详解相关的知识,希望对你有一定的参考价值。
裸流的AAC数据是没办法播放的,因为设备找不到AAC格式的相关信息;需要添加ADTS头才能够播放,每帧音频包都有一个ADTS头,ADTS头没有crc校验的话是7字节长度,有crc校验的话是9字节长度。
ADTS个字段协议:
字段 | 长度 | 描述 |
synword | 12bit | 固定0xFFF,用作同步,一帧的开始 |
id | 1bit | MPEG标识符,0:MPEG-4, 1:MPEG-2 |
layer | 2bit | 一般为00 |
protection_absent | 1bit | crc校验标识,0:有crc校验,1:没有crc校验 |
profile | 2bit | AAC级别,再ffmpeg的AVStream中:streams[audio]->codecpar->profile audio:帧索引 |
sampling_frequency_index | 4bit | 采样率下标,下标对应的采样率如下: 0: 96000 Hz streams[audio]->codecpar->sample_rate audio:帧索引 |
private_bit | 1bit | 私有位,编码时为0,解码时忽略 |
channel_configuration | 3bit | 声道数。 front - left:左声道 front - right:右声道 back - left:后置左 back - right:后置右 side - left:侧置左 side - right:侧置右 LFE - channel:低频声道 音频通道也在ffmpeg的AVStream中: streams[audio]->codecpar->channels audio:帧索引 |
orininal_copy | 1bit | 编码是设置为0,解码时忽略 |
home | 1bit | 编码时设置为0,解码时忽略 |
copyrigth_identification_bit | 1bit | 编码时设置为0,解码时忽略 |
copyrigth_identification_stat | 1bit | 编码时设置为0,解码时忽略 |
aac_frame_length | 13bit | 一个ADTS帧的⻓度,包括ADTS头和AAC原始流。 |
adts_bufferfullness | 11bit | 缓冲区充满度,0x7FF说明是码率可变的码流,不需要此字段。CBR可能需要此字段,不同编码器使用情况不同。具体查看附录。 |
number_of_raw_data_blocks_in_frame | 2bit | 表示ADTS帧中有number_of_raw_data_blocks_in_frame + 1个AAC原始帧,为0表示说ADTS帧中只有一个AAC数据. |
crc | 16bit | protection_absent为0就有该字段,否则没有该字段 |
ffmpeg解复用时,MP4,FLV格式的包解出的音频流是纯AAC流,不带ADTS头数据,需要人工添加ADTS头。
添加头的代码实现接口:
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; // 默认使用48000hz
int 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)
std::cout << "没有找到支持的采样率" << std::endl;
return -1;
p_adts_header[0] = 0xff; //前12bit固定0xfff 高8bits
p_adts_header[1] = 0xf0; //前12bit的低四位 低4bits
p_adts_header[1] |= (0 << 3); //0:MPEG-4, 1:MPEG-2 1bit
p_adts_header[1] |= (0 << 1); //一般为0 2bits
p_adts_header[1] |= 1; //1:没有crc校验字段 1bit
p_adts_header[2] = (profile) << 6; //aac级别,可以使用ffmpeg获取 2bits
p_adts_header[2] |=
(sampling_frequency_index & 0x0f) << 2; //可以使用ffgmpeg从包中获得 4bits
p_adts_header[2] |= (0 << 1); //私有位 编码时为0 1bit
p_adts_header[2] |= (channels & 0x04) >> 2; //3bit的声道设置的最高位 高1bit
p_adts_header[3] = (channels & 0x03) << 6; //3bit的声道设置的最低两位 低2bits
p_adts_header[3] |= (0 << 5); //编码设置为0 1bit
p_adts_header[3] |= (0 << 4); //编码设置为0 1bit
p_adts_header[3] |= (0 << 3); //编码设置为0 1bit
p_adts_header[3] |= (0 << 2); //编码设置为0 1bit
p_adts_header[3] |= ((adtsLen & 0x1800) >> 11); //帧长度包括ADTS头长度 高2bits
p_adts_header[4] = (uint8_t) ((adtsLen & 0x7f8) >> 3); //帧长度包括ADTS头长度 中间8bits
p_adts_header[5] = (uint8_t) ((adtsLen & 0x7) << 5); //帧长度包括ADTS头长度 低3bits
p_adts_header[5] |= 0x1f; //可变码率vbr:0x7ff 高5bits
p_adts_header[6] = 0xfc; //11111100 //buffer fullness:0x7ff 低6bits
return 0;
代码中的sampling_frequencies[]数组是一个sampling_frequency_index字段的采样率对照表 。
AAC ADTS头详解
AAC ADTS详解 结合:http://blog.csdn.net/jay100500/article/details/52955232 与下面的程序 输入 aac 文件 /** * 最简单的视音频数据处理示例 * Simplest MediaData Test * * 雷霄骅 Lei Xiaohua * [email protected] * 中国传媒大学/数字电视技术 * Communication University of China / Digital TV Technology * http://blog.csdn.net/leixiaohua1020 * * 本项目包含如下几种视音频测试示例: * (1)像素数据处理程序。包含RGB和YUV像素格式处理的函数。 * (2)音频采样数据处理程序。包含PCM音频采样格式处理的函数。 * (3)H.264码流分析程序。可以分离并解析NALU。 * (4)AAC码流分析程序。可以分离并解析ADTS帧。 * (5)FLV封装格式分析程序。可以将FLV中的MP3音频码流分离出来。 * (6)UDP-RTP协议分析程序。可以将分析UDP/RTP/MPEG-TS数据包。 * * This project contains following samples to handling multimedia data: * (1) Video pixel data handling program. It contains several examples to handle RGB and YUV data. * (2) Audio sample data handling program. It contains several examples to handle PCM data. * (3) H.264 stream analysis program. It can parse H.264 bitstream and analysis NALU of stream. * (4) AAC stream analysis program. It can parse AAC bitstream and analysis ADTS frame of stream. * (5) FLV format analysis program. It can analysis FLV file and extract MP3 audio stream. * (6) UDP-RTP protocol analysis program. It can analysis UDP/RTP/MPEG-TS Packet. * */ #include <stdio.h> #include <stdlib.h> #include <string.h> 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; } //Sync words if((buffer[0] == 0xff) && ((buffer[1] & 0xf0) == 0xf0) ) { 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) { return 1; } memcpy(data, buffer, size); *data_size = size; return 0; } int simplest_aac_parser(char *url) { int data_size = 0; int size = 0; int cnt=0; int offset=0; //FILE *myout=fopen("output_log.txt","wb+"); FILE *myout=stdout; unsigned char *aacframe=(unsigned char *)malloc(1024*5); unsigned char *aacbuffer=(unsigned char *)malloc(1024*1024); FILE *ifile = fopen(url, "rb"); if(!ifile){ printf("Open file error"); return -1; } printf("-----+- ADTS Frame Table -+------+\n"); printf(" NUM | Profile | Frequency| Size |\n"); printf("-----+---------+----------+------+\n"); while(!feof(ifile)){ 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){ 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; }
以上是关于AAC音频格式ADTS头详解的主要内容,如果未能解决你的问题,请参考以下文章
ffmpeg:如何将 AAC 音频数据包转换为 ADTS 格式