www.pudn.com > src.rar > submuxDlg.cpp
// submuxDlg.cpp : implementation file // #include "stdafx.h" #include "submux.h" #include "submuxDlg.h" #include#include #include "../DirectVobSub/DirectVobSubUIDs.h" #include "OggDS.h" #include #include #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CAboutDlg dialog used for App About class CAboutDlg : public CDialog { public: CAboutDlg(); // Dialog Data //{{AFX_DATA(CAboutDlg) enum { IDD = IDD_ABOUTBOX }; //}}AFX_DATA // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CAboutDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL // Implementation protected: //{{AFX_MSG(CAboutDlg) afx_msg void OnButton1(); afx_msg void OnButton2(); //}}AFX_MSG DECLARE_MESSAGE_MAP() }; CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) { //{{AFX_DATA_INIT(CAboutDlg) //}}AFX_DATA_INIT } void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CAboutDlg) //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) //{{AFX_MSG_MAP(CAboutDlg) ON_BN_CLICKED(IDC_BUTTON1, OnButton1) ON_BN_CLICKED(IDC_BUTTON2, OnButton2) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CSubmuxDlg dialog static HANDLE hThread = 0, hBreakEvent = 0; CSubmuxDlg::CSubmuxDlg(CWnd* pParent /*=NULL*/) : CDialog(CSubmuxDlg::IDD, pParent) , m_iInfo(0) , m_strInfo(_T("")) { //{{AFX_DATA_INIT(CSubmuxDlg) //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); hBreakEvent = CreateEvent(0, FALSE, FALSE, 0); } CSubmuxDlg::~CSubmuxDlg() { CloseHandle(hBreakEvent); } void CSubmuxDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CSubmuxDlg) DDX_Control(pDX, IDC_LIST1, m_list); DDX_Control(pDX, IDC_BUTTON4, m_mux); DDX_Control(pDX, IDOK, m_exit); DDX_CBIndex(pDX, IDC_COMBO1, m_iInfo); DDX_Text(pDX, IDC_EDIT1, m_strInfo); //}}AFX_DATA_MAP DDX_Control(pDX, IDC_COMBO1, m_InfoCtrl); } BEGIN_MESSAGE_MAP(CSubmuxDlg, CDialog) //{{AFX_MSG_MAP(CSubmuxDlg) ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_BUTTON4, OnMUX) ON_WM_DESTROY() ON_WM_DROPFILES() ON_WM_CONTEXTMENU() ON_COMMAND(ID_ADDTOLIST, OnAddToList) ON_COMMAND(ID_REMOVEFROMLIST, OnRemoveFromList) ON_BN_CLICKED(IDC_BUTTON1, OnButton1) ON_CBN_SELCHANGE(IDC_COMBO1, OnCbnSelchangeCombo1) ON_EN_CHANGE(IDC_EDIT1, OnEnChangeEdit1) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CSubmuxDlg message handlers BOOL CSubmuxDlg::OnInitDialog() { CDialog::OnInitDialog(); // Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon m_InfoCtrl.AddString(_T("Title")); m_InfoCtrl.AddString(_T("Subject")); m_InfoCtrl.AddString(_T("Artist")); m_InfoCtrl.AddString(_T("Copyright")); m_InfoCtrl.AddString(_T("Comment")); m_InfoCtrl.SetCurSel(0); m_InfoFields.SetSize(m_InfoCtrl.GetCount()); return TRUE; // return TRUE unless you set the focus to a control } void CSubmuxDlg::OnCbnSelchangeCombo1() { UpdateData(); m_strInfo = m_InfoFields[m_iInfo]; UpdateData(FALSE); } void CSubmuxDlg::OnEnChangeEdit1() { UpdateData(); m_InfoFields[m_iInfo] = m_strInfo; } // 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 CSubmuxDlg::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(); } } // The system calls this to obtain the cursor to display while the user drags // the minimized window. HCURSOR CSubmuxDlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } HRESULT AddToRot(IUnknown* pUnkGraph, DWORD* pdwRegister) { CComPtr pROT; if(FAILED(GetRunningObjectTable(0, &pROT))) return E_FAIL; WCHAR wsz[256]; wsprintfW(wsz, L"FilterGraph %08p pid %08x", (DWORD_PTR)pUnkGraph, GetCurrentProcessId()); HRESULT hr; CComPtr pMoniker; if(SUCCEEDED(hr = CreateItemMoniker(L"!", wsz, &pMoniker))) hr = pROT->Register(0, pUnkGraph, pMoniker, pdwRegister); return hr; } void RemoveFromRot(DWORD pdwRegister) { CComPtr pROT; if(SUCCEEDED(GetRunningObjectTable(0, &pROT))) pROT->Revoke(pdwRegister); } HRESULT LoadFileSourceFilter(WCHAR* fn, CComPtr & pRet, const GUID clsid, IBaseFilter* pDst, ICaptureGraphBuilder* pBuild, IGraphBuilder* pGraph) { HRESULT hr; CComPtr pSrc; if(FAILED(hr = pSrc.CoCreateInstance(clsid))) return(E_FAIL); CComQIPtr pFileSrc = pSrc; if(!pFileSrc || FAILED(hr = pFileSrc->Load(fn, NULL))) return(E_FAIL); // if(IsEqualGUID(clsid, CLSID_AVIDoc)) { GUID mp3 = {0x00000055,0x0000,0x0010,{0x80,0x00,0x00,0xAA,0x00,0x38,0x9B,0x71}}; GUID wma = {0x00000161,0x0000,0x0010,{0x80,0x00,0x00,0xAA,0x00,0x38,0x9B,0x71}}; bool fFail = true; CComPtr pEnumPins; if(SUCCEEDED(pSrc->EnumPins(&pEnumPins))) { for(CComPtr pPin; S_OK == pEnumPins->Next(1, &pPin, NULL); pPin = NULL) { CComPtr pEnumMediaTypes; if(SUCCEEDED(pPin->EnumMediaTypes(&pEnumMediaTypes))) { for(AM_MEDIA_TYPE* pAMMT; S_OK == pEnumMediaTypes->Next(1, &pAMMT, NULL); DeleteMediaType(pAMMT)) { if(pAMMT->majortype == MEDIATYPE_Audio && pAMMT->subtype == mp3) fFail = false; } } } } if(fFail) return E_FAIL; } // if(FAILED(hr = pGraph->AddFilter(pSrc, fn)) && hr != VFW_S_DUPLICATE_NAME) return(E_FAIL); if(FAILED(hr = pBuild->RenderStream(NULL, pSrc, NULL, pDst))) return(E_FAIL); while(SUCCEEDED(pBuild->RenderStream(NULL, pSrc, NULL, pDst))); // render all remaining pins pRet = pSrc; return(S_OK); } static void OGMEnableAllStreams(bool fEnable) { CComPtr pOS; pOS.CoCreateInstance(CLSID_OggSplitter); if(pOS) { CComVariant oneorzero(fEnable?1:0); CComQIPtr (pOS)->Write(CComBSTR("AlwaysEnableAllStreams"), &oneorzero); } } static void ShowPPages(CComPtr pBF, HWND hwnd) { CComQIPtr pSpecify = pBF; if(pSpecify) { CAUUID caGUID; caGUID.pElems = NULL; if(SUCCEEDED(pSpecify->GetPages(&caGUID))) { IUnknown* lpUnk = NULL; pBF.QueryInterface(&lpUnk); OleCreatePropertyFrame( hwnd, 0, 0, L"Muxer Settings", 1, &lpUnk, caGUID.cElems, caGUID.pElems, 0, 0, NULL); lpUnk->Release(); if(caGUID.pElems) CoTaskMemFree(caGUID.pElems); } } } static DWORD __stdcall ThreadProc(LPVOID pParam) { CSubmuxDlg* dlg = (CSubmuxDlg*)pParam; WCHAR infile[MAX_PATH], outfile[MAX_PATH]; #ifdef UNICODE wcscpy(outfile, dlg->m_out); #else mbstowcs(outfile, dlg->m_out, dlg->m_out.GetLength()+1); #endif int type = dlg->m_type; if(FAILED(CoInitialize(NULL))) return((UINT)(hThread = 0)); OGMEnableAllStreams(true); { CComPtr pBuild; CComPtr pGraph; CComPtr pSrc[100]; CComPtr pMux; CComQIPtr pControl; CComQIPtr pSeek; CComQIPtr pEvent; #ifdef _DEBUG DWORD dwRegister = 0; #endif int i = 0, j = 1; do { if(type != 1 && type != 2) break; const GUID* muxguid = type == 1 ? &MEDIASUBTYPE_Avi : &CLSID_OggMux; if(FAILED(pBuild.CoCreateInstance(CLSID_CaptureGraphBuilder))) break; if(FAILED(pBuild->SetOutputFileName(muxguid, outfile, &pMux, NULL))) break; if(FAILED(pBuild->GetFiltergraph(&pGraph))) break; #ifdef _DEBUG AddToRot(pGraph, &dwRegister); #endif GUID CLSID_WMASource = {0x6b6d0800, 0x9ada, 0x11d0, {0xa5, 0x20, 0x00, 0xa0, 0xd1, 0x01, 0x29, 0xc0}}; for(i = 0, j = min(100, dlg->m_list.GetCount()); i < j; i++) { CString fn; dlg->m_list.GetText(i, fn); #ifdef UNICODE wcscpy(infile, fn); #else mbstowcs(infile, fn, fn.GetLength()+1); #endif if(FAILED(LoadFileSourceFilter(infile, pSrc[i], CLSID_SubtitleSource, pMux, pBuild, pGraph)) && FAILED(LoadFileSourceFilter(infile, pSrc[i], CLSID_AVIDoc, pMux, pBuild, pGraph)) && FAILED(LoadFileSourceFilter(infile, pSrc[i], CLSID_WMASource, pMux, pBuild, pGraph)) && FAILED(LoadFileSourceFilter(infile, pSrc[i], CLSID_AsyncReader, pMux, pBuild, pGraph))) { CString msg; msg.Format(_T("Couldn't load %s"), fn); AfxMessageBox(msg, MB_OK); break; } } if(i < j) break; pControl = pGraph; pEvent = pGraph; pSeek = pMux; if(!pControl || !pEvent) break; // if(type == 1) { CComQIPtr pCfgIL = pMux; if(pCfgIL) { REFERENCE_TIME Interleave = 1000 * 10000, Preroll = 750 * 10000; pCfgIL->put_Interleaving(&Interleave, &Preroll); pCfgIL->put_Mode(INTERLEAVE_FULL); } CComQIPtr pPersist = pMux; if(pPersist) { CComPtr pBag; pBag.CoCreateInstance(CLSID_MediaPropertyBag); WCHAR* infotype[] = {L"INFO/INAM", L"INFO/ISBJ", L"INFO/IART", L"INFO/ICOP", L"INFO/ICMT"}; for(int i = 0; i < 5; i++) { CString str = dlg->m_InfoFields[i]; if(!str.IsEmpty()) { CComVariant val = str; pBag->Write(infotype[i], &val); } } pPersist->Load(pBag, NULL); } } ShowPPages(pMux, dlg->m_hWnd); dlg->m_mux.EnableWindow(FALSE); dlg->m_list.EnableWindow(FALSE); dlg->m_exit.SetWindowText(ResStr(IDS_BREAK)); dlg->m_mux.SetWindowText(ResStr(IDS_PLEASEWAIT)); HRESULT hr2 = pControl->Run(); long evCode; REFERENCE_TIME rtTotal, rtNow = 0; if(pSeek) // IMediaSeeking is supported { CString progress; GUID tf; //hr2 = pSeek->SetTimeFormat(&TIME_FORMAT_BYTE); pSeek->GetTimeFormat(&tf); HRESULT hr = pSeek->GetDuration(&rtTotal); while((pEvent->WaitForCompletion(1000, &evCode)) != S_OK && WaitForSingleObject(hBreakEvent, 0) != WAIT_OBJECT_0) { pSeek->GetCurrentPosition(&rtNow); if(hr == S_OK) { progress.Format(_T("%.2f%%\n"), 1.0 * (rtNow * 100) / rtTotal); } else if(tf == TIME_FORMAT_BYTE) { progress.Format(_T("%.2fKB\n"), 1.0 * rtNow / 1024); } else if(tf == TIME_FORMAT_MEDIA_TIME) { rtNow /= 10000; progress.Format(_T("%02d:%02d:%02d.%03d\n"), int(rtNow/3600000)%60, int(rtNow/60000)%60, int(rtNow/1000)%60, int(rtNow)%1000); } else continue; dlg->m_mux.SetWindowText(progress); } } else // Cannot update the progress { while((pEvent->WaitForCompletion(1000, &evCode)) != S_OK && WaitForSingleObject(hBreakEvent, 0) != WAIT_OBJECT_0) { } } pControl->Stop(); } while(false); #ifdef _DEBUG if(dwRegister) RemoveFromRot(dwRegister); #endif } OGMEnableAllStreams(false); CoUninitialize(); dlg->m_mux.SetWindowText(ResStr(IDS_MUX)); dlg->m_mux.EnableWindow(); dlg->m_list.EnableWindow(); dlg->m_exit.SetWindowText(ResStr(IDS_EXIT)); return((DWORD)(hThread = 0)); } void CSubmuxDlg::OnMUX() { UpdateData(); if(m_list.GetCount() <= 0) return; static TCHAR BASED_CODE szFilter[] = _T("Avi files (*.avi)|*.avi|Ogm files (*.ogm)|*.ogm||"); CFileDialog fd(FALSE, NULL, NULL, OFN_EXPLORER | OFN_ENABLESIZING | OFN_HIDEREADONLY, szFilter, this); if(fd.DoModal() != IDOK) return; m_out = fd.GetPathName(); m_type = fd.m_ofn.nFilterIndex; if(m_type == 1 && m_out.Mid(m_out.ReverseFind('.')+1).CompareNoCase(_T("avi"))) m_out += _T(".avi"); if(m_type == 2 && m_out.Mid(m_out.ReverseFind('.')+1).CompareNoCase(_T("ogm"))) m_out += _T(".ogm"); ResetEvent(hBreakEvent); DWORD tid; hThread = CreateThread(NULL, 0, ThreadProc, this, 0, &tid); } void CSubmuxDlg::OnDestroy() { if(hThread) { ASSERT(0); SetEvent(hBreakEvent); } CDialog::OnDestroy(); } void CSubmuxDlg::OnOK() { if(hThread) { SetEvent(hBreakEvent); } else { CDialog::OnOK(); } } void CSubmuxDlg::OnCancel() { if(!hThread) { CDialog::OnCancel(); } } void CSubmuxDlg::OnDropFiles(HDROP hDropInfo) { TCHAR buff[MAX_PATH]; for(int i = 0, j = DragQueryFile(hDropInfo, 0xffffffff, 0, 0); i < j; i++) { DragQueryFile(hDropInfo, i, buff, MAX_PATH); CString fn(buff); CString ext = fn.Mid(fn.ReverseFind('.')+1); ext.MakeLower(); CString str; if(!(ext == _T("avi") || ext == _T("ogm") || ext == _T("wav") || ext == _T("wma") || ext == _T("ogg") || ext == _T("srt") || ext == _T("ssa"))) continue; for(int k = 0, l = m_list.GetCount(); k < l; k++) { m_list.GetText(k, str); str = str.Mid(str.ReverseFind('.')+1); str.MakeLower(); if((ext == _T("avi") || ext == _T("ogm")) && (str == _T("wav") || str == _T("wma") || str == _T("ogg") || str == _T("ssa") || str == _T("srt"))) { break; } if((ext == _T("wav") || ext == _T("wma") || ext == _T("wma")) && (str == _T("ssa") || str == _T("srt"))) { break; } } m_list.InsertString(k, fn); } } void CSubmuxDlg::OnContextMenu(CWnd* pWnd, CPoint point) { if(pWnd->GetDlgCtrlID() == IDC_LIST1) { INT i; i = m_list.GetSelItems(1, &i); CMenu menu; if(menu.LoadMenu(IDR_MENU1)) { CMenu* pPopup = menu.GetSubMenu(0); ASSERT(pPopup != NULL); pPopup->EnableMenuItem(ID_REMOVEFROMLIST, MF_BYCOMMAND | (i <= 0 ? (MF_DISABLED|MF_GRAYED) : MF_ENABLED)); pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, AfxGetMainWnd()); // use main window for cmds } } } void CSubmuxDlg::OnAddToList() { static TCHAR BASED_CODE szFilter[] = _T("Supported formats (*.avi;*.ogm;*.wav;*.wma;*.ogg;*.ssa;*.srt)|*.avi;*.ogm;*.wav;*.wma;*.ogg;*.ssa;*.srt||"); CFileDialog fd(TRUE, NULL, NULL, OFN_EXPLORER | OFN_ENABLESIZING | OFN_HIDEREADONLY | OFN_ALLOWMULTISELECT, szFilter, this); if(fd.DoModal() != IDOK) return; POSITION pos = fd.GetStartPosition(); while(pos) { CString fn = fd.GetNextPathName(pos); CString ext = fn.Mid(fn.ReverseFind('.')+1); ext.MakeLower(); CString str; if(!(ext == _T("avi") || ext == _T("ogm") || ext == _T("wav") || ext == _T("wma") || ext == _T("ogg") || ext == _T("srt") || ext == _T("ssa"))) continue; for(int k = 0, l = m_list.GetCount(); k < l; k++) { m_list.GetText(k, str); str = str.Mid(str.ReverseFind('.')+1); str.MakeLower(); if((ext == _T("avi") || ext == _T("ogm")) && (str == _T("wav") || str == _T("wma") || str == _T("ogg") || str == _T("ssa") || str == _T("srt"))) { break; } if((ext == _T("wav") || ext == _T("wma") || ext == _T("ogg")) && (str == _T("ssa") || str == _T("srt"))) { break; } } m_list.InsertString(k, fn); } } void CSubmuxDlg::OnRemoveFromList() { INT sel[100]; for(int i = m_list.GetSelItems(100, sel)-1; i >= 0; i--) { m_list.DeleteString(sel[i]); } } void CAboutDlg::OnButton1() { ShellExecute(m_hWnd, _T("open"), _T("http://vobsub.edensrising.com"), NULL, NULL, SW_SHOWNORMAL); } void CAboutDlg::OnButton2() { ShellExecute(m_hWnd, _T("open"), _T("mailto:gabest@freemail.hu"), NULL, NULL, SW_SHOWNORMAL); } void CSubmuxDlg::OnButton1() { CAboutDlg dlgAbout; dlgAbout.DoModal(); }