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

多媒体编程

开发平台:

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 "mplayerc.h"
  23. #include <atlbase.h>
  24. #include <atlcoll.h>
  25. #include "....DSUtilDSUtil.h"
  26. #include <initguid.h>
  27. #include "DX7AllocatorPresenter.h"
  28. #include <ddraw.h>
  29. #include <d3d.h>
  30. #include "....SubPicDX7SubPic.h"
  31. #include "......includeRealMediapntypes.h"
  32. #include "......includeRealMediapnwintyp.h"
  33. #include "......includeRealMediapncom.h"
  34. #include "......includeRealMediarmavsurf.h"
  35. #include "IQTVideoSurface.h"
  36. #include "IPinHook.h"
  37. bool IsVMR7InGraph(IFilterGraph* pFG)
  38. {
  39. BeginEnumFilters(pFG, pEF, pBF)
  40. if(CComQIPtr<IVMRWindowlessControl>(pBF)) return(true);
  41. EndEnumFilters
  42. return(false);
  43. }
  44. namespace DSObjects
  45. {
  46. class CDX7AllocatorPresenter
  47. : public ISubPicAllocatorPresenterImpl
  48. {
  49. protected:
  50. CSize m_ScreenSize;
  51. CComPtr<IDirectDraw7> m_pDD;
  52. CComQIPtr<IDirect3D7, &IID_IDirect3D7> m_pD3D;
  53.     CComPtr<IDirect3DDevice7> m_pD3DDev;
  54. CComPtr<IDirectDrawSurface7> m_pPrimary, m_pBackBuffer;
  55. CComPtr<IDirectDrawSurface7> m_pVideoTexture, m_pVideoSurface;
  56.     virtual HRESULT CreateDevice();
  57. virtual HRESULT AllocSurfaces();
  58. virtual void DeleteSurfaces();
  59. public:
  60. CDX7AllocatorPresenter(HWND hWnd, HRESULT& hr);
  61. // ISubPicAllocatorPresenter
  62. STDMETHODIMP CreateRenderer(IUnknown** ppRenderer);
  63. STDMETHODIMP_(bool) Paint(bool fAll);
  64. STDMETHODIMP GetDIB(BYTE* lpDib, DWORD* size);
  65. };
  66. class CVMR7AllocatorPresenter
  67. : public CDX7AllocatorPresenter
  68. , public IVMRSurfaceAllocator
  69. , public IVMRImagePresenter
  70. , public IVMRWindowlessControl
  71. {
  72. CComPtr<IVMRSurfaceAllocatorNotify> m_pIVMRSurfAllocNotify;
  73. CComPtr<IVMRSurfaceAllocator> m_pSA;
  74. HRESULT CreateDevice();
  75. void DeleteSurfaces();
  76. bool m_fUseInternalTimer;
  77. public:
  78. CVMR7AllocatorPresenter(HWND hWnd, HRESULT& hr);
  79. DECLARE_IUNKNOWN
  80.     STDMETHODIMP NonDelegatingQueryInterface(REFIID riid, void** ppv);
  81. // ISubPicAllocatorPresenter
  82. STDMETHODIMP CreateRenderer(IUnknown** ppRenderer);
  83. STDMETHODIMP_(void) SetTime(REFERENCE_TIME rtNow);
  84. // IVMRSurfaceAllocator
  85.     STDMETHODIMP AllocateSurface(DWORD_PTR dwUserID, VMRALLOCATIONINFO* lpAllocInfo, DWORD* lpdwBuffer, LPDIRECTDRAWSURFACE7* lplpSurface);
  86.     STDMETHODIMP FreeSurface(DWORD_PTR dwUserID);
  87.     STDMETHODIMP PrepareSurface(DWORD_PTR dwUserID, IDirectDrawSurface7* lpSurface, DWORD dwSurfaceFlags);
  88.     STDMETHODIMP AdviseNotify(IVMRSurfaceAllocatorNotify* lpIVMRSurfAllocNotify);
  89. // IVMRImagePresenter
  90.     STDMETHODIMP StartPresenting(DWORD_PTR dwUserID);
  91.     STDMETHODIMP StopPresenting(DWORD_PTR dwUserID);
  92.     STDMETHODIMP PresentImage(DWORD_PTR dwUserID, VMRPRESENTATIONINFO* lpPresInfo);
  93. // IVMRWindowlessControl
  94. STDMETHODIMP GetNativeVideoSize(LONG* lpWidth, LONG* lpHeight, LONG* lpARWidth, LONG* lpARHeight);
  95. STDMETHODIMP GetMinIdealVideoSize(LONG* lpWidth, LONG* lpHeight);
  96. STDMETHODIMP GetMaxIdealVideoSize(LONG* lpWidth, LONG* lpHeight);
  97. STDMETHODIMP SetVideoPosition(const LPRECT lpSRCRect, const LPRECT lpDSTRect);
  98.     STDMETHODIMP GetVideoPosition(LPRECT lpSRCRect, LPRECT lpDSTRect);
  99. STDMETHODIMP GetAspectRatioMode(DWORD* lpAspectRatioMode);
  100. STDMETHODIMP SetAspectRatioMode(DWORD AspectRatioMode);
  101. STDMETHODIMP SetVideoClippingWindow(HWND hwnd);
  102. STDMETHODIMP RepaintVideo(HWND hwnd, HDC hdc);
  103. STDMETHODIMP DisplayModeChanged();
  104. STDMETHODIMP GetCurrentImage(BYTE** lpDib);
  105. STDMETHODIMP SetBorderColor(COLORREF Clr);
  106. STDMETHODIMP GetBorderColor(COLORREF* lpClr);
  107. STDMETHODIMP SetColorKey(COLORREF Clr);
  108. STDMETHODIMP GetColorKey(COLORREF* lpClr);
  109. };
  110. class CRM7AllocatorPresenter
  111. : public CDX7AllocatorPresenter
  112. , public IRMAVideoSurface
  113. {
  114. CComPtr<IDirectDrawSurface7> m_pVideoSurfaceOff;
  115. CComPtr<IDirectDrawSurface7> m_pVideoSurfaceYUY2;
  116.     RMABitmapInfoHeader m_bitmapInfo;
  117.     RMABitmapInfoHeader m_lastBitmapInfo;
  118. protected:
  119. HRESULT AllocSurfaces();
  120. void DeleteSurfaces();
  121. public:
  122. CRM7AllocatorPresenter(HWND hWnd, HRESULT& hr);
  123. DECLARE_IUNKNOWN
  124.     STDMETHODIMP NonDelegatingQueryInterface(REFIID riid, void** ppv);
  125. // IRMAVideoSurface
  126.     STDMETHODIMP Blt(UCHAR* pImageData, RMABitmapInfoHeader* pBitmapInfo, REF(PNxRect) inDestRect, REF(PNxRect) inSrcRect);
  127. STDMETHODIMP BeginOptimizedBlt(RMABitmapInfoHeader* pBitmapInfo);
  128. STDMETHODIMP OptimizedBlt(UCHAR* pImageBits, REF(PNxRect) rDestRect, REF(PNxRect) rSrcRect);
  129. STDMETHODIMP EndOptimizedBlt();
  130. STDMETHODIMP GetOptimizedFormat(REF(RMA_COMPRESSION_TYPE) ulType);
  131.     STDMETHODIMP GetPreferredFormat(REF(RMA_COMPRESSION_TYPE) ulType);
  132. };
  133. class CQT7AllocatorPresenter
  134. : public CDX7AllocatorPresenter
  135. , public IQTVideoSurface
  136. {
  137. CComPtr<IDirectDrawSurface7> m_pVideoSurfaceOff;
  138. protected:
  139. HRESULT AllocSurfaces();
  140. void DeleteSurfaces();
  141. public:
  142. CQT7AllocatorPresenter(HWND hWnd, HRESULT& hr);
  143. DECLARE_IUNKNOWN
  144.     STDMETHODIMP NonDelegatingQueryInterface(REFIID riid, void** ppv);
  145. // IQTVideoSurface
  146. STDMETHODIMP BeginBlt(const BITMAP& bm);
  147. STDMETHODIMP DoBlt(const BITMAP& bm);
  148. };
  149. }
  150. using namespace DSObjects;
  151. //
  152. HRESULT CreateAP7(const CLSID& clsid, HWND hWnd, ISubPicAllocatorPresenter** ppAP)
  153. {
  154. CheckPointer(ppAP, E_POINTER);
  155. *ppAP = NULL;
  156. HRESULT hr;
  157. if(clsid == CLSID_VMR7AllocatorPresenter && !(*ppAP = new CVMR7AllocatorPresenter(hWnd, hr))
  158. || clsid == CLSID_RM7AllocatorPresenter && !(*ppAP = new CRM7AllocatorPresenter(hWnd, hr))
  159. || clsid == CLSID_QT7AllocatorPresenter && !(*ppAP = new CQT7AllocatorPresenter(hWnd, hr)))
  160. return E_OUTOFMEMORY;
  161. if(*ppAP == NULL)
  162. return E_FAIL;
  163. (*ppAP)->AddRef();
  164. if(FAILED(hr))
  165. {
  166. (*ppAP)->Release();
  167. *ppAP = NULL;
  168. }
  169. return hr;
  170. }
  171. //
  172. static HRESULT TextureBlt(CComPtr<IDirect3DDevice7> pD3DDev, CComPtr<IDirectDrawSurface7> pTexture, Vector dst[4], CRect src)
  173. {
  174. if(!pTexture)
  175. return E_POINTER;
  176. HRESULT hr;
  177.     do
  178. {
  179. DDSURFACEDESC2 ddsd;
  180. INITDDSTRUCT(ddsd);
  181. if(FAILED(hr = pTexture->GetSurfaceDesc(&ddsd)))
  182. break;
  183.         float w = (float)ddsd.dwWidth;
  184.         float h = (float)ddsd.dwHeight;
  185. struct
  186. {
  187. float x, y, z, rhw;
  188. float tu, tv;
  189. }
  190. pVertices[] =
  191. {
  192. {(float)dst[0].x, (float)dst[0].y, (float)dst[0].z, 1.0f/(float)dst[0].z, (float)src.left / w, (float)src.top / h},
  193. {(float)dst[1].x, (float)dst[1].y, (float)dst[1].z, 1.0f/(float)dst[1].z, (float)src.right / w, (float)src.top / h},
  194. {(float)dst[2].x, (float)dst[2].y, (float)dst[2].z, 1.0f/(float)dst[2].z, (float)src.left / w, (float)src.bottom / h},
  195. {(float)dst[3].x, (float)dst[3].y, (float)dst[3].z, 1.0f/(float)dst[3].z, (float)src.right / w, (float)src.bottom / h},
  196. };
  197. for(int i = 0; i < countof(pVertices); i++)
  198. {
  199. pVertices[i].x -= 0.5;
  200. pVertices[i].y -= 0.5;
  201. }
  202.         hr = pD3DDev->SetTexture(0, pTexture);
  203.         pD3DDev->SetRenderState(D3DRENDERSTATE_CULLMODE, D3DCULL_NONE);
  204.         pD3DDev->SetRenderState(D3DRENDERSTATE_LIGHTING, FALSE);
  205.         pD3DDev->SetRenderState(D3DRENDERSTATE_BLENDENABLE, FALSE);
  206. pD3DDev->SetRenderState(D3DRENDERSTATE_ALPHATESTENABLE, FALSE); 
  207.         pD3DDev->SetTextureStageState(0, D3DTSS_MAGFILTER, D3DTFG_LINEAR);
  208.         pD3DDev->SetTextureStageState(0, D3DTSS_MINFILTER, D3DTFN_LINEAR);
  209.         pD3DDev->SetTextureStageState(0, D3DTSS_MIPFILTER, D3DTFP_LINEAR);
  210.         pD3DDev->SetTextureStageState(0, D3DTSS_ADDRESS, D3DTADDRESS_CLAMP);
  211. //
  212.         if(FAILED(hr = pD3DDev->BeginScene()))
  213. break;
  214. hr = pD3DDev->DrawPrimitive(D3DPT_TRIANGLESTRIP,
  215. D3DFVF_XYZRHW | D3DFVF_TEX1,
  216. pVertices, 4, D3DDP_WAIT);
  217. pD3DDev->EndScene();
  218.         //
  219. pD3DDev->SetTexture(0, NULL);
  220. return S_OK;
  221.     }
  222. while(0);
  223.     return E_FAIL;
  224. }
  225. //
  226. // CDX7AllocatorPresenter
  227. //
  228. CDX7AllocatorPresenter::CDX7AllocatorPresenter(HWND hWnd, HRESULT& hr) 
  229. : ISubPicAllocatorPresenterImpl(hWnd, hr)
  230. , m_ScreenSize(0, 0)
  231. {
  232. if(FAILED(hr)) return;
  233. if(FAILED(hr = DirectDrawCreateEx(NULL, (VOID**)&m_pDD, IID_IDirectDraw7, NULL))
  234. || FAILED(hr = m_pDD->SetCooperativeLevel(AfxGetMainWnd()->GetSafeHwnd(), DDSCL_NORMAL)))
  235. return;
  236. if(!(m_pD3D = m_pDD)) {hr = E_NOINTERFACE; return;}
  237. hr = CreateDevice();
  238. }
  239. HRESULT CDX7AllocatorPresenter::CreateDevice()
  240. {
  241.     m_pD3DDev = NULL;
  242. m_pPrimary = NULL;
  243. m_pBackBuffer = NULL;
  244.     DDSURFACEDESC2 ddsd;
  245. INITDDSTRUCT(ddsd);
  246.     if(FAILED(m_pDD->GetDisplayMode(&ddsd))
  247. || ddsd.ddpfPixelFormat.dwRGBBitCount <= 8)
  248. return DDERR_INVALIDMODE;
  249. m_ScreenSize.SetSize(ddsd.dwWidth, ddsd.dwHeight);
  250. HRESULT hr;
  251. // m_pPrimary
  252. INITDDSTRUCT(ddsd);
  253.     ddsd.dwFlags = DDSD_CAPS;
  254.     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
  255.     if(FAILED(hr = m_pDD->CreateSurface(&ddsd, &m_pPrimary, NULL)))
  256.         return hr;
  257. CComPtr<IDirectDrawClipper> pcClipper;
  258.     if(FAILED(hr = m_pDD->CreateClipper(0, &pcClipper, NULL)))
  259.         return hr;
  260. pcClipper->SetHWnd(0, m_hWnd);
  261. m_pPrimary->SetClipper(pcClipper);
  262. // m_pBackBuffer
  263. INITDDSTRUCT(ddsd);
  264.     ddsd.dwFlags        = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
  265.     ddsd.ddsCaps.dwCaps = /*DDSCAPS_OFFSCREENPLAIN |*/ DDSCAPS_VIDEOMEMORY | DDSCAPS_3DDEVICE;
  266. ddsd.dwWidth = m_ScreenSize.cx;
  267. ddsd.dwHeight = m_ScreenSize.cy;
  268. if(FAILED(hr = m_pDD->CreateSurface(&ddsd, &m_pBackBuffer, NULL)))
  269.         return hr;
  270. pcClipper = NULL;
  271.     if(FAILED(hr = m_pDD->CreateClipper(0, &pcClipper, NULL)))
  272. return hr;
  273.     BYTE rgnDataBuffer[1024];
  274. HRGN hrgn = CreateRectRgn(0, 0, ddsd.dwWidth, ddsd.dwHeight);
  275. GetRegionData(hrgn, sizeof(rgnDataBuffer), (RGNDATA*)rgnDataBuffer);
  276. DeleteObject(hrgn);
  277. pcClipper->SetClipList((RGNDATA*)rgnDataBuffer, 0);
  278. m_pBackBuffer->SetClipper(pcClipper);
  279. // m_pD3DDev
  280. if(FAILED(hr = m_pD3D->CreateDevice(IID_IDirect3DHALDevice, m_pBackBuffer, &m_pD3DDev))) // this seems to fail if the desktop size is too large (width or height >2048)
  281. return hr;
  282. //
  283. CComPtr<ISubPicProvider> pSubPicProvider;
  284. if(m_pSubPicQueue) m_pSubPicQueue->GetSubPicProvider(&pSubPicProvider);
  285. CSize size;
  286. switch(AfxGetAppSettings().nSPCMaxRes)
  287. {
  288. case 0: default: size = m_ScreenSize; break;
  289. case 1: size.SetSize(1024, 768); break;
  290. case 2: size.SetSize(800, 600); break;
  291. case 3: size.SetSize(640, 480); break;
  292. case 4: size.SetSize(512, 384); break;
  293. case 5: size.SetSize(384, 288); break;
  294. }
  295. if(m_pAllocator)
  296. {
  297. m_pAllocator->ChangeDevice(m_pD3DDev);
  298. }
  299. else
  300. {
  301. m_pAllocator = new CDX7SubPicAllocator(m_pD3DDev, size, AfxGetAppSettings().fSPCPow2Tex);
  302. if(!m_pAllocator || FAILED(hr))
  303. return E_FAIL;
  304. }
  305. hr = S_OK;
  306. m_pSubPicQueue = AfxGetAppSettings().nSPCSize > 0 
  307. ? (ISubPicQueue*)new CSubPicQueue(AfxGetAppSettings().nSPCSize, m_pAllocator, &hr)
  308. : (ISubPicQueue*)new CSubPicQueueNoThread(m_pAllocator, &hr);
  309. if(!m_pSubPicQueue || FAILED(hr))
  310. return E_FAIL;
  311. if(pSubPicProvider) m_pSubPicQueue->SetSubPicProvider(pSubPicProvider);
  312. return S_OK;
  313. }
  314. HRESULT CDX7AllocatorPresenter::AllocSurfaces()
  315. {
  316.     CAutoLock cAutoLock(this);
  317. AppSettings& s = AfxGetAppSettings();
  318. m_pVideoTexture = NULL;
  319. m_pVideoSurface = NULL;
  320. DDSURFACEDESC2 ddsd;
  321. INITDDSTRUCT(ddsd);
  322. ddsd.dwFlags = DDSD_CAPS|DDSD_WIDTH|DDSD_HEIGHT|DDSD_PIXELFORMAT;
  323. ddsd.ddsCaps.dwCaps = DDSCAPS_VIDEOMEMORY;
  324. ddsd.dwWidth = m_NativeVideoSize.cx;
  325. ddsd.dwHeight = m_NativeVideoSize.cy;
  326. ddsd.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
  327. ddsd.ddpfPixelFormat.dwFlags = DDPF_RGB;
  328. ddsd.ddpfPixelFormat.dwRGBBitCount = 32;
  329. ddsd.ddpfPixelFormat.dwRBitMask = 0x00FF0000;
  330. ddsd.ddpfPixelFormat.dwGBitMask = 0x0000FF00;
  331. ddsd.ddpfPixelFormat.dwBBitMask = 0x000000FF;
  332. if(s.iAPSurfaceUsage == VIDRNDT_AP_TEXTURE2D || s.iAPSurfaceUsage == VIDRNDT_AP_TEXTURE3D)
  333. {
  334. ddsd.ddsCaps.dwCaps |= DDSCAPS_TEXTURE;
  335. // ddsd.ddpfPixelFormat.dwFlags |= DDPF_ALPHAPIXELS;
  336. // ddsd.ddpfPixelFormat.dwRGBAlphaBitMask = 0xFF000000;
  337. }
  338. HRESULT hr = m_pDD->CreateSurface(&ddsd, &m_pVideoSurface, NULL);
  339. if(FAILED(hr))
  340. {
  341. // FIXME: eh, dx9 has no problem creating a 32bpp surface under a 16bpp desktop, but dx7 fails here (textures are ok)
  342. DDSURFACEDESC2 ddsd2;
  343. INITDDSTRUCT(ddsd2);
  344. if(!(s.iAPSurfaceUsage == VIDRNDT_AP_TEXTURE2D || s.iAPSurfaceUsage == VIDRNDT_AP_TEXTURE3D)
  345. && SUCCEEDED(m_pDD->GetDisplayMode(&ddsd2))
  346. && ddsd2.ddpfPixelFormat.dwRGBBitCount == 16)
  347. {
  348. ddsd.ddpfPixelFormat.dwRGBBitCount = 16;
  349. ddsd.ddpfPixelFormat.dwRBitMask = 0x0000F800;
  350. ddsd.ddpfPixelFormat.dwGBitMask = 0x000007E0;
  351. ddsd.ddpfPixelFormat.dwBBitMask = 0x0000001F;
  352. hr = m_pDD->CreateSurface(&ddsd, &m_pVideoSurface, NULL);
  353. }
  354. if(FAILED(hr))
  355. return hr;
  356. }
  357. if(s.iAPSurfaceUsage == VIDRNDT_AP_TEXTURE3D)
  358. m_pVideoTexture = m_pVideoSurface;
  359. DDBLTFX fx;
  360. INITDDSTRUCT(fx);
  361. fx.dwFillColor = 0;
  362. hr = m_pVideoSurface->Blt(NULL, NULL, NULL, DDBLT_WAIT|DDBLT_COLORFILL, &fx);
  363. return S_OK;
  364. }
  365. void CDX7AllocatorPresenter::DeleteSurfaces()
  366. {
  367.     CAutoLock cAutoLock(this);
  368. m_pVideoTexture = NULL;
  369. m_pVideoSurface = NULL;
  370. }
  371. // ISubPicAllocatorPresenter
  372. STDMETHODIMP CDX7AllocatorPresenter::CreateRenderer(IUnknown** ppRenderer)
  373. {
  374. return E_NOTIMPL;
  375. }
  376. STDMETHODIMP_(bool) CDX7AllocatorPresenter::Paint(bool fAll)
  377. {
  378. CAutoLock cAutoLock(this);
  379. if(m_WindowRect.right <= m_WindowRect.left || m_WindowRect.bottom <= m_WindowRect.top
  380. || m_NativeVideoSize.cx <= 0 || m_NativeVideoSize.cy <= 0
  381. || !m_pPrimary || !m_pBackBuffer || !m_pVideoSurface)
  382. return(false);
  383. HRESULT hr;
  384. CRect rSrcVid(CPoint(0, 0), m_NativeVideoSize);
  385. CRect rDstVid(m_VideoRect);
  386. CRect rSrcPri(CPoint(0, 0), m_WindowRect.Size());
  387. CRect rDstPri(m_WindowRect);
  388. MapWindowRect(m_hWnd, HWND_DESKTOP, &rDstPri);
  389. if(fAll)
  390. {
  391. // clear the backbuffer
  392. CRect rl(0, 0, rDstVid.left, rSrcPri.bottom);
  393. CRect rr(rDstVid.right, 0, rSrcPri.right, rSrcPri.bottom);
  394. CRect rt(0, 0, rSrcPri.right, rDstVid.top);
  395. CRect rb(0, rDstVid.bottom, rSrcPri.right, rSrcPri.bottom);
  396. DDBLTFX fx;
  397. INITDDSTRUCT(fx);
  398. fx.dwFillColor = 0;
  399. hr = m_pBackBuffer->Blt(NULL, NULL, NULL, DDBLT_WAIT|DDBLT_COLORFILL, &fx);
  400. // paint the video on the backbuffer
  401. if(!rDstVid.IsRectEmpty())
  402. {
  403. if(m_pVideoTexture)
  404. {
  405. Vector v[4];
  406. Transform(rDstVid, v);
  407. hr = TextureBlt(m_pD3DDev, m_pVideoTexture, v, rSrcVid);
  408. }
  409. else
  410. {
  411. hr = m_pBackBuffer->Blt(rDstVid, m_pVideoSurface, rSrcVid, DDBLT_WAIT, NULL);
  412. }
  413. }
  414. // paint the text on the backbuffer
  415. AlphaBltSubPic(rSrcPri.Size());
  416. }
  417. // wait vsync
  418. m_pDD->WaitForVerticalBlank(DDWAITVB_BLOCKBEGIN, NULL);
  419. // blt to the primary surface
  420. hr = m_pPrimary->Blt(rDstPri, m_pBackBuffer, rSrcPri, DDBLT_WAIT, NULL);
  421. if(hr == DDERR_SURFACELOST)
  422. {
  423. HRESULT hr = DDERR_WRONGMODE; // m_pDD->TestCooperativeLevel();
  424. if(hr == DDERR_WRONGMODE) 
  425. {
  426. DeleteSurfaces();
  427. if(SUCCEEDED(CreateDevice()) || FAILED(hr = AllocSurfaces()))
  428. return(true);
  429. }
  430. hr = S_OK;
  431. }
  432. return(true);
  433. }
  434. STDMETHODIMP CDX7AllocatorPresenter::GetDIB(BYTE* lpDib, DWORD* size)
  435. {
  436. CheckPointer(size, E_POINTER);
  437. HRESULT hr;
  438. DDSURFACEDESC2 ddsd;
  439. INITDDSTRUCT(ddsd);
  440. if(FAILED(m_pVideoSurface->GetSurfaceDesc(&ddsd)))
  441. return E_FAIL;
  442. if(ddsd.ddpfPixelFormat.dwRGBBitCount != 16 && ddsd.ddpfPixelFormat.dwRGBBitCount != 32)
  443. return E_FAIL;
  444. DWORD required = sizeof(BITMAPINFOHEADER) + (ddsd.dwWidth*ddsd.dwHeight*32>>3);
  445. if(!lpDib) {*size = required; return S_OK;}
  446. if(*size < required) return E_OUTOFMEMORY;
  447. *size = required;
  448. INITDDSTRUCT(ddsd);
  449. if(FAILED(hr = m_pVideoSurface->Lock(NULL, &ddsd, DDLOCK_WAIT|DDLOCK_SURFACEMEMORYPTR|DDLOCK_READONLY|DDLOCK_NOSYSLOCK, NULL)))
  450. {
  451. // TODO
  452. return hr;
  453. }
  454. BITMAPINFOHEADER* bih = (BITMAPINFOHEADER*)lpDib;
  455. memset(bih, 0, sizeof(BITMAPINFOHEADER));
  456. bih->biSize = sizeof(BITMAPINFOHEADER);
  457. bih->biWidth = ddsd.dwWidth;
  458. bih->biHeight = ddsd.dwHeight;
  459. bih->biBitCount = 32;
  460. bih->biPlanes = 1;
  461. bih->biSizeImage = bih->biWidth*bih->biHeight*bih->biBitCount>>3;
  462. BitBltFromRGBToRGB(
  463. bih->biWidth, bih->biHeight, 
  464. (BYTE*)(bih + 1), bih->biWidth*bih->biBitCount>>3, bih->biBitCount,
  465. (BYTE*)ddsd.lpSurface + ddsd.lPitch*(ddsd.dwHeight-1), -(int)ddsd.lPitch, ddsd.ddpfPixelFormat.dwRGBBitCount);
  466. m_pVideoSurface->Unlock(NULL);
  467. /*
  468. BitBltFromRGBToRGB(
  469. w, h, 
  470. (BYTE*)ddsd.lpSurface, ddsd.lPitch, ddsd.ddpfPixelFormat.dwRGBBitCount,
  471. (BYTE*)bm.bmBits, bm.bmWidthBytes, bm.bmBitsPixel);
  472. m_pVideoSurfaceOff->Unlock(NULL);
  473. fOk = true;
  474. }
  475. */
  476. return S_OK;
  477. }
  478. //
  479. // CVMR7AllocatorPresenter
  480. //
  481. #define MY_USER_ID 0x6ABE51
  482. CVMR7AllocatorPresenter::CVMR7AllocatorPresenter(HWND hWnd, HRESULT& hr) 
  483. : CDX7AllocatorPresenter(hWnd, hr)
  484. , m_fUseInternalTimer(false)
  485. {
  486.     if(FAILED(hr))
  487. return;
  488. if(FAILED(hr = m_pSA.CoCreateInstance(CLSID_AllocPresenter)))
  489. {
  490. hr = E_FAIL;
  491. return;
  492. }
  493. }
  494. STDMETHODIMP CVMR7AllocatorPresenter::NonDelegatingQueryInterface(REFIID riid, void** ppv)
  495. {
  496.     CheckPointer(ppv, E_POINTER);
  497. return 
  498. QI(IVMRSurfaceAllocator)
  499. QI(IVMRImagePresenter)
  500. QI(IVMRWindowlessControl)
  501. __super::NonDelegatingQueryInterface(riid, ppv);
  502. }
  503. HRESULT CVMR7AllocatorPresenter::CreateDevice()
  504. {
  505. HRESULT hr = __super::CreateDevice();
  506. if(FAILED(hr)) return hr;
  507. if(m_pIVMRSurfAllocNotify)
  508. {
  509. HMONITOR hMonitor = MonitorFromWindow(m_hWnd, MONITOR_DEFAULTTONEAREST);
  510. if(FAILED(hr = m_pIVMRSurfAllocNotify->ChangeDDrawDevice(m_pDD, hMonitor)))
  511. return(false);
  512. }
  513. return hr;
  514. }
  515. void CVMR7AllocatorPresenter::DeleteSurfaces()
  516. {
  517.     CAutoLock cAutoLock(this);
  518. m_pSA->FreeSurface(MY_USER_ID);
  519. __super::DeleteSurfaces();
  520. }
  521. // ISubPicAllocatorPresenter
  522. STDMETHODIMP CVMR7AllocatorPresenter::CreateRenderer(IUnknown** ppRenderer)
  523. {
  524.     CheckPointer(ppRenderer, E_POINTER);
  525. *ppRenderer = NULL;
  526. HRESULT hr;
  527. do
  528. {
  529. CComPtr<IBaseFilter> pBF;
  530. if(FAILED(hr = pBF.CoCreateInstance(CLSID_VideoMixingRenderer)))
  531. break;
  532. CComQIPtr<IVMRFilterConfig> pConfig = pBF;
  533. if(!pConfig)
  534. break;
  535. if(FAILED(hr = pConfig->SetRenderingMode(VMRMode_Renderless)))
  536. break;
  537. CComQIPtr<IVMRSurfaceAllocatorNotify> pSAN = pBF;
  538. if(!pSAN)
  539. break;
  540. if(FAILED(hr = pSAN->AdviseSurfaceAllocator(MY_USER_ID, static_cast<IVMRSurfaceAllocator*>(this)))
  541. || FAILED(hr = AdviseNotify(pSAN)))
  542. break;
  543. CComPtr<IPin> pPin = GetFirstPin(pBF);
  544. CComQIPtr<IMemInputPin> pMemInputPin = pPin;
  545. m_fUseInternalTimer = HookNewSegmentAndReceive((IPinC*)(IPin*)pPin, (IMemInputPinC*)(IMemInputPin*)pMemInputPin);
  546. *ppRenderer = (IUnknown*)pBF.Detach();
  547. return S_OK;
  548. }
  549. while(0);
  550.     return E_FAIL;
  551. }
  552. STDMETHODIMP_(void) CVMR7AllocatorPresenter::SetTime(REFERENCE_TIME rtNow)
  553. {
  554. __super::SetTime(rtNow);
  555. m_fUseInternalTimer = false;
  556. }
  557. // IVMRSurfaceAllocator
  558. STDMETHODIMP CVMR7AllocatorPresenter::AllocateSurface(DWORD_PTR dwUserID, VMRALLOCATIONINFO* lpAllocInfo, DWORD* lpdwBuffer, LPDIRECTDRAWSURFACE7* lplpSurface)
  559. {
  560. if(!lpAllocInfo || !lpdwBuffer || !lplpSurface)
  561. return E_POINTER;
  562. if(!m_pIVMRSurfAllocNotify)
  563. return E_FAIL;
  564. HRESULT hr;
  565.     DeleteSurfaces();
  566. // HACK: yv12 will fail to blt onto the backbuffer anyway, but if we first
  567. // allocate it and then let our FreeSurface callback call m_pSA->FreeSurface,
  568. // then that might stall for about 30 seconds because of some unknown buggy code 
  569. // behind <ddraw surface>->Release()
  570. if(lpAllocInfo->lpHdr->biBitCount < 16)
  571. return E_FAIL;
  572. hr = m_pSA->AllocateSurface(dwUserID, lpAllocInfo, lpdwBuffer, lplpSurface);
  573. if(FAILED(hr))
  574. return hr;
  575. m_NativeVideoSize = CSize(abs(lpAllocInfo->lpHdr->biWidth), abs(lpAllocInfo->lpHdr->biHeight));
  576. m_AspectRatio = m_NativeVideoSize;
  577. int arx = lpAllocInfo->szAspectRatio.cx, ary = lpAllocInfo->szAspectRatio.cy;
  578. if(arx > 0 && ary > 0) m_AspectRatio.SetSize(arx, ary);
  579. if(FAILED(hr = AllocSurfaces()))
  580. return hr;
  581. // test if the colorspace is acceptable
  582. if(FAILED(hr = m_pVideoSurface->Blt(NULL, *lplpSurface, NULL, DDBLT_WAIT, NULL)))
  583. {
  584. DeleteSurfaces();
  585. return hr;
  586. }
  587. DDBLTFX fx;
  588. INITDDSTRUCT(fx);
  589. fx.dwFillColor = 0;
  590. m_pVideoSurface->Blt(NULL, NULL, NULL, DDBLT_WAIT|DDBLT_COLORFILL, &fx);
  591. return hr;
  592. }
  593. STDMETHODIMP CVMR7AllocatorPresenter::FreeSurface(DWORD_PTR dwUserID)
  594. {
  595.     DeleteSurfaces();
  596. return S_OK;
  597. }
  598. STDMETHODIMP CVMR7AllocatorPresenter::PrepareSurface(DWORD_PTR dwUserID, IDirectDrawSurface7* lpSurface, DWORD dwSurfaceFlags)
  599. {
  600.     if(!lpSurface)
  601. return E_POINTER;
  602. // FIXME: sometimes the msmpeg4/divx3/wmv decoder wants to reuse our 
  603. // surface (expects it to point to the same mem every time), and to avoid 
  604. // problems we can't call m_pSA->PrepareSurface (flips? clears?).
  605. return S_OK; 
  606. /*
  607. return m_pSA->PrepareSurface(dwUserID, lpSurface, dwSurfaceFlags);
  608. */
  609. }
  610. STDMETHODIMP CVMR7AllocatorPresenter::AdviseNotify(IVMRSurfaceAllocatorNotify* lpIVMRSurfAllocNotify)
  611. {
  612.     CAutoLock cAutoLock(this);
  613. m_pIVMRSurfAllocNotify = lpIVMRSurfAllocNotify;
  614. HRESULT hr;
  615. HMONITOR hMonitor = MonitorFromWindow(m_hWnd, MONITOR_DEFAULTTONEAREST);
  616. if(FAILED(hr = m_pIVMRSurfAllocNotify->SetDDrawDevice(m_pDD, hMonitor)))
  617. return hr;
  618. return m_pSA->AdviseNotify(lpIVMRSurfAllocNotify);
  619. }
  620. // IVMRImagePresenter
  621. STDMETHODIMP CVMR7AllocatorPresenter::StartPresenting(DWORD_PTR dwUserID)
  622. {
  623.     CAutoLock cAutoLock(this);
  624.     ASSERT(m_pD3DDev);
  625. return m_pD3DDev ? S_OK : E_FAIL;
  626. }
  627. STDMETHODIMP CVMR7AllocatorPresenter::StopPresenting(DWORD_PTR dwUserID)
  628. {
  629. return S_OK;
  630. }
  631. STDMETHODIMP CVMR7AllocatorPresenter::PresentImage(DWORD_PTR dwUserID, VMRPRESENTATIONINFO* lpPresInfo)
  632. {
  633.     HRESULT hr;
  634. {
  635. if(!lpPresInfo || !lpPresInfo->lpSurf)
  636. return E_POINTER;
  637. CAutoLock cAutoLock(this);
  638. hr = m_pVideoSurface->Blt(NULL, lpPresInfo->lpSurf, NULL, DDBLT_WAIT, NULL);
  639. if(lpPresInfo->rtEnd > lpPresInfo->rtStart)
  640. {
  641. REFERENCE_TIME rtTimePerFrame = lpPresInfo->rtEnd - lpPresInfo->rtStart;
  642. m_fps = 10000000.0 / rtTimePerFrame;
  643. if(m_pSubPicQueue) 
  644. {
  645. m_pSubPicQueue->SetFPS(m_fps);
  646. if(m_fUseInternalTimer)
  647. {
  648. __super::SetTime(g_tSegmentStart + g_tSampleStart);
  649. }
  650. }
  651. }
  652. CSize VideoSize = m_NativeVideoSize;
  653. int arx = lpPresInfo->szAspectRatio.cx, ary = lpPresInfo->szAspectRatio.cy;
  654. if(arx > 0 && ary > 0) VideoSize.cx = VideoSize.cy*arx/ary;
  655. if(VideoSize != GetVideoSize())
  656. {
  657. m_AspectRatio.SetSize(arx, ary);
  658. AfxGetApp()->m_pMainWnd->PostMessage(WM_REARRANGERENDERLESS);
  659. }
  660. Paint(true);
  661. hr = S_OK;
  662. }
  663. return S_OK;
  664. }
  665. // IVMRWindowlessControl
  666. //
  667. // It is only implemented (partially) for the dvd navigator's 
  668. // menu handling, which needs to know a few things about the 
  669. // location of our window.
  670. STDMETHODIMP CVMR7AllocatorPresenter::GetNativeVideoSize(LONG* lpWidth, LONG* lpHeight, LONG* lpARWidth, LONG* lpARHeight)
  671. {
  672. CSize vs = m_NativeVideoSize, ar = m_AspectRatio;
  673. // DVD Nav. bug workaround fix
  674. vs.cx = vs.cy * ar.cx / ar.cy;
  675. if(lpWidth) *lpWidth = vs.cx;
  676. if(lpHeight) *lpHeight = vs.cy;
  677. if(lpARWidth) *lpARWidth = ar.cx;
  678. if(lpARHeight) *lpARHeight = ar.cy;
  679. return S_OK;
  680. }
  681. STDMETHODIMP CVMR7AllocatorPresenter::GetMinIdealVideoSize(LONG* lpWidth, LONG* lpHeight) {return E_NOTIMPL;}
  682. STDMETHODIMP CVMR7AllocatorPresenter::GetMaxIdealVideoSize(LONG* lpWidth, LONG* lpHeight) {return E_NOTIMPL;}
  683. STDMETHODIMP CVMR7AllocatorPresenter::SetVideoPosition(const LPRECT lpSRCRect, const LPRECT lpDSTRect) {return E_NOTIMPL;} // we have our own method for this
  684. STDMETHODIMP CVMR7AllocatorPresenter::GetVideoPosition(LPRECT lpSRCRect, LPRECT lpDSTRect)
  685. {
  686. CopyRect(lpSRCRect, CRect(CPoint(0, 0), m_NativeVideoSize));
  687. CopyRect(lpDSTRect, &m_VideoRect);
  688. // DVD Nav. bug workaround fix
  689. GetNativeVideoSize(&lpSRCRect->right, &lpSRCRect->bottom, NULL, NULL);
  690. return S_OK;
  691. }
  692. STDMETHODIMP CVMR7AllocatorPresenter::GetAspectRatioMode(DWORD* lpAspectRatioMode)
  693. {
  694. if(lpAspectRatioMode) *lpAspectRatioMode = AM_ARMODE_STRETCHED;
  695. return S_OK;
  696. }
  697. STDMETHODIMP CVMR7AllocatorPresenter::SetAspectRatioMode(DWORD AspectRatioMode) {return E_NOTIMPL;}
  698. STDMETHODIMP CVMR7AllocatorPresenter::SetVideoClippingWindow(HWND hwnd) {return E_NOTIMPL;}
  699. STDMETHODIMP CVMR7AllocatorPresenter::RepaintVideo(HWND hwnd, HDC hdc) {return E_NOTIMPL;}
  700. STDMETHODIMP CVMR7AllocatorPresenter::DisplayModeChanged() {return E_NOTIMPL;}
  701. STDMETHODIMP CVMR7AllocatorPresenter::GetCurrentImage(BYTE** lpDib) {return E_NOTIMPL;}
  702. STDMETHODIMP CVMR7AllocatorPresenter::SetBorderColor(COLORREF Clr) {return E_NOTIMPL;}
  703. STDMETHODIMP CVMR7AllocatorPresenter::GetBorderColor(COLORREF* lpClr)
  704. {
  705. if(lpClr) *lpClr = 0;
  706. return S_OK;
  707. }
  708. STDMETHODIMP CVMR7AllocatorPresenter::SetColorKey(COLORREF Clr) {return E_NOTIMPL;}
  709. STDMETHODIMP CVMR7AllocatorPresenter::GetColorKey(COLORREF* lpClr) {return E_NOTIMPL;}
  710. //
  711. static HRESULT AllocDX7Surface(IDirectDraw7* pDD, CSize size, DWORD compression, int bpp, IDirectDrawSurface7** pSurface)
  712. {
  713. if(!pDD || !pSurface || size.cx <= 0 || size.cy <= 0)
  714. return E_POINTER;
  715. *pSurface = NULL;
  716. DDSURFACEDESC2 ddsd;
  717. INITDDSTRUCT(ddsd);
  718. ddsd.dwFlags = DDSD_CAPS|DDSD_WIDTH|DDSD_HEIGHT|DDSD_PIXELFORMAT;
  719. ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN|DDSCAPS_VIDEOMEMORY;
  720. ddsd.dwWidth = size.cx;
  721. ddsd.dwHeight = size.cy;
  722. ddsd.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
  723. if(compression >= 0x1000)
  724. {
  725. ddsd.ddpfPixelFormat.dwFlags = DDPF_FOURCC;
  726. ddsd.ddpfPixelFormat.dwFourCC = compression;
  727. }
  728. else if((compression == 0 || compression == 3) && (bpp == 15 || bpp == 16 || bpp == 24 || bpp == 32))
  729. {
  730. ddsd.ddpfPixelFormat.dwFlags = DDPF_RGB;
  731. ddsd.ddpfPixelFormat.dwRGBBitCount = max(bpp, 16);
  732. ddsd.ddpfPixelFormat.dwRGBAlphaBitMask = (bpp == 16) ? 0x0000 : (bpp == 15) ? 0x8000 : 0xFF000000;
  733. ddsd.ddpfPixelFormat.dwRBitMask = (bpp == 16) ? 0xf800 : (bpp == 15) ? 0x7c00 : 0x00FF0000;
  734. ddsd.ddpfPixelFormat.dwGBitMask = (bpp == 16) ? 0x07e0 : (bpp == 15) ? 0x03e0 : 0x0000FF00;
  735. ddsd.ddpfPixelFormat.dwBBitMask = (bpp == 16) ? 0x001F : (bpp == 15) ? 0x001F : 0x000000FF;
  736. }
  737. return pDD->CreateSurface(&ddsd, pSurface, NULL);
  738. }
  739. //
  740. // CRM7AllocatorPresenter
  741. //
  742. CRM7AllocatorPresenter::CRM7AllocatorPresenter(HWND hWnd, HRESULT& hr) 
  743. : CDX7AllocatorPresenter(hWnd, hr)
  744. {
  745. }
  746. STDMETHODIMP CRM7AllocatorPresenter::NonDelegatingQueryInterface(REFIID riid, void** ppv)
  747. {
  748.     CheckPointer(ppv, E_POINTER);
  749. return 
  750. QI2(IRMAVideoSurface)
  751. __super::NonDelegatingQueryInterface(riid, ppv);
  752. }
  753. HRESULT CRM7AllocatorPresenter::AllocSurfaces()
  754. {
  755.     CAutoLock cAutoLock(this);
  756. m_pVideoSurfaceOff = NULL;
  757. m_pVideoSurfaceYUY2 = NULL;
  758. DDSURFACEDESC2 ddsd;
  759. DDBLTFX fx;
  760. INITDDSTRUCT(ddsd);
  761. ddsd.dwFlags = DDSD_CAPS|DDSD_WIDTH|DDSD_HEIGHT|DDSD_PIXELFORMAT;
  762. ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
  763. ddsd.dwWidth = m_NativeVideoSize.cx;
  764. ddsd.dwHeight = m_NativeVideoSize.cy;
  765. ddsd.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
  766. ddsd.ddpfPixelFormat.dwFlags = DDPF_RGB;
  767. ddsd.ddpfPixelFormat.dwRGBBitCount = 32;
  768. ddsd.ddpfPixelFormat.dwRGBAlphaBitMask = 0xFF000000;
  769. ddsd.ddpfPixelFormat.dwRBitMask = 0x00FF0000;
  770. ddsd.ddpfPixelFormat.dwGBitMask = 0x0000FF00;
  771. ddsd.ddpfPixelFormat.dwBBitMask = 0x000000FF;
  772. HRESULT hr = m_pDD->CreateSurface(&ddsd, &m_pVideoSurfaceOff, NULL);
  773. if(FAILED(hr)) return E_FAIL;
  774. INITDDSTRUCT(fx);
  775. fx.dwFillColor = 0;
  776. m_pVideoSurfaceOff->Blt(NULL, NULL, NULL, DDBLT_WAIT|DDBLT_COLORFILL, &fx);
  777. INITDDSTRUCT(ddsd);
  778. ddsd.dwFlags = DDSD_CAPS|DDSD_WIDTH|DDSD_HEIGHT|DDSD_PIXELFORMAT;
  779. ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
  780. ddsd.dwWidth = m_NativeVideoSize.cx;
  781. ddsd.dwHeight = m_NativeVideoSize.cy;
  782. ddsd.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
  783. ddsd.ddpfPixelFormat.dwFlags = DDPF_FOURCC;
  784. ddsd.ddpfPixelFormat.dwYUVBitCount = 16;
  785. ddsd.ddpfPixelFormat.dwFourCC = '2YUY';
  786. hr = m_pDD->CreateSurface(&ddsd, &m_pVideoSurfaceYUY2, NULL);
  787. if(FAILED(m_pVideoSurfaceOff->Blt(NULL, m_pVideoSurfaceYUY2, NULL, DDBLT_WAIT, NULL)))
  788. m_pVideoSurfaceYUY2 = NULL;
  789. if(m_pVideoSurfaceYUY2)
  790. {
  791. INITDDSTRUCT(fx);
  792. fx.dwFillColor = 0x80108010;
  793. m_pVideoSurfaceYUY2->Blt(NULL, NULL, NULL, DDBLT_WAIT|DDBLT_COLORFILL, &fx);
  794. }
  795. return __super::AllocSurfaces();
  796. }
  797. void CRM7AllocatorPresenter::DeleteSurfaces()
  798. {
  799.     CAutoLock cAutoLock(this);
  800. m_pVideoSurfaceOff = NULL;
  801. m_pVideoSurfaceYUY2 = NULL;
  802. __super::DeleteSurfaces();
  803. }
  804. // IRMAVideoSurface
  805. STDMETHODIMP CRM7AllocatorPresenter::Blt(UCHAR* pImageData, RMABitmapInfoHeader* pBitmapInfo, REF(PNxRect) inDestRect, REF(PNxRect) inSrcRect)
  806. {
  807. if(!m_pVideoSurface || !m_pVideoSurfaceOff)
  808. return E_FAIL;
  809. bool fRGB = false;
  810. bool fYUY2 = false;
  811. CRect src((RECT*)&inSrcRect), dst((RECT*)&inDestRect), src2(CPoint(0,0), src.Size());
  812. if(src.Width() > dst.Width() || src.Height() > dst.Height())
  813. return E_FAIL;
  814. DDSURFACEDESC2 ddsd;
  815. if(pBitmapInfo->biCompression == '024I')
  816. {
  817. DWORD pitch = pBitmapInfo->biWidth;
  818. DWORD size = pitch*abs(pBitmapInfo->biHeight);
  819. BYTE* y = pImageData + src.top*pitch + src.left;
  820. BYTE* u = pImageData + size + src.top*(pitch/2) + src.left/2;
  821. BYTE* v = pImageData + size + size/4 + src.top*(pitch/2) + src.left/2;
  822. if(m_pVideoSurfaceYUY2)
  823. {
  824. INITDDSTRUCT(ddsd);
  825. if(SUCCEEDED(m_pVideoSurfaceYUY2->Lock(src2, &ddsd, DDLOCK_WAIT|DDLOCK_SURFACEMEMORYPTR|DDLOCK_WRITEONLY, NULL)))
  826. {
  827. BitBltFromI420ToYUY2(src.Width(), src.Height(), (BYTE*)ddsd.lpSurface, ddsd.lPitch, y, u, v, pitch);
  828. m_pVideoSurfaceYUY2->Unlock(src2);
  829. fYUY2 = true;
  830. }
  831. }
  832. else
  833. {
  834. INITDDSTRUCT(ddsd);
  835. if(SUCCEEDED(m_pVideoSurfaceOff->Lock(src2, &ddsd, DDLOCK_WAIT|DDLOCK_SURFACEMEMORYPTR|DDLOCK_WRITEONLY, NULL)))
  836. {
  837. BitBltFromI420ToRGB(src.Width(), src.Height(), (BYTE*)ddsd.lpSurface, ddsd.lPitch, ddsd.ddpfPixelFormat.dwRGBBitCount, y, u, v, pitch);
  838. m_pVideoSurfaceOff->Unlock(src2);
  839. fRGB = true;
  840. }
  841. }
  842. }
  843. else if(pBitmapInfo->biCompression == '2YUY')
  844. {
  845. DWORD w = pBitmapInfo->biWidth;
  846. DWORD h = abs(pBitmapInfo->biHeight);
  847. DWORD pitch = pBitmapInfo->biWidth*2;
  848. BYTE* yvyu = pImageData + src.top*pitch + src.left*2;
  849. if(m_pVideoSurfaceYUY2)
  850. {
  851. INITDDSTRUCT(ddsd);
  852. if(SUCCEEDED(m_pVideoSurfaceYUY2->Lock(src2, &ddsd, DDLOCK_WAIT|DDLOCK_SURFACEMEMORYPTR|DDLOCK_WRITEONLY, NULL)))
  853. {
  854. BitBltFromYUY2ToYUY2(src.Width(), src.Height(), (BYTE*)ddsd.lpSurface, ddsd.lPitch, yvyu, pitch);
  855. m_pVideoSurfaceYUY2->Unlock(src2);
  856. fYUY2 = true;
  857. }
  858. }
  859. else
  860. {
  861. INITDDSTRUCT(ddsd);
  862. if(SUCCEEDED(m_pVideoSurfaceOff->Lock(src2, &ddsd, DDLOCK_WAIT|DDLOCK_SURFACEMEMORYPTR|DDLOCK_WRITEONLY, NULL)))
  863. {
  864. BitBltFromYUY2ToRGB(src.Width(), src.Height(), (BYTE*)ddsd.lpSurface, ddsd.lPitch, ddsd.ddpfPixelFormat.dwRGBBitCount, yvyu, pitch);
  865. m_pVideoSurfaceOff->Unlock(src2);
  866. fRGB = true;
  867. }
  868. }
  869. }
  870. else if(pBitmapInfo->biCompression == 0 || pBitmapInfo->biCompression == 3
  871.  || pBitmapInfo->biCompression == 'BGRA')
  872. {
  873. DWORD w = pBitmapInfo->biWidth;
  874. DWORD h = abs(pBitmapInfo->biHeight);
  875. DWORD pitch = pBitmapInfo->biWidth*pBitmapInfo->biBitCount>>3;
  876. BYTE* rgb = pImageData + src.top*pitch + src.left*(pBitmapInfo->biBitCount>>3);
  877. INITDDSTRUCT(ddsd);
  878. if(SUCCEEDED(m_pVideoSurfaceOff->Lock(src2, &ddsd, DDLOCK_WAIT|DDLOCK_SURFACEMEMORYPTR|DDLOCK_WRITEONLY, NULL)))
  879. {
  880. BYTE* lpSurface = (BYTE*)ddsd.lpSurface;
  881. if(pBitmapInfo->biHeight > 0) {lpSurface += ddsd.lPitch*(src.Height()-1); ddsd.lPitch = -ddsd.lPitch;}
  882. BitBltFromRGBToRGB(src.Width(), src.Height(), lpSurface, ddsd.lPitch, ddsd.ddpfPixelFormat.dwRGBBitCount, rgb, pitch, pBitmapInfo->biBitCount);
  883. fRGB = true;
  884. m_pVideoSurfaceOff->Unlock(src2);
  885. }
  886. }
  887. if(!fRGB && !fYUY2)
  888. {
  889. DDBLTFX fx;
  890. INITDDSTRUCT(fx);
  891. fx.dwFillColor = 0;
  892. m_pVideoSurfaceOff->Blt(NULL, NULL, NULL, DDBLT_WAIT|DDBLT_COLORFILL, &fx);
  893. HDC hDC;
  894. if(SUCCEEDED(m_pVideoSurfaceOff->GetDC(&hDC)))
  895. {
  896. CString str;
  897. str.Format(_T("Sorry, this format is not supported"));
  898. SetBkColor(hDC, 0);
  899. SetTextColor(hDC, 0x404040);
  900. TextOut(hDC, 10, 10, str, str.GetLength());
  901. m_pVideoSurfaceOff->ReleaseDC(hDC);
  902. fRGB = true;
  903. }
  904. }
  905. HRESULT hr;
  906. if(fRGB)
  907. hr = m_pVideoSurface->Blt(dst, m_pVideoSurfaceOff, src2, DDBLT_WAIT, NULL);
  908. if(fYUY2)
  909. hr = m_pVideoSurface->Blt(dst, m_pVideoSurfaceYUY2, src2, DDBLT_WAIT, NULL);
  910.     
  911. Paint(true);
  912. return PNR_OK;
  913. }
  914. STDMETHODIMP CRM7AllocatorPresenter::BeginOptimizedBlt(RMABitmapInfoHeader* pBitmapInfo)
  915. {
  916.     CAutoLock cAutoLock(this);
  917. DeleteSurfaces();
  918. m_NativeVideoSize = m_AspectRatio = CSize(pBitmapInfo->biWidth, abs(pBitmapInfo->biHeight));
  919. if(FAILED(AllocSurfaces())) return E_FAIL;
  920. return PNR_NOTIMPL;
  921. }
  922. STDMETHODIMP CRM7AllocatorPresenter::OptimizedBlt(UCHAR* pImageBits, REF(PNxRect) rDestRect, REF(PNxRect) rSrcRect)
  923. {
  924. return PNR_NOTIMPL;
  925. }
  926. STDMETHODIMP CRM7AllocatorPresenter::EndOptimizedBlt()
  927. {
  928. return PNR_NOTIMPL;
  929. }
  930. STDMETHODIMP CRM7AllocatorPresenter::GetOptimizedFormat(REF(RMA_COMPRESSION_TYPE) ulType)
  931. {
  932. return PNR_NOTIMPL;
  933. }
  934. STDMETHODIMP CRM7AllocatorPresenter::GetPreferredFormat(REF(RMA_COMPRESSION_TYPE) ulType)
  935. {
  936. ulType = RMA_I420;
  937. return PNR_OK;
  938. }
  939. //
  940. // CQT7AllocatorPresenter
  941. //
  942. CQT7AllocatorPresenter::CQT7AllocatorPresenter(HWND hWnd, HRESULT& hr) 
  943. : CDX7AllocatorPresenter(hWnd, hr)
  944. {
  945. }
  946. STDMETHODIMP CQT7AllocatorPresenter::NonDelegatingQueryInterface(REFIID riid, void** ppv)
  947. {
  948.     CheckPointer(ppv, E_POINTER);
  949. return 
  950. QI(IQTVideoSurface)
  951. __super::NonDelegatingQueryInterface(riid, ppv);
  952. }
  953. HRESULT CQT7AllocatorPresenter::AllocSurfaces()
  954. {
  955.     CAutoLock cAutoLock(this);
  956. m_pVideoSurfaceOff = NULL;
  957. DDSURFACEDESC2 ddsd;
  958. INITDDSTRUCT(ddsd);
  959. ddsd.dwFlags = DDSD_CAPS|DDSD_WIDTH|DDSD_HEIGHT|DDSD_PIXELFORMAT;
  960. ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
  961. ddsd.dwWidth = m_NativeVideoSize.cx;
  962. ddsd.dwHeight = m_NativeVideoSize.cy;
  963. ddsd.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
  964. ddsd.ddpfPixelFormat.dwFlags = DDPF_RGB;
  965. ddsd.ddpfPixelFormat.dwRGBBitCount = 32;
  966. ddsd.ddpfPixelFormat.dwRGBAlphaBitMask = 0xFF000000;
  967. ddsd.ddpfPixelFormat.dwRBitMask = 0x00FF0000;
  968. ddsd.ddpfPixelFormat.dwGBitMask = 0x0000FF00;
  969. ddsd.ddpfPixelFormat.dwBBitMask = 0x000000FF;
  970. HRESULT hr = m_pDD->CreateSurface(&ddsd, &m_pVideoSurfaceOff, NULL);
  971. if(FAILED(hr)) return E_FAIL;
  972. DDBLTFX fx;
  973. INITDDSTRUCT(fx);
  974. fx.dwFillColor = 0;
  975. m_pVideoSurfaceOff->Blt(NULL, NULL, NULL, DDBLT_WAIT|DDBLT_COLORFILL, &fx);
  976. return __super::AllocSurfaces();
  977. }
  978. void CQT7AllocatorPresenter::DeleteSurfaces()
  979. {
  980.     CAutoLock cAutoLock(this);
  981. m_pVideoSurfaceOff = NULL;
  982. __super::DeleteSurfaces();
  983. }
  984. // IQTVideoSurface
  985. STDMETHODIMP CQT7AllocatorPresenter::BeginBlt(const BITMAP& bm)
  986. {
  987.     CAutoLock cAutoLock(this);
  988. DeleteSurfaces();
  989. m_NativeVideoSize = m_AspectRatio = CSize(bm.bmWidth, abs(bm.bmHeight));
  990. HRESULT hr;
  991. if(FAILED(hr = AllocSurfaces()))
  992. return hr;
  993. return S_OK;
  994. }
  995. STDMETHODIMP CQT7AllocatorPresenter::DoBlt(const BITMAP& bm)
  996. {
  997. if(!m_pVideoSurface || !m_pVideoSurfaceOff)
  998. return E_FAIL;
  999. bool fOk = false;
  1000. DDSURFACEDESC2 ddsd;
  1001. INITDDSTRUCT(ddsd);
  1002. if(FAILED(m_pVideoSurfaceOff->GetSurfaceDesc(&ddsd)))
  1003. return E_FAIL;
  1004. int w = bm.bmWidth;
  1005. int h = abs(bm.bmHeight);
  1006. int bpp = bm.bmBitsPixel;
  1007. if((bpp == 16 || bpp == 24 || bpp == 32) && w == ddsd.dwWidth && h == ddsd.dwHeight)
  1008. {
  1009. INITDDSTRUCT(ddsd);
  1010. if(SUCCEEDED(m_pVideoSurfaceOff->Lock(NULL, &ddsd, DDLOCK_WAIT|DDLOCK_SURFACEMEMORYPTR|DDLOCK_WRITEONLY, NULL)))
  1011. {
  1012. BitBltFromRGBToRGB(
  1013. w, h, 
  1014. (BYTE*)ddsd.lpSurface, ddsd.lPitch, ddsd.ddpfPixelFormat.dwRGBBitCount,
  1015. (BYTE*)bm.bmBits, bm.bmWidthBytes, bm.bmBitsPixel);
  1016. m_pVideoSurfaceOff->Unlock(NULL);
  1017. fOk = true;
  1018. }
  1019. }
  1020. if(!fOk)
  1021. {
  1022. DDBLTFX fx;
  1023. INITDDSTRUCT(fx);
  1024. fx.dwFillColor = 0;
  1025. m_pVideoSurfaceOff->Blt(NULL, NULL, NULL, DDBLT_WAIT|DDBLT_COLORFILL, &fx);
  1026. HDC hDC;
  1027. if(SUCCEEDED(m_pVideoSurfaceOff->GetDC(&hDC)))
  1028. {
  1029. CString str;
  1030. str.Format(_T("Sorry, this format is not supported"));
  1031. SetBkColor(hDC, 0);
  1032. SetTextColor(hDC, 0x404040);
  1033. TextOut(hDC, 10, 10, str, str.GetLength());
  1034. m_pVideoSurfaceOff->ReleaseDC(hDC);
  1035. }
  1036. }
  1037. m_pVideoSurface->Blt(NULL, m_pVideoSurfaceOff, NULL, DDBLT_WAIT, NULL);
  1038. Paint(true);
  1039. return S_OK;
  1040. }