视音频数据处理入门:AAC音频码流解析
=====================================================
視音頻數(shù)據(jù)處理入門系列文章:
視音頻數(shù)據(jù)處理入門:RGB、YUV像素?cái)?shù)據(jù)處理
視音頻數(shù)據(jù)處理入門:PCM音頻采樣數(shù)據(jù)處理
視音頻數(shù)據(jù)處理入門:H.264視頻碼流解析
視音頻數(shù)據(jù)處理入門:AAC音頻碼流解析
視音頻數(shù)據(jù)處理入門:FLV封裝格式解析
視音頻數(shù)據(jù)處理入門:UDP-RTP協(xié)議解析
=====================================================
本文繼續(xù)上一篇文章的內(nèi)容,介紹一個(gè)音頻碼流處理程序。音頻碼流在視頻播放器中的位置如下所示。
本文中的程序是一個(gè)AAC碼流解析程序。該程序可以從AAC碼流中分析得到它的基本單元ADTS frame,并且可以簡(jiǎn)單解析ADTS frame首部的字段。通過修改該程序可以實(shí)現(xiàn)不同的AAC碼流處理功能。
原理
AAC原始碼流(又稱為“裸流”)是由一個(gè)一個(gè)的ADTS frame組成的。他們的結(jié)構(gòu)如下圖所示。
代碼
整個(gè)程序位于simplest_aac_parser()函數(shù)中,如下所示。
/** * 最簡(jiǎn)單的視音頻數(shù)據(jù)處理示例 * Simplest MediaData Test * * 雷霄驊 Lei Xiaohua * leixiaohua1020@126.com * 中國傳媒大學(xué)/數(shù)字電視技術(shù) * Communication University of China / Digital TV Technology * http://blog.csdn.net/leixiaohua1020 * * 本項(xiàng)目包含如下幾種視音頻測(cè)試示例: * (1)像素?cái)?shù)據(jù)處理程序。包含RGB和YUV像素格式處理的函數(shù)。 * (2)音頻采樣數(shù)據(jù)處理程序。包含PCM音頻采樣格式處理的函數(shù)。 * (3)H.264碼流分析程序。可以分離并解析NALU。 * (4)AAC碼流分析程序。可以分離并解析ADTS幀。 * (5)FLV封裝格式分析程序。可以將FLV中的MP3音頻碼流分離出來。 * (6)UDP-RTP協(xié)議分析程序。可以將分析UDP/RTP/MPEG-TS數(shù)據(jù)包。 * * 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; }上文中的函數(shù)調(diào)用方法如下所示。
simplest_aac_parser("nocturne.aac");結(jié)果
本程序的輸入為一個(gè)AAC原始碼流(裸流)的文件路徑,輸出為該碼流中ADTS frame的統(tǒng)計(jì)數(shù)據(jù),如下圖所示。下載
Simplest mediadata test
項(xiàng)目主頁
SourceForge:https://sourceforge.net/projects/simplest-mediadata-test/
Github:https://github.com/leixiaohua1020/simplest_mediadata_test
開源中國: http://git.oschina.net/leixiaohua1020/simplest_mediadata_testCSDN下載地址: http://download.csdn.net/detail/leixiaohua1020/9422409
本項(xiàng)目包含如下幾種視音頻數(shù)據(jù)解析示例:
?(1)像素?cái)?shù)據(jù)處理程序。包含RGB和YUV像素格式處理的函數(shù)。
?(2)音頻采樣數(shù)據(jù)處理程序。包含PCM音頻采樣格式處理的函數(shù)。
?(3)H.264碼流分析程序。可以分離并解析NALU。
?(4)AAC碼流分析程序。可以分離并解析ADTS幀。
?(5)FLV封裝格式分析程序。可以將FLV中的MP3音頻碼流分離出來。
?(6)UDP-RTP協(xié)議分析程序。可以將分析UDP/RTP/MPEG-TS數(shù)據(jù)包。
雷霄驊 (Lei Xiaohua)
leixiaohua1020@126.com
http://blog.csdn.net/leixiaohua1020
總結(jié)
以上是生活随笔為你收集整理的视音频数据处理入门:AAC音频码流解析的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 视音频数据处理入门:PCM音频采样数据处
- 下一篇: 视音频数据处理入门:UDP-RTP协议解