小工具,用于解析私有流和定位脏数据。
int CtestmfcDlg::parser()
{
m_loc = 0;
m_index = 0;
m_count = 0;
m_extendLen = 0;
m_frameLen = 0;
m_Len = 0;
m_parserLen = 0;
m_lastLoc = 0;
long fileSize;
CString str1;
GetDlgItem(IDC_EDIT1)->GetWindowText(str1);
string str2 = CT2A(str1);
fp = fopen(str2.c_str(), "rb");
if (fp == NULL)
{
TRACE("open file failed!\n");
return 0;
}
fseek(fp, 0L, SEEK_END);
fileSize = ftell(fp);
rewind(fp);
unsigned char *dataPtr= (unsigned char *)malloc((fileSize+1)*sizeof(unsigned char));
fread(dataPtr, fileSize,1, fp);
ParseData(dataPtr,fileSize);
fclose(fp);
return 0;
}
int CtestmfcDlg::ParseData(unsigned char *data, int len)
{
if (data == NULL || len <= 0)
return -1;
m_code = 0xffffffff;
m_falseLen = 0;
m_bufptr = data;
rest = len;
m_list.DeleteAllItems();
while (rest > 0)
{
m_code = m_code << 8 | *m_bufptr++;
rest--;
if (CheckSign(m_code) == true)
{
CString count_str,location_str,index_str,falseLen_str,m_Len_str,m_parserLen_str,m_frameRate_str,m_wh_str;
count_str.Format(_T("%d"),m_count);
location_str.Format(_T("%x"),m_loc-3);
index_str.Format(_T("%x"),m_index);
m_list.InsertItem(m_count,L"");
m_list.SetItemText(m_count, 0, count_str);
m_list.SetItemText(m_count, 1, location_str);
m_list.SetItemText(m_count, 2, index_str);
m_falseLen = (m_loc-3) - m_index;
m_index += m_falseLen;
if (ParseOneFrame() == false)
{
return 0;
}
if (m_falseLen<0)
{
m_falseLen = m_parserLen+m_falseLen;
}
if (m_falseLen!=0)
{
falseLen_str.Format(_T("%d"),m_falseLen);
m_list.SetItemText(m_count, 5, falseLen_str);
}
m_Len = m_loc-m_lastLoc;
m_Len_str.Format(_T("%d"),m_Len);
m_list.SetItemText(m_count-1, 3, m_Len_str);
m_parserLen_str.Format(_T("%d"),m_parserLen);
m_list.SetItemText(m_count, 4, m_parserLen_str);
m_list.SetItemText(m_count, 6, type(m_nType));
if (m_nType==1)
{
m_list.SetItemText(m_count, 7, subType(m_nSubType));
m_frameRate_str.Format(_T("%d"),m_nFrameRate);
m_wh_str.Format(_T("%d*%d"),m_nWidth,m_nHeight);
m_list.SetItemText(m_count, 8, m_frameRate_str);
m_list.SetItemText(m_count, 9, m_wh_str);
}
m_count++;
m_lastLoc = m_loc;
if (m_falseLen>0)
{
m_list.SetItemColor(m_count-1,RGB(240,128,128));
}
}
m_loc++;
}
if (rest==0)//最后一帧
{
m_Len = m_loc-m_lastLoc+3;
CString m_Len_str;
m_Len_str.Format(_T("%d"),m_Len);
m_list.SetItemText(m_count-1, 3, m_Len_str);
if (m_Len!=m_parserLen||m_falseLen>0)
{
m_list.SetItemColor(m_count-1,RGB(240,128,128));
}
}
return 0;
}
bool CtestmfcDlg::CheckSign(const unsigned int& Code)
{
if (Code == 0x01ED || Code == 0x01EC || Code == 0x01EA)
{
return true;
}
return false;
}
bool CtestmfcDlg::ParseOneFrame()
{
if (rest < 20)
return false;
if (m_code == 0x01ED)
{
m_nType = DH_FRAME_TYPE_VIDEO;
m_nSubType = DH_FRAME_TYPE_VIDEO_I_FRAME;
}
else if (m_code == 0x01EC)
{
m_nType = DH_FRAME_TYPE_VIDEO;
m_nSubType = DH_FRAME_TYPE_VIDEO_P_FRAME;
}
else if (m_code == 0x01EA)
{
m_nType = DH_FRAME_TYPE_AUDIO;
}
m_nFrameRate = m_bufptr[2] &0x1F;
m_nWidth = m_bufptr[4] | m_bufptr[5]<<8;
m_nHeight = m_bufptr[6] | m_bufptr[7]<<8;
m_extendLen = (m_bufptr[14 ] & 0xFF) | ((m_bufptr[15 ] << 8 ) & 0xFF00);
unsigned char* frameLenPtr = m_bufptr + m_extendLen;
m_frameLen = frameLenPtr[19]<<24|frameLenPtr[18]<<16|frameLenPtr[17]<<8|frameLenPtr[16];
m_parserLen = m_frameLen + 24 + m_extendLen;
m_index += m_parserLen;
return true;
}
网友评论