// EncapDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Encap.h"
#include "EncapDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CEncapDlg dialog
CEncapDlg::CEncapDlg(CWnd* pParent /*=NULL*/)
: CDialog(CEncapDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CEncapDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_pIn = NULL;
m_dwBytes = 0;
m_nEncap = 0;
m_bIncLength = TRUE;
}
void CEncapDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CEncapDlg)
DDX_Control(pDX, IDC_SRC_ENCAP, m_SrcEncap);
DDX_Control(pDX, IDC_ENCAPS, m_Encaps);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CEncapDlg, CDialog)
//{{AFX_MSG_MAP(CEncapDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_SAVE, OnSave)
ON_WM_TIMER()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CEncapDlg message handlers
BOOL CEncapDlg::OnInitDialog()
{
CDialog::OnInitDialog();
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
int nItem;
nItem = m_Encaps.AddString(_T("headless"));
m_Encaps.SetItemData(nItem,0);
nItem = m_Encaps.AddString(_T("000001 started"));
m_Encaps.SetItemData(nItem,1);
nItem = m_Encaps.AddString(_T("16-bit lengthed"));
m_Encaps.SetItemData(nItem,2);
nItem = m_Encaps.AddString(_T("32-bit lengthed"));
m_Encaps.SetItemData(nItem,3);
nItem = m_Encaps.AddString(_T("invert first two bytes for each NAL"));
m_Encaps.SetItemData(nItem,4);
m_Encaps.SelectString(-1,_T("16-bit lengthed"));
return TRUE; // return TRUE unless you set the focus to a control
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CEncapDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
HCURSOR CEncapDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CEncapDlg::OnCancel()
{
if(m_pIn!=NULL)
{
UnmapViewOfFile(m_pIn);
}
CDialog::OnCancel();
}
void CEncapDlg::OnOK()
{
if(m_pIn!=NULL)
{
UnmapViewOfFile(m_pIn);
m_pIn = NULL;
m_nEncap = 0;
SetDlgItemText(IDC_SRC_ENCAP,_T(""));
GetDlgItem(IDC_SAVE)->EnableWindow(FALSE);
}
LPCTSTR szFilter = _T("H264 bitstream(*.264;*.bin)|*.264;*.bin|All files(*.*)|*.*");
CFileDialog dlg(TRUE,_T("264"),NULL,OFN_FILEMUSTEXIST,szFilter,this);
if(dlg.DoModal()!=IDOK)
{
return;
}
CString strFilePath = dlg.GetPathName();
HANDLE f = CreateFile(strFilePath,GENERIC_READ,FILE_SHARE_READ,0,OPEN_EXISTING,0,0);
if(f==INVALID_HANDLE_VALUE)
{
MessageBox(_T("Error: can't open input file"),_T("H264 Encapulation Converter"),MB_OK|MB_ICONERROR);
return;
}
m_dwBytes = GetFileSize(f,0);
HANDLE m = CreateFileMapping(f,0,PAGE_READONLY,0,0,0);
CloseHandle(f);
if(!m)
{
MessageBox(_T("Error: out of memory?"),_T("H264 Encapulation Converter"),MB_OK|MB_ICONERROR);
return;
}
m_pIn = (BYTE *)MapViewOfFile(m,FILE_MAP_READ,0,0,0);
CloseHandle(m);
if(!m_pIn)
{
MessageBox(_T("Error: out of memory?"),_T("H264 Encapulation Converter"),MB_OK|MB_ICONERROR);
return;
}
GetDlgItem(IDC_SAVE)->EnableWindow(TRUE);
BYTE *p = m_pIn;
DWORD dw = (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
if(dw==0x1)
{
m_nEncap = 1;
SetDlgItemText(IDC_SRC_ENCAP,_T("000001 started"));
}
else
{
for(;p-m_pIn<m_dwBytes;)
{
dw = (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
if((p-m_pIn)+dw+4>m_dwBytes)
{
break;
}
p += dw + 4;
}
if(p-m_pIn==m_dwBytes)
{
m_nEncap = 3;
SetDlgItemText(IDC_SRC_ENCAP,_T("32-bit lengthed"));
}
else
{
for(p=m_pIn;p-m_pIn<m_dwBytes;)
{
dw = (p[0]<<8) | p[1];
if((p-m_pIn)+dw+2>m_dwBytes)
{
break;
}
p += dw + 2;
}
if(p-m_pIn==m_dwBytes)
{
m_nEncap = 2;
SetDlgItemText(IDC_SRC_ENCAP,_T("16-bit lengthed"));
}
else
{
m_nEncap = 0;
SetDlgItemText(IDC_SRC_ENCAP,_T("headless"));
}
}
}
SetDlgItemText(IDC_TIP,_T("Opened successfully!"));
SetTimer(100,10000,NULL);
}
void CEncapDlg::OnSave()
{
int n = m_Encaps.GetCurSel();
if(n<0)
{
return;
}
n = m_Encaps.GetItemData(n);
if(!m_nEncap)
{
if(n!=4)
{
MessageBox(_T("Sorry: source format unsupported"),_T("H264 Encapulation Converter"),MB_OK|MB_ICONINFORMATION);
}
}
m_bIncLength = IsDlgButtonChecked(IDC_INCLUDE_LENGTH_FILED);
if(n==m_nEncap)
{
MessageBox(_T("Hint: no conversion needed"),_T("H264 Encapulation Converter"),MB_OK|MB_ICONINFORMATION);
return;
}
LPCTSTR szFilter = _T("H264 bitstream(*.264;*.bin)|*.264;*.bin|All files(*.*)|*.*");
CFileDialog dlg(FALSE,_T("264"),NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,szFilter,this);
if(dlg.DoModal()!=IDOK)
{
return;
}
CString strFilePath = dlg.GetPathName();
HANDLE f = CreateFile(strFilePath,GENERIC_WRITE,FILE_SHARE_READ,0,CREATE_ALWAYS,0,0);
if(f==INVALID_HANDLE_VALUE)
{
MessageBox(_T("Error: can't open output file"),_T("H264 Encapulation Converter"),MB_OK|MB_ICONERROR);
return;
}
switch(m_nEncap)
{
case 0:
InvertTwoBytes(f);
break;
case 1:
switch(n)
{
case 0:
ConvertStartCodeToHeadless(f);
break;
case 2:
ConvertStartCodeToLength16(f);
break;
case 3:
ConvertStartCodeToLength32(f);
break;
}
break;
case 2:
switch(n)
{
case 0:
ConvertLength16ToHeadless(f);
break;
case 1:
ConvertLength16ToStartCode(f);
break;
case 3:
ConvertLength16ToLength32(f);
break;
}
break;
case 3:
switch(n)
{
case 0:
ConvertLength32ToHeadless(f);
break;
case 1:
ConvertLength32ToStartCode(f);
break;
case 2:
ConvertLength32ToLength16(f);
break;
}
break;
}
SetEndOfFile(f);
CloseHandle(f);
}
void CEncapDlg::ConvertStartCodeToHeadless(HANDLE f)
{
BYTE *p,*t;
DWORD dw,n;
for(t=p=m_pIn+4;p-m_pIn<m_dwBytes;)
{
for(dw=0;dw!=0x1 && p-m_pIn<m_dwBytes;++p)
{
dw = (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
}
if(p-m_pIn>=m_dwBytes)
{
MessageBox(_T("Error: invalid input file"),_T("H264 Encapulation Converter"),MB_OK|MB_ICONERROR);
SetDlgItemText(IDC_TIP,_T("failed to convert"));
SetTimer(100,10000,NULL);
return;
}
n = 0;
if(!WriteFile(f,t,p-t,&n,0))
{
MessageBox(_T("Error: can't write output file"),_T("H264 Encapulation Converter"),MB_OK|MB_ICONERROR);
SetDlgItemText(IDC_TIP,_T("failed to convert"));
SetTimer(100,10000,NULL);
return;
}
p += 4;
t = p;
}
SetDlgItemText(IDC_TIP,_T("convert finished"));
SetTimer(100,10000,NULL);
}
void CEncapDlg::ConvertStartCodeToLength16(HANDLE f)
{
BYTE *p,*t;
DWORD dw,n;
WORD len;
for(t=p=m_pIn+4;p-m_pIn<m_dwBytes;)
{
for(dw=0;p-m_pIn<m_dwBytes;++p)
{
dw = (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
if(dw==0x1)
{
break;
}
}
if(p-m_pIn>m_dwBytes)
{
MessageBox(_T("Error: invalid input file"),_T("H264 Encapulation Converter"),MB_OK|MB_ICONERROR);
SetDlgItemText(IDC_TIP,_T("failed to conve
- 1
- 2
- 3
- 4
- 5
- 6
前往页