IPinHook.cpp
上传用户:tangyu_668
上传日期:2014-02-27
资源大小:678k
文件大小:12k
源码类别:

多媒体编程

开发平台:

Visual C++

  1. /* 
  2.  * Copyright (C) 2003-2006 Gabest
  3.  * http://www.gabest.org
  4.  *
  5.  *  This Program is free software; you can redistribute it and/or modify
  6.  *  it under the terms of the GNU General Public License as published by
  7.  *  the Free Software Foundation; either version 2, or (at your option)
  8.  *  any later version.
  9.  *   
  10.  *  This Program is distributed in the hope that it will be useful,
  11.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13.  *  GNU General Public License for more details.
  14.  *   
  15.  *  You should have received a copy of the GNU General Public License
  16.  *  along with GNU Make; see the file COPYING.  If not, write to
  17.  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
  18.  *  http://www.gnu.org/copyleft/gpl.html
  19.  *
  20.  */
  21. #include "stdafx.h"
  22. #include "IPinHook.h"
  23. REFERENCE_TIME g_tSegmentStart = 0;
  24. REFERENCE_TIME g_tSampleStart = 0;
  25. static HRESULT (STDMETHODCALLTYPE * NewSegmentOrg)(IPinC * This, /* [in] */ REFERENCE_TIME tStart, /* [in] */ REFERENCE_TIME tStop, /* [in] */ double dRate) = NULL;
  26. static HRESULT STDMETHODCALLTYPE NewSegmentMine(IPinC * This, /* [in] */ REFERENCE_TIME tStart, /* [in] */ REFERENCE_TIME tStop, /* [in] */ double dRate)
  27. {
  28. g_tSegmentStart = tStart;
  29. return NewSegmentOrg(This, tStart, tStop, dRate);
  30. }
  31. static HRESULT ( STDMETHODCALLTYPE *ReceiveOrg )( IMemInputPinC * This, IMediaSample *pSample) = NULL;
  32. static HRESULT STDMETHODCALLTYPE ReceiveMine(IMemInputPinC * This, IMediaSample *pSample)
  33. {
  34. REFERENCE_TIME rtStart, rtStop;
  35. if(pSample && SUCCEEDED(pSample->GetTime(&rtStart, &rtStop)))
  36. g_tSampleStart = rtStart;
  37. return ReceiveOrg(This, pSample);
  38. }
  39. bool HookNewSegmentAndReceive(IPinC* pPinC, IMemInputPinC* pMemInputPinC)
  40. {
  41. if(!pPinC || !pMemInputPinC || (GetVersion()&0x80000000))
  42. return false;
  43. g_tSegmentStart = 0;
  44. g_tSampleStart = 0;
  45. BOOL res;
  46. DWORD flOldProtect = 0;
  47. res = VirtualProtect(pPinC->lpVtbl, sizeof(IPinC), PAGE_WRITECOPY, &flOldProtect);
  48. if(NewSegmentOrg == NULL) NewSegmentOrg = pPinC->lpVtbl->NewSegment;
  49. pPinC->lpVtbl->NewSegment = NewSegmentMine;
  50. res = VirtualProtect(pPinC->lpVtbl, sizeof(IPinC), PAGE_EXECUTE, &flOldProtect);
  51. res = VirtualProtect(pMemInputPinC->lpVtbl, sizeof(IMemInputPinC), PAGE_WRITECOPY, &flOldProtect);
  52. if(ReceiveOrg == NULL) ReceiveOrg = pMemInputPinC->lpVtbl->Receive;
  53. pMemInputPinC->lpVtbl->Receive = ReceiveMine;
  54. res = VirtualProtect(pMemInputPinC->lpVtbl, sizeof(IMemInputPinC), PAGE_EXECUTE, &flOldProtect);
  55. return true;
  56. }
  57. static HRESULT ( STDMETHODCALLTYPE *GetVideoAcceleratorGUIDsOrg )( IAMVideoAcceleratorC * This,/* [out][in] */ LPDWORD pdwNumGuidsSupported,/* [out][in] */ LPGUID pGuidsSupported) = NULL;
  58. static HRESULT ( STDMETHODCALLTYPE *GetUncompFormatsSupportedOrg )( IAMVideoAcceleratorC * This,/* [in] */ const GUID *pGuid,/* [out][in] */ LPDWORD pdwNumFormatsSupported,/* [out][in] */ LPDDPIXELFORMAT pFormatsSupported) = NULL;
  59. static HRESULT ( STDMETHODCALLTYPE *GetInternalMemInfoOrg )( IAMVideoAcceleratorC * This,/* [in] */ const GUID *pGuid,/* [in] */ const AMVAUncompDataInfo *pamvaUncompDataInfo,/* [out][in] */ LPAMVAInternalMemInfo pamvaInternalMemInfo) = NULL;
  60. static HRESULT ( STDMETHODCALLTYPE *GetCompBufferInfoOrg )( IAMVideoAcceleratorC * This,/* [in] */ const GUID *pGuid,/* [in] */ const AMVAUncompDataInfo *pamvaUncompDataInfo,/* [out][in] */ LPDWORD pdwNumTypesCompBuffers,/* [out] */ LPAMVACompBufferInfo pamvaCompBufferInfo) = NULL;
  61. static HRESULT ( STDMETHODCALLTYPE *GetInternalCompBufferInfoOrg )( IAMVideoAcceleratorC * This,/* [out][in] */ LPDWORD pdwNumTypesCompBuffers,/* [out] */ LPAMVACompBufferInfo pamvaCompBufferInfo) = NULL;
  62. static HRESULT ( STDMETHODCALLTYPE *BeginFrameOrg )( IAMVideoAcceleratorC * This,/* [in] */ const AMVABeginFrameInfo *amvaBeginFrameInfo) = NULL;        
  63. static HRESULT ( STDMETHODCALLTYPE *EndFrameOrg )( IAMVideoAcceleratorC * This,/* [in] */ const AMVAEndFrameInfo *pEndFrameInfo) = NULL;
  64. static HRESULT ( STDMETHODCALLTYPE *GetBufferOrg )( IAMVideoAcceleratorC * This,/* [in] */ DWORD dwTypeIndex,/* [in] */ DWORD dwBufferIndex,/* [in] */ BOOL bReadOnly,/* [out] */ LPVOID *ppBuffer,/* [out] */ LONG *lpStride) = NULL;
  65. static HRESULT ( STDMETHODCALLTYPE *ReleaseBufferOrg )( IAMVideoAcceleratorC * This,/* [in] */ DWORD dwTypeIndex,/* [in] */ DWORD dwBufferIndex) = NULL;
  66. static HRESULT ( STDMETHODCALLTYPE *ExecuteOrg )( IAMVideoAcceleratorC * This,/* [in] */ DWORD dwFunction,/* [in] */ LPVOID lpPrivateInputData,/* [in] */ DWORD cbPrivateInputData,/* [in] */ LPVOID lpPrivateOutputDat,/* [in] */ DWORD cbPrivateOutputData,/* [in] */ DWORD dwNumBuffers,/* [in] */ const AMVABUFFERINFO *pamvaBufferInfo) = NULL;
  67. static HRESULT ( STDMETHODCALLTYPE *QueryRenderStatusOrg )( IAMVideoAcceleratorC * This,/* [in] */ DWORD dwTypeIndex,/* [in] */ DWORD dwBufferIndex,/* [in] */ DWORD dwFlags) = NULL;
  68. static HRESULT ( STDMETHODCALLTYPE *DisplayFrameOrg )( IAMVideoAcceleratorC * This,/* [in] */ DWORD dwFlipToIndex,/* [in] */ IMediaSample *pMediaSample) = NULL;
  69. static void LOG(LPCTSTR fmt, ...)
  70. {
  71. va_list args;
  72. va_start(args, fmt);
  73. if(TCHAR* buff = new TCHAR[_vsctprintf(fmt, args) + 1])
  74. {
  75. _vstprintf(buff, fmt, args);
  76. if(FILE* f = _tfopen(_T("c:\dxva.log"), _T("at")))
  77. {
  78. fseek(f, 0, 2);
  79. _ftprintf(f, _T("%sn"), buff);
  80. fclose(f);
  81. }
  82. delete [] buff;
  83. }
  84. va_end(args);
  85. }
  86. static void LOGPF(LPCTSTR prefix, const DDPIXELFORMAT* p, int n)
  87. {
  88. for(int i = 0; i < n; i++)
  89. {
  90. LOG(_T("%s[%d].dwSize = %d"), prefix, i, p[i].dwSize);
  91. LOG(_T("%s[%d].dwFlags = %08x"), prefix, i, p[i].dwFlags);
  92. LOG(_T("%s[%d].dwFourCC = %4.4hs"), prefix, i, &p[i].dwFourCC);
  93. LOG(_T("%s[%d].dwRGBBitCount = %08x"), prefix, i, &p[i].dwRGBBitCount);
  94. LOG(_T("%s[%d].dwRBitMask = %08x"), prefix, i, &p[i].dwRBitMask);
  95. LOG(_T("%s[%d].dwGBitMask = %08x"), prefix, i, &p[i].dwGBitMask);
  96. LOG(_T("%s[%d].dwBBitMask = %08x"), prefix, i, &p[i].dwBBitMask);
  97. LOG(_T("%s[%d].dwRGBAlphaBitMask = %08x"), prefix, i, &p[i].dwRGBAlphaBitMask);
  98. }
  99. }
  100. static void LOGUDI(LPCTSTR prefix, const AMVAUncompDataInfo* p, int n)
  101. {
  102. for(int i = 0; i < n; i++)
  103. {
  104. LOG(_T("%s[%d].dwUncompWidth = %d"), prefix, i, p[i].dwUncompWidth);
  105. LOG(_T("%s[%d].dwUncompHeight = %d"), prefix, i, p[i].dwUncompHeight);
  106. CString prefix2;
  107. prefix2.Format(_T("%s[%d]"), prefix, i);
  108. LOGPF(prefix2, &p[i].ddUncompPixelFormat, 1);
  109. }
  110. }
  111. static HRESULT STDMETHODCALLTYPE GetVideoAcceleratorGUIDsMine(
  112. IAMVideoAcceleratorC * This,
  113. /* [out][in] */ LPDWORD pdwNumGuidsSupported,
  114. /* [out][in] */ LPGUID pGuidsSupported)
  115. {
  116. LOG(_T("nGetVideoAcceleratorGUIDs"));
  117. if(pdwNumGuidsSupported)
  118. {
  119. LOG(_T("[in] *pdwNumGuidsSupported = %d"), *pdwNumGuidsSupported);
  120. }
  121. HRESULT hr = GetVideoAcceleratorGUIDsOrg(This, pdwNumGuidsSupported, pGuidsSupported);
  122. LOG(_T("hr = %08x"), hr);
  123. if(pdwNumGuidsSupported)
  124. {
  125. LOG(_T("[out] *pdwNumGuidsSupported = %d"), *pdwNumGuidsSupported);
  126. if(pGuidsSupported)
  127. {
  128. for(int i = 0; i < *pdwNumGuidsSupported; i++)
  129. {
  130. LOG(_T("[out] pGuidsSupported[%d] = %s"), i, CStringFromGUID(pGuidsSupported[i]));
  131. }
  132. }
  133. }
  134. return hr;
  135. }
  136. static HRESULT STDMETHODCALLTYPE GetUncompFormatsSupportedMine(
  137. IAMVideoAcceleratorC * This,
  138. /* [in] */ const GUID *pGuid,
  139. /* [out][in] */ LPDWORD pdwNumFormatsSupported,
  140. /* [out][in] */ LPDDPIXELFORMAT pFormatsSupported)
  141. {
  142. LOG(_T("nGetUncompFormatsSupported"));
  143. if(pGuid)
  144. {
  145. LOG(_T("[in] *pGuid = %s"), CStringFromGUID(*pGuid));
  146. }
  147. if(pdwNumFormatsSupported)
  148. {
  149. LOG(_T("[in] *pdwNumFormatsSupported = %d"), *pdwNumFormatsSupported);
  150. }
  151. HRESULT hr = GetUncompFormatsSupportedOrg(This, pGuid, pdwNumFormatsSupported, pFormatsSupported);
  152. LOG(_T("hr = %08x"), hr);
  153. if(pdwNumFormatsSupported)
  154. {
  155. LOG(_T("[out] *pdwNumFormatsSupported = %d"), *pdwNumFormatsSupported);
  156. if(pFormatsSupported)
  157. {
  158. LOGPF(_T("[out] pFormatsSupported"), pFormatsSupported, *pdwNumFormatsSupported);
  159. }
  160. }
  161. return hr;
  162. }
  163. static HRESULT STDMETHODCALLTYPE GetInternalMemInfoMine(
  164. IAMVideoAcceleratorC * This,
  165. /* [in] */ const GUID *pGuid,
  166. /* [in] */ const AMVAUncompDataInfo *pamvaUncompDataInfo,
  167. /* [out][in] */ LPAMVAInternalMemInfo pamvaInternalMemInfo)
  168. {
  169. LOG(_T("nGetInternalMemInfo"));
  170. HRESULT hr = GetInternalMemInfoOrg(This, pGuid, pamvaUncompDataInfo, pamvaInternalMemInfo);
  171. LOG(_T("hr = %08x"), hr);
  172. return hr;
  173. }
  174. static HRESULT STDMETHODCALLTYPE GetCompBufferInfoMine(
  175. IAMVideoAcceleratorC * This,
  176. /* [in] */ const GUID *pGuid,
  177. /* [in] */ const AMVAUncompDataInfo *pamvaUncompDataInfo,
  178. /* [out][in] */ LPDWORD pdwNumTypesCompBuffers,
  179. /* [out] */ LPAMVACompBufferInfo pamvaCompBufferInfo)
  180. {
  181. LOG(_T("nGetCompBufferInfo"));
  182. if(pGuid)
  183. {
  184. LOG(_T("[in] *pGuid = %s"), CStringFromGUID(*pGuid));
  185. if(pdwNumTypesCompBuffers)
  186. {
  187. LOG(_T("[in] *pdwNumTypesCompBuffers = %d"), *pdwNumTypesCompBuffers);
  188. }
  189. }
  190. HRESULT hr = GetCompBufferInfoOrg(This, pGuid, pamvaUncompDataInfo, pdwNumTypesCompBuffers, pamvaCompBufferInfo);
  191. LOG(_T("hr = %08x"), hr);
  192. if(pdwNumTypesCompBuffers)
  193. {
  194. LOG(_T("[out] *pdwNumTypesCompBuffers = %d"), *pdwNumTypesCompBuffers);
  195. if(pamvaUncompDataInfo)
  196. {
  197. LOGUDI(_T("[out] pamvaUncompDataInfo"), pamvaUncompDataInfo, *pdwNumTypesCompBuffers);
  198. }
  199. }
  200. return hr;
  201. }
  202. static HRESULT STDMETHODCALLTYPE GetInternalCompBufferInfoMine(
  203. IAMVideoAcceleratorC * This,
  204. /* [out][in] */ LPDWORD pdwNumTypesCompBuffers,
  205. /* [out] */ LPAMVACompBufferInfo pamvaCompBufferInfo)
  206. {
  207. LOG(_T("nGetInternalCompBufferInfo"));
  208. HRESULT hr = GetInternalCompBufferInfoOrg(This, pdwNumTypesCompBuffers, pamvaCompBufferInfo);
  209. LOG(_T("hr = %08x"), hr);
  210. return hr;
  211. }
  212. static HRESULT STDMETHODCALLTYPE BeginFrameMine(
  213. IAMVideoAcceleratorC * This,
  214. /* [in] */ const AMVABeginFrameInfo *amvaBeginFrameInfo)
  215. {
  216. LOG(_T("nBeginFrame"));
  217. if(amvaBeginFrameInfo)
  218. {
  219. LOG(_T("[in] amvaBeginFrameInfo->dwDestSurfaceIndex = %08x"), amvaBeginFrameInfo->dwDestSurfaceIndex);
  220. LOG(_T("[in] amvaBeginFrameInfo->pInputData = %08x"), amvaBeginFrameInfo->pInputData);
  221. LOG(_T("[in] amvaBeginFrameInfo->dwSizeInputData = %08x"), amvaBeginFrameInfo->dwSizeInputData);
  222. LOG(_T("[in] amvaBeginFrameInfo->pOutputData = %08x"), amvaBeginFrameInfo->pOutputData);
  223. LOG(_T("[in] amvaBeginFrameInfo->dwSizeOutputData = %08x"), amvaBeginFrameInfo->dwSizeOutputData);
  224. }
  225. HRESULT hr = BeginFrameOrg(This, amvaBeginFrameInfo);
  226. LOG(_T("hr = %08x"), hr);
  227. if(amvaBeginFrameInfo && amvaBeginFrameInfo->pOutputData)
  228. {
  229. LOG(_T("[out] amvaBeginFrameInfo->pOutputData = %02x %02x %02x %02x..."), 
  230. ((BYTE*)amvaBeginFrameInfo->pOutputData)[0],
  231. ((BYTE*)amvaBeginFrameInfo->pOutputData)[1],
  232. ((BYTE*)amvaBeginFrameInfo->pOutputData)[2],
  233. ((BYTE*)amvaBeginFrameInfo->pOutputData)[3]);
  234. }
  235. return hr;
  236. }
  237.         
  238. static HRESULT STDMETHODCALLTYPE EndFrameMine(
  239. IAMVideoAcceleratorC * This,
  240. /* [in] */ const AMVAEndFrameInfo *pEndFrameInfo)
  241. {
  242. LOG(_T("nEndFrame"));
  243. if(pEndFrameInfo)
  244. {
  245. LOG(_T("[in] pEndFrameInfo->dwSizeMiscData = %08x"), pEndFrameInfo->dwSizeMiscData);
  246. LOG(_T("[in] pEndFrameInfo->pMiscData = %08x"), pEndFrameInfo->pMiscData);
  247. }
  248. HRESULT hr = EndFrameOrg(This, pEndFrameInfo);
  249. LOG(_T("hr = %08x"), hr);
  250. return hr;
  251. }
  252. static HRESULT STDMETHODCALLTYPE GetBufferMine(
  253. IAMVideoAcceleratorC * This,
  254. /* [in] */ DWORD dwTypeIndex,
  255. /* [in] */ DWORD dwBufferIndex,
  256. /* [in] */ BOOL bReadOnly,
  257. /* [out] */ LPVOID *ppBuffer,
  258. /* [out] */ LONG *lpStride)
  259. {
  260. LOG(_T("nGetBuffer"));
  261. LOG(_T("[in] dwTypeIndex = %08x"), dwTypeIndex);
  262. LOG(_T("[in] dwBufferIndex = %08x"), dwBufferIndex);
  263. LOG(_T("[in] bReadOnly = %08x"), bReadOnly);
  264. LOG(_T("[in] ppBuffer = %08x"), ppBuffer);
  265. LOG(_T("[in] lpStride = %08x"), lpStride);
  266. HRESULT hr = GetBufferOrg(This, dwTypeIndex, dwBufferIndex, bReadOnly, ppBuffer, lpStride);
  267. LOG(_T("hr = %08x"), hr);
  268. LOG(_T("[out] *ppBuffer = %02x %02x %02x %02x ..."), ((BYTE*)*ppBuffer)[0], ((BYTE*)*ppBuffer)[1], ((BYTE*)*ppBuffer)[2], ((BYTE*)*ppBuffer)[3]);
  269. LOG(_T("[out] *lpStride = %08x"), *lpStride);
  270. return hr;
  271. }
  272. static HRESULT STDMETHODCALLTYPE ReleaseBufferMine(
  273. IAMVideoAcceleratorC * This,
  274. /* [in] */ DWORD dwTypeIndex,
  275. /* [in] */ DWORD dwBufferIndex)
  276. {
  277. LOG(_T("nReleaseBuffer"));
  278. LOG(_T("[in] dwTypeIndex = %08x"), dwTypeIndex);
  279. LOG(_T("[in] dwBufferIndex = %08x"), dwBufferIndex);
  280. HRESULT hr = ReleaseBufferOrg(This, dwTypeIndex, dwBufferIndex);
  281. LOG(_T("hr = %08x"), hr);
  282. return hr;
  283. }
  284. static HRESULT STDMETHODCALLTYPE ExecuteMine(
  285. IAMVideoAcceleratorC * This,
  286. /* [in] */ DWORD dwFunction,
  287. /* [in] */ LPVOID lpPrivateInputData,
  288. /* [in] */ DWORD cbPrivateInputData,
  289. /* [in] */ LPVOID lpPrivateOutputData,
  290. /* [in] */ DWORD cbPrivateOutputDat