Welcome to mirror list, hosted at ThFree Co, Russian Federation.

VideoDeckLink.h « VideoTexture « gameengine « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 50099d2ead47a95f00d6e971479e67fc1fdce4b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
 * ***** BEGIN GPL LICENSE BLOCK *****
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * The Original Code is Copyright (C) 2015, Blender Foundation
 * All rights reserved.
 *
 * The Original Code is: all of this file.
 *
 * Contributor(s): Blender Foundation.
 *
 * ***** END GPL LICENSE BLOCK *****
 */

/** \file VideoDeckLink.h
 *  \ingroup bgevideotex
 */

#ifndef __VIDEODECKLINK_H__
#define __VIDEODECKLINK_H__

#ifdef WITH_GAMEENGINE_DECKLINK

/* this needs to be parsed with __cplusplus defined before included through DeckLink_compat.h */
#if defined(__FreeBSD__)
#  include <inttypes.h>
#endif
#include <map>
#include <set>

extern "C" {
#include <pthread.h>
#include "DNA_listBase.h"
#include "BLI_threads.h"
#include "BLI_blenlib.h"
}
#include "GL/glew.h"
#ifdef WIN32
#include "dvpapi.h"
#endif
#include "DeckLinkAPI.h"
#include "VideoBase.h"

class PinnedMemoryAllocator;

struct TextureDesc
{
	uint32_t	width;
	uint32_t	height;
	uint32_t	stride;
	uint32_t	size;
	GLenum		internalFormat;
	GLenum		format;
	GLenum		type;
	TextureDesc()
	{
		width = 0;
		height = 0;
		stride = 0;
		size = 0;
		internalFormat = 0;
		format = 0;
		type = 0;
	}
};

class CaptureDelegate;

// type VideoDeckLink declaration
class VideoDeckLink : public VideoBase
{
	friend class CaptureDelegate;
public:
	/// constructor
	VideoDeckLink (HRESULT * hRslt);
	/// destructor
	virtual ~VideoDeckLink ();

	/// open video/image file
	virtual void openFile(char *file);
	/// open video capture device
	virtual void openCam(char *driver, short camIdx);

	/// release video source
	virtual bool release (void);
	/// overwrite base refresh to handle fixed image
	virtual void refresh(void);
	/// play video
	virtual bool play (void);
	/// pause video
	virtual bool pause (void);
	/// stop video
	virtual bool stop (void);
	/// set play range
	virtual void setRange (double start, double stop);
	/// set frame rate
	virtual void setFrameRate (float rate);

protected:
	// format and codec information
	/// image calculation
	virtual void calcImage (unsigned int texId, double ts);

private:
	void					VideoFrameArrived(IDeckLinkVideoInputFrame* inputFrame);
	void					LockCache()
	{
		pthread_mutex_lock(&mCacheMutex);
	}
	void					UnlockCache()
	{
		pthread_mutex_unlock(&mCacheMutex);
	}

	IDeckLinkInput*			mDLInput;
	BMDDisplayMode			mDisplayMode;
	BMDPixelFormat			mPixelFormat;
	bool					mUse3D;
	uint32_t				mFrameWidth;
	uint32_t				mFrameHeight;
	TextureDesc				mTextureDesc;
	PinnedMemoryAllocator*	mpAllocator;
	CaptureDelegate*		mpCaptureDelegate;

	// cache frame in transit between the callback thread and the main BGE thread
	// keep only one frame in cache because we just want to keep up with real time
	pthread_mutex_t			mCacheMutex;
	IDeckLinkVideoInputFrame* mpCacheFrame;
	bool					mClosing;

};

inline VideoDeckLink *getDeckLink(PyImage *self)
{
	return static_cast<VideoDeckLink*>(self->m_image);
}

////////////////////////////////////////////
// TextureTransfer : Abstract class to perform a transfer to GPU memory using fast transfer if available
////////////////////////////////////////////
class TextureTransfer
{
public:
	TextureTransfer() {}
	virtual ~TextureTransfer() { }

	virtual void PerformTransfer() = 0;
protected:
	static bool _PinBuffer(void *address, uint32_t size);
	static void _UnpinBuffer(void* address, uint32_t size);
};

////////////////////////////////////////////
// PinnedMemoryAllocator
////////////////////////////////////////////

// PinnedMemoryAllocator implements the IDeckLinkMemoryAllocator interface and can be used instead of the
// built-in frame allocator, by setting with SetVideoInputFrameMemoryAllocator() or SetVideoOutputFrameMemoryAllocator().
//
// For this sample application a custom frame memory allocator is used to ensure each address
// of frame memory is aligned on a 4kB boundary required by the OpenGL pinned memory extension.
// If the pinned memory extension is not available, this allocator will still be used and
// demonstrates how to cache frame allocations for efficiency.
//
// The frame cache delays the releasing of buffers until the cache fills up, thereby avoiding an
// allocate plus pin operation for every frame, followed by an unpin and deallocate on every frame.


class PinnedMemoryAllocator : public IDeckLinkMemoryAllocator
{
public:
	PinnedMemoryAllocator(unsigned cacheSize, size_t memSize);
	virtual ~PinnedMemoryAllocator();

	void TransferBuffer(void* address, TextureDesc* texDesc, GLuint texId);

	// IUnknown methods
	virtual HRESULT STDMETHODCALLTYPE	QueryInterface(REFIID iid, LPVOID *ppv);
	virtual ULONG STDMETHODCALLTYPE		AddRef(void);
	virtual ULONG STDMETHODCALLTYPE		Release(void);

	// IDeckLinkMemoryAllocator methods
	virtual HRESULT STDMETHODCALLTYPE	AllocateBuffer(dl_size_t bufferSize, void* *allocatedBuffer);
	virtual HRESULT STDMETHODCALLTYPE	ReleaseBuffer(void* buffer);
	virtual HRESULT STDMETHODCALLTYPE	Commit();
	virtual HRESULT STDMETHODCALLTYPE	Decommit();

private:
	static bool				mGPUDirectInitialized;
	static bool				mHasDvp;
	static bool				mHasAMDPinnedMemory;
	static size_t			mReservedProcessMemory;
	static bool ReserveMemory(size_t size);

	void Lock()
	{
		pthread_mutex_lock(&mMutex);
	}
	void Unlock()
	{
		pthread_mutex_unlock(&mMutex);
	}
	HRESULT _ReleaseBuffer(void* buffer);

	uint32_t							mRefCount;
	// protect the cache and the allocated map, 
	// not the pinnedBuffer map as it is only used from main thread
	pthread_mutex_t						mMutex;
	std::map<void*, uint32_t>			mAllocatedSize;
	std::vector<void*>					mBufferCache;
	std::map<void *, TextureTransfer*>	mPinnedBuffer;
#ifdef WIN32
	DVPBufferHandle						mDvpCaptureTextureHandle;
#endif
	// target texture in GPU
	GLuint								mTexId;
	uint32_t							mBufferCacheSize;
};

////////////////////////////////////////////
// Capture Delegate Class
////////////////////////////////////////////

class CaptureDelegate : public IDeckLinkInputCallback
{
	VideoDeckLink*	mpOwner;

public:
	CaptureDelegate(VideoDeckLink* pOwner);

	// IUnknown needs only a dummy implementation
	virtual HRESULT	STDMETHODCALLTYPE	QueryInterface(REFIID iid, LPVOID *ppv)	{ return E_NOINTERFACE; }
	virtual ULONG	STDMETHODCALLTYPE	AddRef()								{ return 1; }
	virtual ULONG	STDMETHODCALLTYPE	Release()								{ return 1; }

	virtual HRESULT STDMETHODCALLTYPE	VideoInputFrameArrived(IDeckLinkVideoInputFrame *videoFrame, IDeckLinkAudioInputPacket *audioPacket);
	virtual HRESULT	STDMETHODCALLTYPE	VideoInputFormatChanged(BMDVideoInputFormatChangedEvents notificationEvents, IDeckLinkDisplayMode *newDisplayMode, BMDDetectedVideoInputFormatFlags detectedSignalFlags);
};


#endif	/* WITH_GAMEENGINE_DECKLINK */

#endif  /* __VIDEODECKLINK_H__ */