xmvcodec.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #ifdef _XBOX
  2. #include "xmvcodec.h"
  3. #include "..\Render.h"
  4. #include "..\mediaplayer\XBoxMediaPlayer.h"
  5. XMVCodec::XMVCodec(IRender* _pRender,bool _useStreaming) : IVideoCodec(_pRender)
  6. {
  7. pPlayer = 0;
  8. SetViewPosition(0.f,0.f,1.f,1.f,false);
  9. useStreaming = _useStreaming;
  10. bPausePlay = false;
  11. }
  12. XMVCodec::~XMVCodec()
  13. {
  14. CloseFile();
  15. }
  16. bool XMVCodec::OpenFile(const char* pcVideoFile,bool bLoopPlay)
  17. {
  18. CloseFile();
  19. pPlayer = GetRender()->GetMediaPlayer(pcVideoFile,bLoopPlay,useStreaming);
  20. if( pPlayer==NULL )
  21. return false;
  22. UpdateViewPosition();
  23. return true;
  24. }
  25. bool XMVCodec::Frame(bool bPause)
  26. {
  27. if( pPlayer )
  28. {
  29. if( useStreaming && bPausePlay != (IsPause() || bPause) )
  30. {
  31. bPausePlay = !bPausePlay;
  32. pPlayer->Pause(bPausePlay);
  33. }
  34. UpdateViewPosition();
  35. //NGRender::pRS->D3D()->Clear(0, NULL, D3DCLEAR_TARGET, rand(), 0, 0x0);
  36. DWORD dwSavedColorWrite;
  37. DWORD dwSavedFillMode;
  38. DWORD dwSavedAlphaBlend;
  39. DWORD dwSavedAlphaTest;
  40. NGRender::pRS->D3D()->GetRenderState(D3DRS_COLORWRITEENABLE, &dwSavedColorWrite);
  41. NGRender::pRS->D3D()->GetRenderState(D3DRS_FILLMODE, &dwSavedFillMode);
  42. NGRender::pRS->D3D()->GetRenderState(D3DRS_ALPHABLENDENABLE, &dwSavedAlphaBlend);
  43. NGRender::pRS->D3D()->GetRenderState(D3DRS_ALPHATESTENABLE, &dwSavedAlphaTest);
  44. NGRender::pRS->D3D()->SetRenderState(D3DRS_COLORWRITEENABLE, 0x0000000F);
  45. NGRender::pRS->D3D()->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
  46. NGRender::pRS->D3D()->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
  47. NGRender::pRS->D3D()->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
  48. IMediaPlayer::MPResultCode rc = pPlayer->RenderNextFrame();
  49. // wait till async audio stream reading
  50. //while( rc == IMediaPlayer::MP_NO_DATA )
  51. // rc = pPlayer->RenderNextFrame();
  52. if( rc == IMediaPlayer::MP_EOF )
  53. {
  54. CloseFile();
  55. }
  56. NGRender::pRS->D3D()->SetRenderState(D3DRS_COLORWRITEENABLE, dwSavedColorWrite);
  57. NGRender::pRS->D3D()->SetRenderState(D3DRS_FILLMODE, dwSavedFillMode);
  58. NGRender::pRS->D3D()->SetRenderState(D3DRS_ALPHABLENDENABLE, dwSavedAlphaBlend);
  59. NGRender::pRS->D3D()->SetRenderState(D3DRS_ALPHATESTENABLE, dwSavedAlphaTest);
  60. }
  61. return pPlayer!=0;
  62. }
  63. void XMVCodec::CloseFile()
  64. {
  65. Stop();
  66. if( pPlayer )
  67. {
  68. pPlayer->Release();
  69. pPlayer = 0;
  70. }
  71. bPausePlay = false;
  72. }
  73. void XMVCodec::SetViewPosition(float fx, float fy, float fw, float fh, bool bModifyAspect)
  74. {
  75. IRender* pRS = GetRender();
  76. float fAspect = 1.f;
  77. if( bModifyAspect )
  78. {
  79. float cx = (float)pRS->GetFullScreenViewPort_2D().Width;
  80. float cy = (float)pRS->GetFullScreenViewPort_2D().Height;
  81. float scr_aspect = cx / cy;
  82. const float def_aspect = 16.0f / 9.0f;
  83. fAspect = def_aspect / scr_aspect * pRS->GetWideScreenAspectWidthMultipler();
  84. }
  85. float fWidthMultipler = fAspect;
  86. float fHeightMultipler = 1.f;
  87. viewPos.left = (fx - 0.5f) * fWidthMultipler + 0.5f;
  88. viewPos.top = (fy - 0.5f) * fHeightMultipler + 0.5f;
  89. viewPos.right = (fx + fw - 0.5f) * fWidthMultipler + 0.5f;
  90. viewPos.bottom = (fy + fh - 0.5f) * fHeightMultipler + 0.5f;
  91. //SetUV();
  92. float uvl=0.f;
  93. float uvr=1.f;
  94. float uvt=0.f;
  95. float uvb=1.f;
  96. float fvpw = viewPos.right - viewPos.left;
  97. float fvph = viewPos.bottom - viewPos.top;
  98. // правим UV по левой стороне
  99. if( viewPos.left < 0.f ) {
  100. uvl = -viewPos.left / fvpw;
  101. viewPos.left = 0.f;
  102. }
  103. if( viewPos.left > 1.f ) {
  104. uvl = (viewPos.left - 1.f) / fvpw;
  105. viewPos.left = 1.f;
  106. }
  107. // правим UV по верху
  108. if( viewPos.top < 0.f ) {
  109. uvt = -viewPos.top / fvph;
  110. viewPos.top = 0.f;
  111. }
  112. if( viewPos.top > 1.f ) {
  113. uvt = 1.f - (viewPos.top - 1.f) / fvph;
  114. viewPos.top = 1.f;
  115. }
  116. // правим UV по правой стороне
  117. if( viewPos.right > 1.f ) {
  118. uvr = 1.f - (viewPos.right - 1.f) / fvpw;
  119. viewPos.right = 1.f;
  120. }
  121. if( viewPos.right < 0.f ) {
  122. uvr = -viewPos.right / fvpw;
  123. viewPos.right = 0.f;
  124. }
  125. // правим UV по низу
  126. if( viewPos.bottom > 1.f ) {
  127. uvb = 1.f - (viewPos.bottom - 1.f) / fvph;
  128. viewPos.bottom = 1.f;
  129. }
  130. if( viewPos.bottom < 0.f ) {
  131. uvb = -viewPos.bottom / fvph;
  132. viewPos.bottom = 0.f;
  133. }
  134. if( pPlayer )
  135. ((XBoxMediaPlayer*)pPlayer)->SetUV(uvl,uvt,uvr,uvb);
  136. UpdateViewPosition();
  137. }
  138. void XMVCodec::UpdateViewPosition()
  139. {
  140. if( !pPlayer ) return;
  141. //RENDERSCREEN & scr = GetHostObj()->Render().GetScreenInfo();
  142. const RENDERVIEWPORT & vprt = GetRender()->GetViewport();
  143. RECT r;
  144. r.left = vprt.X + (long)(viewPos.left * vprt.Width);
  145. r.top = vprt.Y + (long)(viewPos.top * vprt.Height);
  146. r.right = vprt.X + (long)(viewPos.right * vprt.Width);
  147. r.bottom = vprt.Y + (long)(viewPos.bottom * vprt.Height);
  148. pPlayer->SetRectangle(r);
  149. }
  150. void XMVCodec::Stop()
  151. {
  152. //CloseFile();
  153. //if( pPlayer )
  154. // pPlayer->Stop();
  155. IVideoCodec::Stop();
  156. }
  157. #endif