1、概述

把上一篇文章中讲到的mpeg-2文件结构分析用代码实现,结合mpeg-2文件分析。才easy看懂。

2、代码

/*
*本程序主要分析MPEG-2文件
*作者:缪国凯(MK)
*821486004@qq.com
*2015-7-8
*/ #include "stdafx.h" #define PRINTLOG PrintLog enum PESTYPE
{
AUDIOPES = 0,
VIDEOPES
}; void Pack_header(FILE *p);
void AnalysisSystemHeader(FILE *p);
void AnalysisPes(FILE *p, BYTE type);
void Sequence_header(FILE*p, WORD &length);
void Sequence_extension(FILE *p, WORD &length);
void Group_of_pictures_header(FILE *p, WORD &length);
void Picture_header(FILE *p, WORD &length);
void picture_coding_extension(FILE *p, WORD &length); FILE *pLogFile = NULL; void PrintLog (const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vfprintf(stdout,fmt,ap);//输出到屏幕
if (NULL != pLogFile)
{
vfprintf(pLogFile,fmt,ap);//输出一份到文件
}
va_end(ap);
} void help()
{
printf("**********************************************\n");
printf("Usage:\n");
printf(" mpeg2Container [inputfile] [logfile(default)]\n");
printf("\n");
printf("Examples: \n");
printf(" mpeg2Container test.mpg log.txt\n");
printf("or you can use like this: \n");
printf(" mpeg2Container test.mpg\n");
printf("the above command will write log with logFile.txt\n");
printf("**********************************************\n");
} int _tmain(int argc, _TCHAR* argv[])
{
if (argc < 2)
{
help();
return -1;
} if (argc >= 3)
{
pLogFile = fopen(argv[2], "w");
if (NULL == pLogFile)
{
PrintLog("can not write log file %s!\n", argv[2]);
}
}
else
{
pLogFile = fopen("logFile.txt", "w");
if (NULL == pLogFile)
{
PrintLog("can not write log file logFile.txt!\n");
}
} FILE *p = NULL;
p = fopen(argv[1], "rb");
if (NULL == p)
{
PrintLog("can not open input file!\n");
goto end;
}
BYTE tmp[4];
bool bFirstPack = true;
int address = 0;
while(1)
{
//把文件读取位置移动到能被2整除的位置
address = ftell(p);
if (address % 2 != 0)
{
fseek(p, -1, SEEK_CUR);
}
memset(tmp, 0, 4);
int sizet = fread(tmp, 1, 2, p);
if(2 != sizet)
{
break;
}
if (tmp[0] == 0x00 && tmp[1] == 0x00)
{
sizet = fread(tmp, 1, 2, p);
if(2 != sizet)
{
break;
} if (tmp[0] == 0x01 && tmp[1] == 0xBA)//Program Pack
{
PRINTLOG("this is a program pack-----adrees:0x%08x\n", ftell(p) - sizet -2);
Pack_header(p);
}
else if (tmp[0] == 0x01 && tmp[1] == 0xBB)//system header
{
PRINTLOG("this is a system pack-----adrees:0x%08x\n", ftell(p) - sizet - 2);
AnalysisSystemHeader(p);
}
else if (tmp[0] == 0x01 && tmp[1] == 0xB9)//end mark
{
PRINTLOG("this is the file end------adrees:0x%08x\n", ftell(p) - sizet - 2);
}
else if (tmp[0] == 0x01 && tmp[1] == 0xBE)//padding stream pes
{
PRINTLOG("this is a padding pes------adrees:0x%08x\n", ftell(p) - sizet - 2);
}
else if (tmp[0] == 0x01 && (tmp[1] >= 0xE0 && tmp[1] < 0xf0))//pes header 111x xxxx video
{
PRINTLOG("this is a video pes pack-----adrees:0x%08x\n", ftell(p) - sizet - 2);
AnalysisPes(p, VIDEOPES);
}
else if (tmp[0] == 0x01 && (tmp[1] >= 0xC0 && tmp[1] < 0xE0))//pes header 11xx xxxx audio
{
PRINTLOG("this is a audio pes pack-----adrees:0x%08x\n", ftell(p) - sizet - 2);
AnalysisPes(p, AUDIOPES);
}
else
{
//假设遇到 00 00 00 00,不倒回去 就会推断出错
fseek(p, -2, SEEK_CUR);
}
}
}
end:
if (NULL != pLogFile)
{
fclose(pLogFile);
}
if (NULL != p)
{
fclose(p);
}
return 0;
} struct PackHeader
{
unsigned paddingByte : 2;
unsigned program_clock_reference_base : 3;
unsigned marker1 : 1;
unsigned SCR_base1 : 15;
unsigned marker2 : 1;
unsigned SCR_base2 : 15;
unsigned marker3 : 1;
unsigned SCR_externsion : 9;
unsigned marker4 : 1; unsigned mutiplex_rate : 22;
unsigned marker5 : 1;
unsigned marker6 : 1; unsigned reserved : 5;
unsigned stuffing_length : 3;
}; void Pack_header(FILE *p)
{
//这个包没什么意义,就不解析了
} struct SystemHeader//112bit
{
unsigned head_length : 16; unsigned marker1 : 1;
unsigned rate_bound : 22;
unsigned marker2 : 1; unsigned audio_bound : 6;
unsigned fixed_flag : 1;
unsigned CSPS_flag : 1; unsigned system_audio_local_flag : 1;
unsigned system_video_local_flag : 1;
unsigned marker3 : 1;
unsigned video_bound : 5; unsigned packet_rate_restriction_flag : 1;
unsigned reserved : 7; unsigned stream_id1 : 8; unsigned paddingByte1 : 2;
unsigned P_STD_buffer_bound_scale1 : 1;
unsigned P_STD_buffer_size_bound1 : 13; unsigned stream_id2 : 8; unsigned paddingByte2 : 2;
unsigned P_STD_buffer_bound_scale2 : 1;
unsigned P_STD_buffer_size_bound2 : 13;
}; void AnalysisSystemHeader(FILE *p)
{
BYTE tmpL[2];
if (2 != fread(tmpL, 1, 2, p))
{
return;
}
WORD tmpLength = 0;
tmpLength |= tmpL[0] << 8;
tmpLength |= tmpL[1];
if (tmpLength >= 12)
{
fseek(p, -2, SEEK_CUR);
}
else
{
return;
} BYTE *tmp = new BYTE[sizeof(SystemHeader)];
memset(tmp, 0, sizeof(SystemHeader)); SystemHeader tmpSystemHeader;
memset(&tmpSystemHeader, 0, sizeof(SystemHeader));
int size = sizeof(SystemHeader);
size = /*sizeof(SystemHeader)*/14;
int sizet = size;
sizet = fread(tmp, 1, size, p);
if (size != sizet)
{
return;
}
tmpSystemHeader.head_length |= tmp[0] << 8;
tmpSystemHeader.head_length |= tmp[1]; tmpSystemHeader.marker1 |= tmp[2] >> 7;
tmpSystemHeader.rate_bound |= tmp[2] << 15;
tmpSystemHeader.rate_bound |= tmp[3] << 7;
tmpSystemHeader.rate_bound |= tmp[4] >> 1;
tmpSystemHeader.marker2 |= tmp[4]; tmpSystemHeader.audio_bound |= tmp[5] >> 2;
tmpSystemHeader.fixed_flag |= tmp[5] >> 1;
tmpSystemHeader.CSPS_flag |= tmp[5]; tmpSystemHeader.system_audio_local_flag |= tmp[6] >> 7;
tmpSystemHeader.system_video_local_flag |= tmp[6] >> 6;
tmpSystemHeader.marker3 |= tmp[6] >> 5;
tmpSystemHeader.video_bound |= tmp[6]; tmpSystemHeader.packet_rate_restriction_flag |= tmp[7] >> 7;
tmpSystemHeader.reserved |= tmp[7]; tmpSystemHeader.stream_id1 |= tmp[8]; tmpSystemHeader.paddingByte1 |= tmp[9] >> 6;
tmpSystemHeader.P_STD_buffer_bound_scale1 |= tmp[9] >> 5;
tmpSystemHeader.P_STD_buffer_size_bound1 |= tmp[9] << 8;
tmpSystemHeader.P_STD_buffer_size_bound1 |= tmp[10]; tmpSystemHeader.stream_id2 |= tmp[11]; tmpSystemHeader.paddingByte2 |= tmp[12] >> 6;
tmpSystemHeader.P_STD_buffer_bound_scale2 |= tmp[12] >> 5;
tmpSystemHeader.P_STD_buffer_size_bound2 |= tmp[12] << 8;
tmpSystemHeader.P_STD_buffer_size_bound2 |= tmp[13]; PRINTLOG("head_length = 0x%02x\n", tmpSystemHeader.head_length); PRINTLOG("marker1 = 0x%02x\n", tmpSystemHeader.marker1);
PRINTLOG("rate_bound = 0x%02x\n", tmpSystemHeader.rate_bound);
PRINTLOG("marker2 = 0x%02x\n", tmpSystemHeader.marker2); PRINTLOG("audio_bound = 0x%02x\n", tmpSystemHeader.audio_bound);
PRINTLOG("fixed_flag = 0x%02x\n", tmpSystemHeader.fixed_flag);
PRINTLOG("CSPS_flag = 0x%02x\n", tmpSystemHeader.CSPS_flag); PRINTLOG("system_audio_local_flag = 0x%02x\n", tmpSystemHeader.system_audio_local_flag);
PRINTLOG("system_video_local_flag = 0x%02x\n", tmpSystemHeader.system_video_local_flag);
PRINTLOG("marker3 = 0x%02x\n", tmpSystemHeader.marker3);
PRINTLOG("video_bound = 0x%02x\n", tmpSystemHeader.video_bound); PRINTLOG("packet_rate_restriction_flag = 0x%02x\n", tmpSystemHeader.packet_rate_restriction_flag);
PRINTLOG("reserved = 0x%02x\n", tmpSystemHeader.reserved); PRINTLOG("stream_id1 = 0x%02x\n", tmpSystemHeader.stream_id1); PRINTLOG("paddingByte1 = 0x%02x\n", tmpSystemHeader.paddingByte1);
PRINTLOG("P_STD_buffer_bound_scale1 = 0x%02x\n", tmpSystemHeader.P_STD_buffer_bound_scale1);
PRINTLOG("P_STD_buffer_size_bound1 = 0x%02x\n", tmpSystemHeader.P_STD_buffer_size_bound1); PRINTLOG("stream_id2 = 0x%02x\n", tmpSystemHeader.stream_id2); PRINTLOG("paddingByte2 = 0x%02x\n", tmpSystemHeader.paddingByte2);
PRINTLOG("P_STD_buffer_bound_scale2 = 0x%02x\n", tmpSystemHeader.P_STD_buffer_bound_scale2);
PRINTLOG("P_STD_buffer_size_bound2 = 0x%02x\n", tmpSystemHeader.P_STD_buffer_size_bound2); delete[] tmp;
} struct PESPacket
{
unsigned PES_packet_length : 16; unsigned paddingByte1 : 2;
unsigned scrambling_control : 2;
unsigned priority : 1;
unsigned alignment : 1;
unsigned copyright : 1;
unsigned original : 1; unsigned PTS_DTS_flag : 2;
unsigned ESCR_flag : 1;
unsigned ES_rate_flag : 1;
unsigned DSM_trick_mode_flag : 1;
unsigned additional_copy_info_flag : 1;
unsigned CRC_flag : 1;
unsigned extension_flag : 1; unsigned PES_header_data_length : 8; unsigned paddingByte2 : 4;
unsigned PTS_32_30 : 3;//pts 的第32-30位
unsigned marker1 : 1; unsigned PTS_29_15 : 15;//pts的第29-15位
unsigned marker2 : 1; unsigned PTS_14_0 : 15;//pts的第14-0位(共33位)
unsigned marker3 : 1; unsigned paddingByte3 : 4;
unsigned DTS_32_30 : 3;
unsigned marker4 : 1; unsigned DTS_29_15 : 15;
unsigned marker5 : 1; unsigned DTS_14_0 : 15;
unsigned marker6 : 1;
}; void AnalysisPes(FILE *p, BYTE type)
{
WORD length;
BYTE tmpL[2];
//read the header length
int sizet = fread(&tmpL, 1, 2, p);
if(2 != sizet)
{
return;
} length = tmpL[0] << 8 | tmpL[1];
PRINTLOG("length = %d\n", length); if (length >= 15)
{
fseek(p, -2, SEEK_CUR); BYTE *tmp = new BYTE[sizeof(PESPacket)];
PESPacket tmpPESPacket;
memset(&tmpPESPacket, 0, sizeof(PESPacket)); int size = /*sizeof(SequenceHeader)*/15;
int sizet = size; sizet = fread(tmp, 1, size, p);
length -= sizet;
if (size != sizet)
{
return;
}
tmpPESPacket.PES_packet_length = tmp[0] << 8 | tmp[1]; tmpPESPacket.paddingByte1 |= tmp[2] >> 6;
tmpPESPacket.scrambling_control |= tmp[2] >> 4;
tmpPESPacket.priority |= tmp[2] >> 3;
tmpPESPacket.alignment |= tmp[2] >> 2;
tmpPESPacket.copyright |= tmp[2] >> 1;
tmpPESPacket.original |= tmp[2]; tmpPESPacket.PTS_DTS_flag |= tmp[3] >> 6;
tmpPESPacket.ESCR_flag |= tmp[3] >> 5;
tmpPESPacket.ES_rate_flag |= tmp[3] >> 4;
tmpPESPacket.DSM_trick_mode_flag |= tmp[3] >> 3;
tmpPESPacket.additional_copy_info_flag |= tmp[3] >> 2;
tmpPESPacket.CRC_flag |= tmp[3] >> 1;
tmpPESPacket.extension_flag |= tmp[3]; tmpPESPacket.PES_header_data_length |= tmp[4]; tmpPESPacket.paddingByte2 |= tmp[5] >> 4;
tmpPESPacket.PTS_32_30 |= tmp[5] >> 1;
tmpPESPacket.marker1 |= tmp[5]; tmpPESPacket.PTS_29_15 |= tmp[6] << 7;
tmpPESPacket.PTS_29_15 |= tmp[7] >> 1;
tmpPESPacket.marker2 |= tmp[7]; tmpPESPacket.PTS_14_0 |= tmp[8] << 7;
tmpPESPacket.PTS_14_0 |= tmp[9] >> 1;
tmpPESPacket.marker3 |= tmp[9]; tmpPESPacket.paddingByte3 |= tmp[10] >> 4;
tmpPESPacket.DTS_32_30 |= tmp[10] >> 1;
tmpPESPacket.marker4 |= tmp[10]; tmpPESPacket.DTS_29_15 |= tmp[11] << 7;
tmpPESPacket.DTS_29_15 |= tmp[12] >> 1;
tmpPESPacket.marker5 |= tmp[12]; tmpPESPacket.DTS_14_0 |= tmp[13] << 7;
tmpPESPacket.DTS_14_0 |= tmp[14] >> 1;
tmpPESPacket.marker6 |= tmp[14]; PRINTLOG("PES_packet_length = 0x%04x\n", tmpPESPacket.PES_packet_length);
PRINTLOG("scrambling_control = 0x%04x\n", tmpPESPacket.scrambling_control);
PRINTLOG("priority = 0x%04x\n", tmpPESPacket.priority);
PRINTLOG("alignment = 0x%04x\n", tmpPESPacket.alignment);
PRINTLOG("copyright = 0x%04x\n", tmpPESPacket.copyright);
PRINTLOG("original = 0x%04x\n", tmpPESPacket.original);
PRINTLOG("PTS_DTS_flag = 0x%04x\n", tmpPESPacket.PTS_DTS_flag);
PRINTLOG("ESCR_flag = 0x%04x\n", tmpPESPacket.ESCR_flag);
PRINTLOG("ES_rate_flag = 0x%04x\n", tmpPESPacket.ES_rate_flag);
PRINTLOG("DSM_trick_mode_flag = 0x%04x\n", tmpPESPacket.DSM_trick_mode_flag);
PRINTLOG("additional_copy_info_flag = 0x%04x\n", tmpPESPacket.additional_copy_info_flag);
PRINTLOG("CRC_flag = 0x%04x\n", tmpPESPacket.CRC_flag);
PRINTLOG("extension_flag = 0x%04x\n", tmpPESPacket.extension_flag);
PRINTLOG("PES_header_data_length = 0x%04x\n", tmpPESPacket.PES_header_data_length); if (tmpPESPacket.PTS_DTS_flag == 3 || tmpPESPacket.PTS_DTS_flag == 2)
{
INT64 pts = 0;
pts |= tmpPESPacket.PTS_32_30 << 30;
pts |= tmpPESPacket.PTS_29_15 << 15;
pts |= tmpPESPacket.PTS_14_0;
PRINTLOG("PTS = 0x%04x\n", pts);
} if (tmpPESPacket.PTS_DTS_flag == 3)
{
INT64 dts = 0;
dts |= tmpPESPacket.DTS_32_30 << 30;
dts |= tmpPESPacket.DTS_29_15 << 15;
dts |= tmpPESPacket.DTS_14_0;
PRINTLOG("DTS = 0x%04x\n", dts);
} //有PESPacket不完整的情况(还没分析原因),这里跳回去。避免漏掉关键标志
fseek(p, -15, SEEK_CUR);
bool bHaveSequenceHeader = false;
bool bSequence_extension = false;
while(length > 4)
{
memset(tmp, 0, 8);
int sizet = fread(tmp, 1, 1, p);
length -= sizet;
if(1 != sizet)
{
break;
}
if (tmp[0] == 0x00)
{
memset(tmp, 0, 8);
int sizet = fread(tmp, 1, 3, p);
length -= sizet;
if(3 != sizet)
{
break;
} if (tmp[0] == 0x00 && tmp[1] == 0x01 && tmp[2] == 0xB3)//Sequence header
{
bHaveSequenceHeader = true;
PRINTLOG("this is a Sequence header-----adrees:0x%08x\n", ftell(p) - sizet - 1);
Sequence_header(p, length);
}
else if (tmp[0] == 0x00 && tmp[1] == 0x01 && tmp[2] == 0xB5)
{
if (bHaveSequenceHeader && !bSequence_extension)//Sequence extension
{
bSequence_extension = true;
PRINTLOG("this is a Sequence_extension-----adrees:0x%08x\n", ftell(p) - sizet - 1);
Sequence_extension(p, length);
}
else//picture_coding_extension
{
PRINTLOG("this is a picture coding extension-----adrees:0x%08x\n", ftell(p) - sizet - 1);
picture_coding_extension(p, length);
}
}
else if (tmp[0] == 0x00 && tmp[1] == 0x01 && tmp[2] == 0xB8)//Group_of_pictures_header
{
PRINTLOG("this is a Group_of_pictures_header-----adrees:0x%08x\n", ftell(p) - sizet - 1);
Group_of_pictures_header(p, length);
}
else if (tmp[0] == 0x00 && tmp[1] == 0x01 && tmp[2] == 0x00)//Picture_header
{
PRINTLOG("this is a Picture_header-----adrees:0x%08x\n", ftell(p) - sizet - 1);
Picture_header(p, length);
}
else
{
fseek(p, -3, SEEK_CUR);
length += 3;
}
}
}
delete[] tmp;
}
else
{
return;
}
} struct SequenceHeader
{
unsigned horizontal_size : 12;
unsigned vertical_size : 12; unsigned aspect_ratio_info : 4;
unsigned frame_rate : 4; unsigned bit_rate : 18;
unsigned marker : 1;
unsigned VBV_buffer_size : 10;
unsigned constrained : 1;
unsigned load_intra_Q_matrix : 1;
unsigned paddingBit : 1;
}; void Sequence_header(FILE*p, WORD &length)
{
if (length < sizeof(SequenceHeader))
{
return;
}
BYTE *tmp = new BYTE[sizeof(SequenceHeader)];
memset(tmp, 0, sizeof(SequenceHeader)); SequenceHeader tmpSequenceHeader;
memset(&tmpSequenceHeader, 0, sizeof(SequenceHeader));
int size = /*sizeof(SequenceHeader)*/8;
int sizet = size; sizet = fread(tmp, 1, size, p);
length -= sizet;
if (size != sizet)
{
return;
} tmpSequenceHeader.horizontal_size = tmp[0] << 4;
tmpSequenceHeader.horizontal_size |= tmp[1] >> 4; tmpSequenceHeader.vertical_size = tmp[2];
tmp[1] <<= 4;
tmpSequenceHeader.vertical_size |= tmp[1] << 4; tmpSequenceHeader.aspect_ratio_info = tmp[3] >> 4;
tmp[3] <<= 4;
tmpSequenceHeader.frame_rate = tmp[3] >> 4; tmpSequenceHeader.bit_rate = tmp[4] << 10;
tmpSequenceHeader.bit_rate |= tmp[5] << 2;
tmpSequenceHeader.bit_rate |= tmp[6] >> 6; tmpSequenceHeader.marker |= tmp[6] >> 5; tmpSequenceHeader.VBV_buffer_size = tmp[6] << 5;
tmpSequenceHeader.VBV_buffer_size |= tmp[7] >> 3; tmpSequenceHeader.constrained != tmp[7] >> 2;
tmpSequenceHeader.load_intra_Q_matrix != tmp[7] >> 1;
tmpSequenceHeader.paddingBit != tmp[7]; PRINTLOG("horizontal_size = 0x%04x\n", tmpSequenceHeader.horizontal_size);
PRINTLOG("vertical_size = 0x%04x\n", tmpSequenceHeader.vertical_size);
PRINTLOG("aspect_ratio_info = 0x%04x\n", tmpSequenceHeader.aspect_ratio_info);
PRINTLOG("frame_rate = 0x%04x\n", tmpSequenceHeader.frame_rate);
PRINTLOG("bit_rate = 0x%04x\n", tmpSequenceHeader.bit_rate);
PRINTLOG("VBV_buffer_size = 0x%04x\n", tmpSequenceHeader.VBV_buffer_size);
PRINTLOG("constrained = 0x%04x\n", tmpSequenceHeader.constrained);
PRINTLOG("load_intra_Q_matrix = 0x%04x\n", tmpSequenceHeader.load_intra_Q_matrix); delete[] tmp;
} struct SequenceExtension
{
unsigned start_code_identifer : 4;
unsigned profile_level_escape : 1;
unsigned profile_level : 7;
unsigned progressive : 1;
unsigned chroma : 2;
unsigned horiz_extension : 2;
unsigned vertical_extension : 2;
}; void Sequence_extension(FILE *p, WORD &length)
{
int size = /*sizeof(SequenceExtension)*/3;
if (length < size)
{
return;
}
SequenceExtension tmpSequenceExtension;
memset(&tmpSequenceExtension, 0, sizeof(SequenceExtension)); BYTE *tmp = new BYTE[sizeof(SequenceExtension)];
memset(tmp, 0, sizeof(SequenceExtension)); int sizet = size;
sizet = fread(tmp, 1, size, p);
length -= sizet;
if (size != sizet)
{
return;
} tmpSequenceExtension.start_code_identifer = tmp[0] >> 4; //4bit 1th - 4th bit
tmpSequenceExtension.profile_level_escape |= tmp[0] >> 3; //1bit 5th bit tmpSequenceExtension.profile_level |= tmp[0] << 4; //7bit 6-8th bit
tmpSequenceExtension.profile_level |= tmp[1] >> 4; // 9-12th bit tmpSequenceExtension.progressive |= tmp[1] >> 3; //1bit 13th bit
tmpSequenceExtension.chroma |= tmp[1] >> 1; //2bit 14-15th bit tmpSequenceExtension.horiz_extension |= tmp[1] << 1; //2bit 16th bit
tmpSequenceExtension.horiz_extension |= tmp[2] >> 7; // 17th bit tmpSequenceExtension.vertical_extension |= tmp[2] >> 5; //2bit 18-19th bit PRINTLOG("start_code_identifer = 0x%04x\n", tmpSequenceExtension.start_code_identifer);
PRINTLOG("profile_level_escape = 0x%04x\n", tmpSequenceExtension.profile_level_escape);
PRINTLOG("profile_level = 0x%04x\n", tmpSequenceExtension.profile_level);
PRINTLOG("progressive = 0x%04x\n", tmpSequenceExtension.progressive);
PRINTLOG("chroma = 0x%04x\n", tmpSequenceExtension.chroma);
PRINTLOG("horiz_extension = 0x%04x\n", tmpSequenceExtension.horiz_extension);
PRINTLOG("vertical_extension = 0x%04x\n", tmpSequenceExtension.vertical_extension); delete[] tmp;
} struct GroupOfPicturesHeader
{
unsigned time_code : 25;
unsigned closed_gop : 1;
unsigned broken_link : 1;
}; void Group_of_pictures_header(FILE *p, WORD &length)
{
int size = /*sizeof(GroupOfPicturesHeader)*/4;
if (length < size)
{
return;
}
GroupOfPicturesHeader tmpGroupOfPicturesHeader;
memset(&tmpGroupOfPicturesHeader, 0, sizeof(GroupOfPicturesHeader)); BYTE *tmp = new BYTE[sizeof(GroupOfPicturesHeader)];
memset(tmp, 0, sizeof(GroupOfPicturesHeader)); int sizet = size;
sizet = fread(tmp, 1, size, p);
length -= sizet;
if (size != sizet)
{
return;
} tmpGroupOfPicturesHeader.time_code |= tmp[0] << 17;
tmpGroupOfPicturesHeader.time_code |= tmp[1] << 9;
tmpGroupOfPicturesHeader.time_code |= tmp[2] << 1;
tmpGroupOfPicturesHeader.time_code |= tmp[3] >> 7; tmpGroupOfPicturesHeader.closed_gop |= tmp[3] >> 6;
tmpGroupOfPicturesHeader.broken_link |= tmp[3] >> 5; PRINTLOG("time_code = 0x%04x\n", tmpGroupOfPicturesHeader.time_code);
PRINTLOG("closed_gop = 0x%04x\n", tmpGroupOfPicturesHeader.closed_gop);
PRINTLOG("broken_link = 0x%04x\n", tmpGroupOfPicturesHeader.broken_link); delete[] tmp;
} struct PictureHeader
{
unsigned temporal_reference : 10;
unsigned picture_coding_type : 3;
unsigned vbv_delay : 16;
unsigned extra_bit_piture : 1;
}; void Picture_header(FILE *p, WORD &length)
{
int size = /*sizeof(PictureHeader)*/4;
if (length < size)
{
return;
}
PictureHeader tmpPictureHeader;
memset(&tmpPictureHeader, 0, sizeof(PictureHeader)); BYTE *tmp = new BYTE[sizeof(PictureHeader)];
memset(tmp, 0, sizeof(PictureHeader)); int sizet = size;
sizet = fread(tmp, 1, size, p);
length -= sizet;
if (size != sizet)
{
return;
} tmpPictureHeader.temporal_reference |= tmp[0] << 2;
tmpPictureHeader.temporal_reference |= tmp[1] >> 6; tmpPictureHeader.picture_coding_type |= tmp[1] >> 3; tmpPictureHeader.vbv_delay |= tmp[1] << 13;
tmpPictureHeader.vbv_delay |= tmp[2] << 5;
tmpPictureHeader.vbv_delay |= tmp[3] >> 3; tmpPictureHeader.extra_bit_piture |= tmp[3] >> 2; PRINTLOG("temporal_reference = 0x%04x\n", tmpPictureHeader.temporal_reference);
PRINTLOG("picture_coding_type = 0x%04x\n", tmpPictureHeader.picture_coding_type);
PRINTLOG("vbv_delay = 0x%04x\n", tmpPictureHeader.vbv_delay);
PRINTLOG("extra_bit_piture = 0x%04x\n", tmpPictureHeader.extra_bit_piture); delete[] tmp;
} struct PictureCodingExtension
{
unsigned extension_start_code_identifier : 4;
unsigned f_code_0_0 : 4; unsigned f_code_0_1 : 4;
unsigned f_code_1_0 : 4; unsigned f_code_1_1 : 4;
unsigned intra_dc_presison : 2;
unsigned picture_structure : 2; unsigned top_field_first : 1;
unsigned frame_pred_frame_dct : 1;
unsigned concealment_motion_vectors : 1;
unsigned q_scale_type : 1;
unsigned intra_vlc_format : 1;
unsigned alternate_scan : 1;
unsigned repeat_first_field : 1;
unsigned chroma_420_type : 1; unsigned progressive_frame : 2;
unsigned composite_display_flag : 2;
}; void picture_coding_extension(FILE *p, WORD &length)
{
int size = /*sizeof(PictureCodingExtension)*/5;
if (length < size)
{
return;
}
PictureCodingExtension tmpPictureCodingExtension;
memset(&tmpPictureCodingExtension, 0, sizeof(PictureCodingExtension)); BYTE *tmp = new BYTE[sizeof(PictureCodingExtension)];
memset(tmp, 0, sizeof(PictureCodingExtension)); int sizet = size;
sizet = fread(tmp, 1, size, p);
length -= sizet;
if (size != sizet)
{
return;
} tmpPictureCodingExtension.extension_start_code_identifier |= tmp[0] >> 4;
tmpPictureCodingExtension.f_code_0_0 |= tmp[0]; tmpPictureCodingExtension.f_code_0_1 |= tmp[1] >> 4;
tmpPictureCodingExtension.f_code_1_0 |= tmp[1]; tmpPictureCodingExtension.f_code_1_1 |= tmp[2] >> 4;
tmpPictureCodingExtension.intra_dc_presison |= tmp[2] >> 2;
tmpPictureCodingExtension.picture_structure |= tmp[2]; tmpPictureCodingExtension.top_field_first |= tmp[3] >> 7;
tmpPictureCodingExtension.frame_pred_frame_dct |= tmp[3] >> 6;
tmpPictureCodingExtension.concealment_motion_vectors |= tmp[3] >> 5;
tmpPictureCodingExtension.q_scale_type |= tmp[3] >> 4;
tmpPictureCodingExtension.intra_vlc_format |= tmp[3] >> 3;
tmpPictureCodingExtension.alternate_scan |= tmp[3] >> 2;
tmpPictureCodingExtension.repeat_first_field |= tmp[3] >> 1;
tmpPictureCodingExtension.chroma_420_type |= tmp[3]; tmpPictureCodingExtension.progressive_frame |= tmp[3] >> 6;
tmpPictureCodingExtension.composite_display_flag |= tmp[3] >> 4; PRINTLOG("extension_start_code_identifier = 0x%04x\n", tmpPictureCodingExtension.extension_start_code_identifier);
PRINTLOG("f_code_0_0 = 0x%04x\n", tmpPictureCodingExtension.f_code_0_0); PRINTLOG("f_code_0_1 = 0x%04x\n", tmpPictureCodingExtension.f_code_0_1);
PRINTLOG("f_code_1_0 = 0x%04x\n", tmpPictureCodingExtension.f_code_1_0); PRINTLOG("f_code_1_1 = 0x%04x\n", tmpPictureCodingExtension.f_code_1_1);
PRINTLOG("intra_dc_presison = 0x%04x\n", tmpPictureCodingExtension.intra_dc_presison);
PRINTLOG("picture_structure = 0x%04x\n", tmpPictureCodingExtension.picture_structure); PRINTLOG("top_field_first = 0x%04x\n", tmpPictureCodingExtension.top_field_first);
PRINTLOG("frame_pred_frame_dct = 0x%04x\n", tmpPictureCodingExtension.frame_pred_frame_dct);
PRINTLOG("concealment_motion_vectors = 0x%04x\n", tmpPictureCodingExtension.concealment_motion_vectors);
PRINTLOG("q_scale_type = 0x%04x\n", tmpPictureCodingExtension.q_scale_type);
PRINTLOG("intra_vlc_format = 0x%04x\n", tmpPictureCodingExtension.intra_vlc_format);
PRINTLOG("alternate_scan = 0x%04x\n", tmpPictureCodingExtension.alternate_scan);
PRINTLOG("repeat_first_field = 0x%04x\n", tmpPictureCodingExtension.repeat_first_field);
PRINTLOG("chroma_420_type = 0x%04x\n", tmpPictureCodingExtension.chroma_420_type); PRINTLOG("progressive_frame = 0x%04x\n", tmpPictureCodingExtension.progressive_frame);
PRINTLOG("composite_display_flag = 0x%04x\n", tmpPictureCodingExtension.composite_display_flag); delete[] tmp;
}

3、project下载地址


代码实现分析mpeg-2文件的更多相关文章

  1. Xdebug文档(五) 代码覆盖分析

    代码覆盖分析能在请求时让你知道脚本哪一行(或哪一段)在执行. 相关设置 xdebug.coverage_enable 类型: boolean, 默认值: 1, 始于 Xdebug >= 2.2 ...

  2. 11个Visual Studio代码性能分析工具

    软件开发中的性能优化对程序员来说是一个非常重要的问题.一个小问题可能成为一个大的系统的瓶颈.但是对于程序员来说,通过自身去优化代码是十分困难的.幸运的是,有一些非常棒的工具可以帮助程序员进行代码分析和 ...

  3. 通过从代码层面分析Linux内核启动来探知操作系统的启动过程

    通过从代码层面分析Linux内核启动来探知操作系统的启动过程 前言说明 本篇为网易云课堂Linux内核分析课程的第三周作业,我将围绕Linux 3.18的内核中的start_kernel到init进程 ...

  4. NetSarang软件中nssock2.dll模块被植入恶意代码技术分析与防护方案

    原文地址:http://blog.nsfocus.net/nssock2-dll-module-malicious-code-analysis-report/ NetSarang是一家提供安全连接解决 ...

  5. 使用Crash工具分析 Linux dump文件【转】

    转自:https://blog.csdn.net/bytxl/article/details/45025183 前言 Linux 内核(以下简称内核)是一个不与特定进程相关的功能集合,内核的代码很难轻 ...

  6. .NET 11 个 Visual Studio 代码性能分析工具

    原文地址 软件开发中的性能优化对程序员来说是一个非常重要的问题.一个小问题可能成为一个大的系统的瓶颈.但是对于程序员来说,通过自身去优化代码是十分困难的.幸运的是,有一些非常棒的工具可以帮助程序员进行 ...

  7. Jenkins报表 代码 指标分析

    Jenkins报表 这表现在前面的章节中,也有可用最简单的一种是适用于 JUnit 测试报告的许多报表插件. 在生成后动作进行任何工作,你可以定义要创建的报告. 该构建已经完成,测试结果选项将可进一步 ...

  8. 使用 Crash 工具分析 Linux dump 文件

    转自:http://blog.csdn.net/commsea/article/details/11804897 简介: Linux 内核由于其复杂性,使得对内核出现的各种异常的追踪变得异常困难.本文 ...

  9. linux-2.6.22.6内核启动分析之Makefile文件

    学习目标 分析Makefile文件,了解内核中的哪些文件被编译,如何被编译,连接时顺序如何确定! Linux内核源码中包含很多的Makefile文件,这些Makefile文件又包含其它的一些文件,比如 ...

随机推荐

  1. 转: Linux --- Supervisor的作用与配置

    supervisor管理进程,是通过fork/exec的方式将这些被管理的进程当作supervisor的子进程来启动,所以我们只需要将要管理进程的可执行文件的路径添加到supervisor的配置文件中 ...

  2. 天蝎第一季/全集Scorpion迅雷下载

    英文译名 Scorpion (第1季) (2014-秋季播出)CBS.本季看点:<天蝎>双名蝎子故事描述一个高深莫测的计算机专家和一群同样具备天才头脑的国际计算机黑客共同组建全球防御网络, ...

  3. Eclipse断点调试(DBG)Android应用

    1.添加断点 双击左侧边框便可添加断点,右击也能添加断点. 2.进入调试模式 点击虫子,然后选择工程运行,快捷键为单击F11 ,如果是正常运行就是Ctrl+F11 3.单步调试+跳到下一个断点 运行到 ...

  4. HipHop PHP & HHVM资料收集

    百度百科 HipHop PHP实战(详解web运行模式) 百度 PHP7和HHVM的性能之争

  5. 总是容易忘记:enum、int、string之间的快速转换

    public enum Color { Red=, Green= } (1)Enum转换为String Color.Read.ToString() Convert.ToString(Color.Gre ...

  6. iOS:自定义导航栏,随着tableView滚动显示和隐藏

    自定义导航栏,随着tableView滚动显示和隐藏 一.介绍 自定义导航栏是APP中很常用的一个功能,通过自定义可以灵活的实现动画隐藏和显示效果.虽然处理系统的导航栏也可以实现,但是这个是有弊端的,因 ...

  7. Swift3.0:Get/Post同步和异步请求

    一.介绍 Get和Post区别: Get是从服务器上获取数据,Post是向服务器发送数据. 对于Get方式,服务端用Request.QueryString获取变量的值,对于Post方式,服务端用Req ...

  8. win32多线程-新版本MtVerify.h

    api调用错误诊断宏,对GetLastError()函数的封装,并解析错误 从网上找的版本并进行了部分修改 /* * MtVerify.h * * The function PrintError() ...

  9. Spring boot Unable to start embedded Tomcat报错 java.lang.NoSuchMethodError: javax.servlet.ServletContext.getVirtualServerName()

    Spring boot Unable to start embedded Tomcat 报错 java.lang.NoSuchMethodError: javax.servlet.ServletCon ...

  10. Pearson(皮尔逊)相关系数

    Pearson(皮尔逊)相关系数:也叫pearson积差相关系数.衡量两个连续变量之间的线性相关程度. 当两个变量都是正态连续变量,而且两者之间呈线性关系时,表现这两个变量之间相关程度用积差相关系数, ...