本文主要是介绍ffmpeg 解析aac adts格式的文件,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
根据http://blog.csdn.net/sz76211822/article/details/53670069所讲,可以先读取文件的二进制数据到缓冲区,然后从缓冲区起始地址检测FFF。如果是,则解析协议。
代码如下:
#include "stdafx.h"
#include <windows.h>
#ifdef _WIN32
//Windows
extern "C"
{
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libavutil/opt.h"
#include "libavutil/parseutils.h"
#include "libavutil/avutil.h"
};
#else
//Linux...
#ifdef __cplusplus
extern "C"
{
#endif
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavutil/opt.h>
#include <libavutil/parseutils.h>
#include <libavutil/avutil.h>
#ifdef __cplusplus
};
#endif
#endiftypedef struct _adts_header
{unsigned int syncword:12;//同步字0xfff,说明一个ADTS帧的开始unsigned char ID:1;//ID比较奇怪,标准文档中是这么说的”MPEG identifier, set to ‘1’. See ISO/IEC 11172-3″,但我写0了,也可以unsigned char layer:2;//一般设置为0unsigned char protection_absent:1;//是否误码校验unsigned char profile:2;//表示使用哪个级别的AAC,如01 Low Complexity(LC)--- AACLCunsigned char sampling_frequency_index:4;//表示使用的采样率下标0x3 48k ,0x4 44.1k, 0x5 32kunsigned char private_bit:1;//一般设置为0unsigned char channel_configuration:3;// 表示声道数unsigned char original_copy:1;//一般设置为0unsigned char home:1;//一般设置为0unsigned char copyright_identification_bit:1;//一般设置为0unsigned char copyright_identification_start:1;//一般设置为0unsigned int frame_length:13;// 一个ADTS帧的长度包括ADTS头和raw data blockunsigned int adts_buffer_fullness:11;// 0x7FF 说明是码率可变的码流unsigned char number_of_raw_data_blocks_in_frame:2;//表示ADTS帧中有number_of_raw_data_blocks_in_frame + 1个AAC原始帧.
};void Process()
{BYTE *btData = NULL;DWORD dwRead = 0;HANDLE hFile = CreateFile(L"E:\\video\\test.aac", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);if (hFile != INVALID_HANDLE_VALUE){DWORD dwFilesize = GetFileSize(hFile, NULL);btData = new BYTE[dwFilesize];ReadFile(hFile, btData, dwFilesize, &dwRead, NULL);CloseHandle(hFile);}if (dwRead < 9){if (btData){delete[] btData;btData = NULL;}return;}int j = 0;int i = 0;int nAdtsHeader_Size = 9;//根据AAC adts协议头可知,协议大小为9个字节while(i < dwRead - nAdtsHeader_Size){if(btData[i] == 0xff && (btData[i + 1]>>4) == 0xf){//判断是aac的adts封装格式DWORD dwErr = 0;printf("%d %d %02x %x %x %x", j, i, (btData[i]<<4) | (btData[i + 1]>>4), (btData[i + 1]>>3) & 0x1,(btData[i + 1]>>1) & 0x3,btData[i + 1] & 0x1);switch((btData[i + 2]>>6) & 0x3){case 0: printf(" Main");break;case 1:printf(" LC");break;case 2:printf(" SSR");break;default:printf(" unknown");break;}switch((btData[i + 2]>>2) & 0xF){case 0: printf(" 96000Hz");break;case 1: printf(" 88200Hz");break;case 2: printf(" 64000Hz");break;case 3: printf(" 48000Hz");break;case 4: printf(" 44100Hz");break;case 5: printf(" 32000Hz");break;case 6: printf(" 24000Hz");break;case 7: printf(" 22050Hz");break;case 8: printf(" 16000Hz");break;case 9: printf(" 12000Hz");break;case 10: printf(" 11025Hz");break;case 11: printf(" 8000Hz");break;default:printf(" unknown");break;}printf("%x %x %x %x %x %x %d %02x %x\n", (btData[i + 2]>>1) & 0x1,((btData[i + 2] & 0x1)<<2) | ((btData[i + 3]>>6) & 0x3),(btData[i + 3]>>5) & 0x1,(btData[i + 3]>>4) & 0x1,(btData[i + 3]>>3) & 0x1,(btData[i + 3]>>2) & 0x1,(((btData[i + 3]) & 0x3)<<11) | (btData[i + 4])<<3 | ((btData[i + 5]>>5) & 0x7),((btData[i + 5] & 0x1F)<<6)|((btData[i + 6]>>2) & 0x3F),btData[i + 6] & 0x3);j++;i += (((btData[i + 3]) & 0x3)<<11) | (btData[i + 4])<<3 | ((btData[i + 5]>>5) & 0x7);//偏移一个音频帧的大小}else{i++;}}if (btData){delete[] btData;btData = NULL;}return;
}int _tmain(int argc, _TCHAR* argv[])
{Process();return 0;
}
http://download.csdn.net/detail/sz76211822/9713309
这篇关于ffmpeg 解析aac adts格式的文件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!