// // Written by Will Braynen (in 2001) // #include "stdafx.h" #include #include "JavuPlayerUtil.h" #include HRESULT AddToRot( IUnknown *pUnkGraph, DWORD *pdwRegister ) { IMoniker * pMoniker; IRunningObjectTable *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 = CreateItemMoniker(L"!", wsz, &pMoniker); if (SUCCEEDED(hr)) { hr = pROT->Register(0, pUnkGraph, pMoniker, pdwRegister); pMoniker->Release(); } pROT->Release(); return hr; } void RemoveFromRot( DWORD pdwRegister ) { IRunningObjectTable *pROT; if (SUCCEEDED(GetRunningObjectTable(0, &pROT))) { pROT->Revoke(pdwRegister); pROT->Release(); } } HRESULT IsValidFile( LPCWSTR lpszFilename ) { if ( ! lpszFilename) return E_INVALIDARG; DWORD dwAttr = GetFileAttributes(lpszFilename); return ((dwAttr != (DWORD) -1) && (! (dwAttr & FILE_ATTRIBUTE_DIRECTORY))); } char * Wide2Ascii( LPCWSTR lpszWideString, char *pAsciiString ) { unsigned int i; for (i = 0; i < wcslen (lpszWideString); i++) { pAsciiString[i] = (char) lpszWideString[i]; } pAsciiString[i] = '\0'; return pAsciiString; } LPWSTR Ascii2Wide( const char *pAsciiString, LPWSTR lpszWideString ) { unsigned int i; for (i = 0; i < strlen (pAsciiString); i++) { lpszWideString[i] = 0; lpszWideString[i] = (TCHAR) pAsciiString[i]; } return lpszWideString; } double GetXMLFrameRate( LPCWSTR lpszFilename ) { HANDLE hFile = ::CreateFile (lpszFilename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL); if (hFile != INVALID_HANDLE_VALUE) { // Attach a CFile object to it. CFile file ((int) hFile); file.SetFilePath (lpszFilename); const int BUFLEN = 10240; char szText[BUFLEN]; DWORD dwSize = file.GetLength(); if (dwSize < BUFLEN) { file.Read (szText, (UINT) dwSize); file.Close(); } else { return -1; } // find "framerate" char *p = strstr (szText, "framerate"); if ( ! p) p = strstr (szText, "FRAMERATE"); if ( ! p) p = strstr (szText, "Framerate"); if ( ! p) p = strstr (szText, "FrameRate"); if ( ! p) return -2; else { // once found "framerate", extract the XXX from "framerate="XXX"; XXX might be 29.97 or just 30, or 5 (in other words anything, as long as it's inside the quotes) do { p++; } while (*p != '\"'); p++; // now p points to the number // extract the number; p points to 29.97"> ... return atof (p); } } else { return -3; } return -4; } RECT * GetCoords( HWND hParentWindow, int nIDDlgItem, RECT *pRect ) { RECT rectParentWindow; ::GetWindowRect (hParentWindow, &rectParentWindow); ::GetWindowRect(::GetDlgItem (hParentWindow, nIDDlgItem), pRect); pRect->left -= rectParentWindow.left; pRect->right -= rectParentWindow.left; pRect->bottom -= rectParentWindow.top; pRect->top -= rectParentWindow.top; pRect->left--; pRect->right--; return pRect; } long GetDlgItemLong(HWND hDlg, int nID, LPTSTR lpString, int nMaxCount) { ::GetWindowText(::GetDlgItem (hDlg, nID), lpString, nMaxCount); TCHAR *c = _T('\0'); return wcstol (lpString, &c, 10); } ////////////////////////////////////////////////////////////////////// // // Converts lDuration ("hhmmssff") -> strDuration ("hh:mm:ss:ff") // // For example, lDuration = 02193498, which means "02:19:34:98" in "hh:mm:ss:ff" format // HRESULT TimecodeInt2String(long lDuration, CString *strDuration) { if ( ! strDuration) return E_POINTER; int hh = (lDuration % 100000000) / 1000000; int mm = (lDuration % 1000000) / 10000; int ss = (lDuration % 10000) / 100; int ff = (lDuration % 100); strDuration->Format (_T("%02d:%02d:%02d:%02d"), hh, mm, ss, ff); return S_OK; } ///////////////////////////////////////////////////////////////////////////////// // // Converts strDuration ("hh:mm:ss:ff" or "hhmmssff") -> lDuration (hhmmssff) // // For example, if strDuration = "02:19:34:98", lDuration will be 02193498 // HRESULT TimecodeString2Int( CString strDuration, long *plDuration ) { if ( ! plDuration) return E_POINTER; if ( strDuration.IsEmpty()) return E_INVALIDARG; // "hh:mm:ss:ff" -> hhmmssff if (-1 != strDuration.Find (_T(":"), 0)) // if contains a ":" { // Get rid of the separating characters: "hh:mm:ss:ff"->"hhmmssff" strDuration.Delete (8, 1); strDuration.Delete (5, 1); strDuration.Delete (2, 1); } // Convert the string into a long integer: "hhmmssff" -> hhmmssff TCHAR *c = _T('\0'); *plDuration = wcstol (strDuration, &c, 10); return S_OK; } ////////////////////////////////////////////////////////////////////// // // Converts rtDuration (100ns) -> strDuration ("hh:mm:ss:ff") // HRESULT Time2Timecode(REFERENCE_TIME rtDuration, CString *strDuration, const double dFrameRate, bool bDropFrameFormat) { if (bDropFrameFormat) return E_NOTIMPL; // bDropFrameFormat unsupported for now if ( ! strDuration) return E_POINTER; if (rtDuration < 0) return E_INVALIDARG; if (dFrameRate > 0) { const double dFrameDuration = 10000000 / dFrameRate; LONGLONG llFrames = rtDuration / (LONGLONG)dFrameDuration; int nFrames = (int)(llFrames % ROUND_UP(dFrameRate)); // here dFrameRate is rounded UP to the nearest whole number int nSeconds = (int)(llFrames / ROUND_UP(dFrameRate)); // here dFrameRate is rounded UP to the nearest whole number int nMinutes = (int)(nSeconds / 60); int nHours = (int)(nMinutes / 60); nSeconds %= 60; // went into nMinutes nMinutes %= 60; // went into nHours strDuration->Format (_T("%02d:%02d:%02d:%02d"), nHours, nMinutes, nSeconds, nFrames); } else { // the frame rate is unknown, so use "hh:mm:ss" format LONGLONG llTotalMS = rtDuration / 10000; // 100ns -> ms int nSeconds = (int)(llTotalMS / 1000); int nMinutes = (int)(nSeconds / 60); int nHours = (int)(nMinutes / 60); nSeconds %= 60; // went into nMinutes nMinutes %= 60; // went into nHours strDuration->Format (_T("%02d:%02d:%02d"), nHours, nMinutes, nSeconds); } return S_OK; } ////////////////////////////////////////////////////////////////////// // // Converts lPositionTimecode ("hhmmssff") -> prtPositionTime (100ns) // // For example, lDuration = 02193498, which means "02:19:34:98" in "hh:mm:ss:ff" format // HRESULT Timecode2Time(long lPositionTimecode, REFERENCE_TIME *prtPositionTime, const double dFrameRate, bool bDropFrameFormat) { if (bDropFrameFormat) return E_NOTIMPL; // bDropFrameFormat unsupported for now if ( ! prtPositionTime) return E_POINTER; if (lPositionTimecode < 0) return E_INVALIDARG; if (0 == dFrameRate) return E_FAIL; // to avoid division by zero const double dFrameDuration = (10000000 / dFrameRate); const LONGLONG hh = (lPositionTimecode % 100000000) / 1000000, mm = (lPositionTimecode % 1000000) / 10000, ss = (lPositionTimecode % 10000) / 100, ff = (lPositionTimecode % 100); *prtPositionTime = (LONGLONG)((double)(hh * 60 * 60 + mm * 60 + ss) * 10000000 + (ff * dFrameDuration) + (dFrameDuration / 2)); if (29.97 == dFrameRate) { *prtPositionTime = (REFERENCE_TIME)(*prtPositionTime * (30 / 29.97)); } return S_OK; }