ffmpeg 解析aac adts格式的文件

根据http://blog.csdn.net/sz76211822/article/details/53670069所讲,可以先读取文件的二进制数据到缓冲区,然后从缓冲区起始地址检测FFF。如果是,则解析协议。

代码如下:

#include "stdafx.h"
#include 
#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 
#include 
#include 
#include 
#include 
#ifdef __cplusplus
};
#endif
#endif

typedef 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;//一般设置为0
	unsigned char protection_absent:1;//是否误码校验
	unsigned char profile:2;//表示使用哪个级别的AAC,如01 Low Complexity(LC)--- AACLC
	unsigned char sampling_frequency_index:4;//表示使用的采样率下标0x3 48k ,0x4 44.1k, 0x5 32k
	unsigned char private_bit:1;//一般设置为0
	unsigned char channel_configuration:3;// 表示声道数
	unsigned char original_copy:1;//一般设置为0
	unsigned char home:1;//一般设置为0

	unsigned char copyright_identification_bit:1;//一般设置为0
	unsigned char copyright_identification_start:1;//一般设置为0
	unsigned int frame_length:13;// 一个ADTS帧的长度包括ADTS头和raw data block
	unsigned 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

你可能感兴趣的:(文件解析)