VideoDeckLink.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * ***** BEGIN GPL LICENSE BLOCK *****
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software Foundation,
  16. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. *
  18. * The Original Code is Copyright (C) 2015, Blender Foundation
  19. * All rights reserved.
  20. *
  21. * The Original Code is: all of this file.
  22. *
  23. * Contributor(s): Blender Foundation.
  24. *
  25. * ***** END GPL LICENSE BLOCK *****
  26. */
  27. /** \file VideoDeckLink.h
  28. * \ingroup bgevideotex
  29. */
  30. #ifndef __VIDEODECKLINK_H__
  31. #define __VIDEODECKLINK_H__
  32. #ifdef WITH_GAMEENGINE_DECKLINK
  33. /* this needs to be parsed with __cplusplus defined before included through DeckLink_compat.h */
  34. #if defined(__FreeBSD__)
  35. # include <inttypes.h>
  36. #endif
  37. #include <map>
  38. #include <set>
  39. extern "C" {
  40. #include <pthread.h>
  41. #include "DNA_listBase.h"
  42. #include "BLI_threads.h"
  43. #include "BLI_blenlib.h"
  44. }
  45. #include "GPU_glew.h"
  46. #ifdef WIN32
  47. #include "dvpapi.h"
  48. #endif
  49. #include "DeckLinkAPI.h"
  50. #include "VideoBase.h"
  51. class PinnedMemoryAllocator;
  52. struct TextureDesc
  53. {
  54. uint32_t width;
  55. uint32_t height;
  56. uint32_t stride;
  57. uint32_t size;
  58. GLenum internalFormat;
  59. GLenum format;
  60. GLenum type;
  61. TextureDesc()
  62. {
  63. width = 0;
  64. height = 0;
  65. stride = 0;
  66. size = 0;
  67. internalFormat = 0;
  68. format = 0;
  69. type = 0;
  70. }
  71. };
  72. class CaptureDelegate;
  73. // type VideoDeckLink declaration
  74. class VideoDeckLink : public VideoBase
  75. {
  76. friend class CaptureDelegate;
  77. public:
  78. /// constructor
  79. VideoDeckLink (HRESULT * hRslt);
  80. /// destructor
  81. virtual ~VideoDeckLink ();
  82. /// open video/image file
  83. virtual void openFile(char *file);
  84. /// open video capture device
  85. virtual void openCam(char *driver, short camIdx);
  86. /// release video source
  87. virtual bool release (void);
  88. /// overwrite base refresh to handle fixed image
  89. virtual void refresh(void);
  90. /// play video
  91. virtual bool play (void);
  92. /// pause video
  93. virtual bool pause (void);
  94. /// stop video
  95. virtual bool stop (void);
  96. /// set play range
  97. virtual void setRange (double start, double stop);
  98. /// set frame rate
  99. virtual void setFrameRate (float rate);
  100. protected:
  101. // format and codec information
  102. /// image calculation
  103. virtual void calcImage (unsigned int texId, double ts);
  104. private:
  105. void VideoFrameArrived(IDeckLinkVideoInputFrame* inputFrame);
  106. void LockCache()
  107. {
  108. pthread_mutex_lock(&mCacheMutex);
  109. }
  110. void UnlockCache()
  111. {
  112. pthread_mutex_unlock(&mCacheMutex);
  113. }
  114. IDeckLinkInput* mDLInput;
  115. BMDDisplayMode mDisplayMode;
  116. BMDPixelFormat mPixelFormat;
  117. bool mUse3D;
  118. uint32_t mFrameWidth;
  119. uint32_t mFrameHeight;
  120. TextureDesc mTextureDesc;
  121. PinnedMemoryAllocator* mpAllocator;
  122. CaptureDelegate* mpCaptureDelegate;
  123. // cache frame in transit between the callback thread and the main BGE thread
  124. // keep only one frame in cache because we just want to keep up with real time
  125. pthread_mutex_t mCacheMutex;
  126. IDeckLinkVideoInputFrame* mpCacheFrame;
  127. bool mClosing;
  128. };
  129. inline VideoDeckLink *getDeckLink(PyImage *self)
  130. {
  131. return static_cast<VideoDeckLink*>(self->m_image);
  132. }
  133. ////////////////////////////////////////////
  134. // TextureTransfer : Abstract class to perform a transfer to GPU memory using fast transfer if available
  135. ////////////////////////////////////////////
  136. class TextureTransfer
  137. {
  138. public:
  139. TextureTransfer() {}
  140. virtual ~TextureTransfer() { }
  141. virtual void PerformTransfer() = 0;
  142. protected:
  143. static bool _PinBuffer(void *address, uint32_t size);
  144. static void _UnpinBuffer(void* address, uint32_t size);
  145. };
  146. ////////////////////////////////////////////
  147. // PinnedMemoryAllocator
  148. ////////////////////////////////////////////
  149. // PinnedMemoryAllocator implements the IDeckLinkMemoryAllocator interface and can be used instead of the
  150. // built-in frame allocator, by setting with SetVideoInputFrameMemoryAllocator() or SetVideoOutputFrameMemoryAllocator().
  151. //
  152. // For this sample application a custom frame memory allocator is used to ensure each address
  153. // of frame memory is aligned on a 4kB boundary required by the OpenGL pinned memory extension.
  154. // If the pinned memory extension is not available, this allocator will still be used and
  155. // demonstrates how to cache frame allocations for efficiency.
  156. //
  157. // The frame cache delays the releasing of buffers until the cache fills up, thereby avoiding an
  158. // allocate plus pin operation for every frame, followed by an unpin and deallocate on every frame.
  159. class PinnedMemoryAllocator : public IDeckLinkMemoryAllocator
  160. {
  161. public:
  162. PinnedMemoryAllocator(unsigned cacheSize, size_t memSize);
  163. virtual ~PinnedMemoryAllocator();
  164. void TransferBuffer(void* address, TextureDesc* texDesc, GLuint texId);
  165. // IUnknown methods
  166. virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, LPVOID *ppv);
  167. virtual ULONG STDMETHODCALLTYPE AddRef(void);
  168. virtual ULONG STDMETHODCALLTYPE Release(void);
  169. // IDeckLinkMemoryAllocator methods
  170. virtual HRESULT STDMETHODCALLTYPE AllocateBuffer(dl_size_t bufferSize, void* *allocatedBuffer);
  171. virtual HRESULT STDMETHODCALLTYPE ReleaseBuffer(void* buffer);
  172. virtual HRESULT STDMETHODCALLTYPE Commit();
  173. virtual HRESULT STDMETHODCALLTYPE Decommit();
  174. private:
  175. static bool mGPUDirectInitialized;
  176. static bool mHasDvp;
  177. static bool mHasAMDPinnedMemory;
  178. static size_t mReservedProcessMemory;
  179. static bool ReserveMemory(size_t size);
  180. void Lock()
  181. {
  182. pthread_mutex_lock(&mMutex);
  183. }
  184. void Unlock()
  185. {
  186. pthread_mutex_unlock(&mMutex);
  187. }
  188. HRESULT _ReleaseBuffer(void* buffer);
  189. uint32_t mRefCount;
  190. // protect the cache and the allocated map,
  191. // not the pinnedBuffer map as it is only used from main thread
  192. pthread_mutex_t mMutex;
  193. std::map<void*, uint32_t> mAllocatedSize;
  194. std::vector<void*> mBufferCache;
  195. std::map<void *, TextureTransfer*> mPinnedBuffer;
  196. #ifdef WIN32
  197. DVPBufferHandle mDvpCaptureTextureHandle;
  198. #endif
  199. // target texture in GPU
  200. GLuint mTexId;
  201. uint32_t mBufferCacheSize;
  202. };
  203. ////////////////////////////////////////////
  204. // Capture Delegate Class
  205. ////////////////////////////////////////////
  206. class CaptureDelegate : public IDeckLinkInputCallback
  207. {
  208. VideoDeckLink* mpOwner;
  209. public:
  210. CaptureDelegate(VideoDeckLink* pOwner);
  211. // IUnknown needs only a dummy implementation
  212. virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, LPVOID *ppv) { return E_NOINTERFACE; }
  213. virtual ULONG STDMETHODCALLTYPE AddRef() { return 1; }
  214. virtual ULONG STDMETHODCALLTYPE Release() { return 1; }
  215. virtual HRESULT STDMETHODCALLTYPE VideoInputFrameArrived(IDeckLinkVideoInputFrame *videoFrame, IDeckLinkAudioInputPacket *audioPacket);
  216. virtual HRESULT STDMETHODCALLTYPE VideoInputFormatChanged(BMDVideoInputFormatChangedEvents notificationEvents, IDeckLinkDisplayMode *newDisplayMode, BMDDetectedVideoInputFormatFlags detectedSignalFlags);
  217. };
  218. #endif /* WITH_GAMEENGINE_DECKLINK */
  219. #endif /* __VIDEODECKLINK_H__ */