Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

177 lines
8.6 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //-----------------------------------------------------------------------------
  3. // File: XMVHelper.h
  4. //
  5. // Desc: Definition of XMV playback helper class for playing XMVs in a number
  6. // of different ways.
  7. //
  8. // Hist: 2.7.03 - Created, based on work by Jeff Sullivan
  9. //
  10. // Copyright (c) Microsoft Corporation. All rights reserved.
  11. //-----------------------------------------------------------------------------
  12. #ifndef _XMVHELPER_H_
  13. #define _XMVHELPER_H_
  14. #include <xtl.h>
  15. #include <xmv.h>
  16. // Number of textures to allocate for decoding. A larger number may help to
  17. // smooth out any frame rate variations if you are decoding to a texture.
  18. // When decoding to overlays two textures are always used.
  19. const DWORD XMVPLAYER_NUMTEXTURES = 2;
  20. //-----------------------------------------------------------------------------
  21. // Name: LOAD_CONTEXT
  22. // Desc: This structure is used to hold information used by the packet callbacks.
  23. //-----------------------------------------------------------------------------
  24. struct LOAD_CONTEXT
  25. {
  26. // Memory available for loading packets - maximum packet size.
  27. DWORD dwPacketSize;
  28. // Packet that XMV was decoding out of, and might still be, that
  29. // we want to load into as soon as we can.
  30. BYTE* pPendingReleasePacket;
  31. // Packet that XMV is decoding out of.
  32. BYTE* pDecodingPacket;
  33. // Packet we are currently loading into.
  34. BYTE* pLoadingPacket;
  35. // Information for when reading packets out of a file.
  36. // Handle to the file we are reading from.
  37. HANDLE hFile;
  38. // Overlapped structure to control asynchronous reading.
  39. OVERLAPPED Overlapped;
  40. // Information for when copying packets out of a memory buffer.
  41. // The size and location of the buffer we are reading from if we are
  42. // playing a movie from memory.
  43. BYTE* pInputBuffer;
  44. DWORD inputSize;
  45. // The offset in the memory buffer that we are currently reading from.
  46. DWORD readOffset;
  47. // The size of the packet that we have 'queued up' for reading. This
  48. // is a packet that we have warned about via ReleasePreviousMemoryPacket
  49. // but have not yet been asked to load.
  50. DWORD currentPacketSize;
  51. };
  52. class CXMVPlayer
  53. {
  54. public:
  55. CXMVPlayer();
  56. ~CXMVPlayer();
  57. // Open a .wmv file and prepare it for playing.
  58. // The format parameter specifies what type of texture it should be unpacked into ( if allocateTextures
  59. // is TRUE ). Only YUY2, LIN_X8R8G8B8 and LIN_A8R8G8B8 are supported as formats.
  60. // If you plan to play the movie with Play() or otherwise use overlays then format must be D3DFMT_YUY2
  61. // pDevice is needed in order to create textures.
  62. // allocateTextures should be TRUE if you plan to play the movie manually with AdvanceFrame.
  63. // It should be FALSE if you plan to play the movie with Play().
  64. // IsPlaying() will return TRUE if the movie opened successfully.
  65. HRESULT OpenFile( const CHAR* lpFilename, D3DFORMAT format, LPDIRECT3DDEVICE8 pDevice, BOOL bAllocateTextures );
  66. // Open a file using the packet interface. This method of opening a .wmv file can be easily
  67. // customized to read the data from game specific file formats. The GetNextPacket and
  68. // ReleasePreviousPacket functions from XMVHelper.cpp are used to read the packets. See above for
  69. // information about the other parameters.
  70. HRESULT OpenFileForPackets( const CHAR* lpFilename, D3DFORMAT format, LPDIRECT3DDEVICE8 pDevice, BOOL bAllocateTextures );
  71. // Open a block of memory for playing using the packet interface. This method of opening a .wmv file can be easily
  72. // customized to read the data from game specific file formats, or to retain the block of data in memory.
  73. // The GetNextMemoryPacket and ReleasePreviousMemoryPacket functions from XMVHelper.cpp are used to read
  74. // the packets. See above for information about the other parameters.
  75. HRESULT OpenMovieFromMemory( const CHAR* lpFilename, D3DFORMAT format, LPDIRECT3DDEVICE8 pDevice, BOOL bAllocateTextures );
  76. // Destroy frees up all the movie resources. IsPlaying() will return FALSE afterwards. Destroy()
  77. // can be called during movie playback, but is not thread safe.
  78. HRESULT Destroy();
  79. // The AdvanceFrame functions move to the next frame if it is time for that. Call this
  80. // function frequently - typically 30-60 times per second.
  81. // AdvanceFrameForTexturing returns the most recent frame decoded, for texturing.
  82. // It will return zero if the first frame has not been decoded, but otherwise
  83. // will always return a texture.
  84. LPDIRECT3DTEXTURE8 AdvanceFrameForTexturing( LPDIRECT3DDEVICE8 pDevice );
  85. // AdvanceFrameForOverlays returns newly decoded frames, for use as an overlays.
  86. // You MUST call UpdateOverlay() with the texture's surface.
  87. // It will return zero if there is not a *new* frame available.
  88. LPDIRECT3DTEXTURE8 AdvanceFrameForOverlays( LPDIRECT3DDEVICE8 pDevice );
  89. // Get information about the movie playing.
  90. DWORD GetWidth() const { return m_VideoDesc.Width; }
  91. DWORD GetHeight() const { return m_VideoDesc.Height; }
  92. DWORD GetCurrentFrame() const { return m_dwCurrentFrame; }
  93. DWORD GetElapsedTime() const { return m_bPlaying ? GetTickCount() - m_dwStartTime : 0; }
  94. // IsPlaying returns TRUE if a movie has been opened but has not ended or been destroyed.
  95. BOOL IsPlaying() const { return m_bPlaying; }
  96. BOOL IsFailed() const { return m_bError; }
  97. // Call TerminatePlayback on the decoder. It may take a few hundred ms before the movie stops.
  98. // This function is thread safe, as long as you ensure that you don't call Destroy() in another
  99. // thread simultaneously.
  100. void TerminatePlayback();
  101. HRESULT Play( DWORD Flags, RECT* pRect );
  102. // Call this to enable overlays if you will be playing the movie in an overlay using
  103. // AdvanceFrame. The overlays will be disabled when Destroy() is called.
  104. void EnableOverlays( LPDIRECT3DDEVICE8 pDevice );
  105. private:
  106. XMVDecoder* m_pXMVDecoder; // Pointer to the XMV decoder object.
  107. XMVVIDEO_DESC m_VideoDesc; // Description of the .xmv files video data.
  108. XMVAUDIO_DESC m_AudioDesc; // Description of the .xmv files audio data.
  109. LPDIRECT3DTEXTURE8 m_pTextures[XMVPLAYER_NUMTEXTURES]; // Textures to cycle through.
  110. DWORD m_nDecodeTextureIndex; // Index of the current texture to decode to.
  111. // These texture pointers are copies of the values in m_pTextures. They are used to track
  112. // the decode/display/pending status.
  113. // When displaying using textures pShowingTexture points to the texture to display,
  114. // pDecodingTexture points to the texture to decode into, and pSubmittedTexture is always
  115. // zero.
  116. // When displaying using overlays pShowingTexture points to the texture that the hardware
  117. // is currently displaying, pDecodingTexture points to the texture to decode into, and
  118. // pSubmittedTexture is a surface that has been submitted to the overlay system, but not
  119. // necessarily displayed yet. At any given time one of these is always zero. When
  120. // pDecodingTexture is zero that means that pShowingTexture might be being displayed, or
  121. // the hardware might have switched to pSubmittedTexture, we don't know yet.
  122. LPDIRECT3DTEXTURE8 pShowingTexture;
  123. LPDIRECT3DTEXTURE8 pDecodingTexture;
  124. LPDIRECT3DTEXTURE8 pSubmittedTexture;
  125. int m_dwCurrentFrame; // Current frame number - minus one if not yet started.
  126. DWORD m_dwStartTime;
  127. BOOL m_bPlaying; // Is a movie currently playing?
  128. BOOL m_bOverlaysEnabled; // Are overlays enabled? For disabling them on destroy.
  129. LPDIRECT3DDEVICE8 m_pDevice; // For use by Destroy to disable overlays.
  130. BOOL m_bError;
  131. LOAD_CONTEXT m_loadContext; // Data for communicating with packet callback functions.
  132. BYTE* m_physicalBuffer; // Pointer to block of physical memory used for loading packets.
  133. // Helper function for opening files.
  134. HRESULT FinishOpeningFile( D3DFORMAT Format, LPDIRECT3DDEVICE8 pDevice, BOOL bAllocateTextures );
  135. // Copy constructor and assignment operator are private and unimplemented
  136. // to prevent unintentional, unsupported, and disastrous copying.
  137. CXMVPlayer( const CXMVPlayer& source );
  138. CXMVPlayer& operator=( const CXMVPlayer& source );
  139. };
  140. #endif //#ifndef _XMVPLAYER_H_