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

VideoDeckLink.cpp « VideoTexture « gameengine « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c8d3c28c55127c85b5e887612540ba8e9a5e3f90 (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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
/*
 * ***** 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 gameengine/VideoTexture/VideoDeckLink.cpp
 *  \ingroup bgevideotex
 */

#ifdef WITH_GAMEENGINE_DECKLINK

// FFmpeg defines its own version of stdint.h on Windows.
// Decklink needs FFmpeg, so it uses its version of stdint.h
// this is necessary for INT64_C macro
#ifndef __STDC_CONSTANT_MACROS
#define __STDC_CONSTANT_MACROS
#endif
// this is necessary for UINTPTR_MAX (used by atomic-ops)
#ifndef __STDC_LIMIT_MACROS
#define __STDC_LIMIT_MACROS
#ifdef __STDC_LIMIT_MACROS  /* else it may be unused */
#endif
#endif
#include <stdint.h>
#include <string.h>
#ifndef WIN32
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/mman.h>
#endif

#include "atomic_ops.h"

#include "MEM_guardedalloc.h"
#include "PIL_time.h"
#include "VideoDeckLink.h"
#include "DeckLink.h"
#include "Exception.h"
#include "KX_KetsjiEngine.h"
#include "KX_PythonInit.h"

extern ExceptionID DeckLinkInternalError;
ExceptionID SourceVideoOnlyCapture, VideoDeckLinkBadFormat, VideoDeckLinkOpenCard, VideoDeckLinkDvpInternalError, VideoDeckLinkPinMemoryError;
ExpDesc SourceVideoOnlyCaptureDesc(SourceVideoOnlyCapture, "This video source only allows live capture");
ExpDesc VideoDeckLinkBadFormatDesc(VideoDeckLinkBadFormat, "Invalid or unsupported capture format, should be <mode>/<pixel>[/3D]");
ExpDesc VideoDeckLinkOpenCardDesc(VideoDeckLinkOpenCard, "Cannot open capture card, check if driver installed");
ExpDesc VideoDeckLinkDvpInternalErrorDesc(VideoDeckLinkDvpInternalError, "DVP API internal error, please report");
ExpDesc VideoDeckLinkPinMemoryErrorDesc(VideoDeckLinkPinMemoryError, "Error pinning memory");


#ifdef WIN32
////////////////////////////////////////////
// SynInfo
//
// Sets up a semaphore which is shared between the GPU and CPU and used to
// synchronise access to DVP buffers.
#define DVP_CHECK(cmd)	if ((cmd) != DVP_STATUS_OK) THRWEXCP(VideoDeckLinkDvpInternalError, S_OK)

struct SyncInfo
{
	SyncInfo(uint32_t semaphoreAllocSize, uint32_t semaphoreAddrAlignment)
	{
		mSemUnaligned = (uint32_t*)malloc(semaphoreAllocSize + semaphoreAddrAlignment - 1);

		// Apply alignment constraints
		uint64_t val = (uint64_t)mSemUnaligned;
		val += semaphoreAddrAlignment - 1;
		val &= ~((uint64_t)semaphoreAddrAlignment - 1);
		mSem = (uint32_t*)val;

		// Initialise
		mSem[0] = 0;
		mReleaseValue = 0;
		mAcquireValue = 0;

		// Setup DVP sync object and import it
		DVPSyncObjectDesc syncObjectDesc;
		syncObjectDesc.externalClientWaitFunc = NULL;
		syncObjectDesc.sem = (uint32_t*)mSem;

		DVP_CHECK(dvpImportSyncObject(&syncObjectDesc, &mDvpSync));

	}
	~SyncInfo()
	{
		dvpFreeSyncObject(mDvpSync);
		free((void*)mSemUnaligned);
	}

	volatile uint32_t*	mSem;
	volatile uint32_t*	mSemUnaligned;
	volatile uint32_t	mReleaseValue;
	volatile uint32_t	mAcquireValue;
	DVPSyncObjectHandle	mDvpSync;
};

////////////////////////////////////////////
// TextureTransferDvp: transfer with GPUDirect
////////////////////////////////////////////

class TextureTransferDvp : public TextureTransfer
{
public:
	TextureTransferDvp(DVPBufferHandle dvpTextureHandle, TextureDesc *pDesc, void *address, uint32_t allocatedSize)
	{
		DVPSysmemBufferDesc sysMemBuffersDesc;

		mExtSync = NULL;
		mGpuSync = NULL;
		mDvpSysMemHandle = 0;
		mDvpTextureHandle = 0;
		mTextureHeight = 0;
		mAllocatedSize = 0;
		mBuffer = NULL;

		if (!_PinBuffer(address, allocatedSize))
			THRWEXCP(VideoDeckLinkPinMemoryError, S_OK);
		mAllocatedSize = allocatedSize;
		mBuffer = address;

		try {
			if (!mBufferAddrAlignment) {
				DVP_CHECK(dvpGetRequiredConstantsGLCtx(&mBufferAddrAlignment, &mBufferGpuStrideAlignment,
					&mSemaphoreAddrAlignment, &mSemaphoreAllocSize,
					&mSemaphorePayloadOffset, &mSemaphorePayloadSize));
			}
			mExtSync = new SyncInfo(mSemaphoreAllocSize, mSemaphoreAddrAlignment);
			mGpuSync = new SyncInfo(mSemaphoreAllocSize, mSemaphoreAddrAlignment);
			sysMemBuffersDesc.width = pDesc->width;
			sysMemBuffersDesc.height = pDesc->height;
			sysMemBuffersDesc.stride = pDesc->stride;
			switch (pDesc->format) {
				case GL_RED_INTEGER:
					sysMemBuffersDesc.format = DVP_RED_INTEGER;
					break;
				default:
					sysMemBuffersDesc.format = DVP_BGRA;
					break;
			}
			switch (pDesc->type) {
				case GL_UNSIGNED_BYTE:
					sysMemBuffersDesc.type = DVP_UNSIGNED_BYTE;
					break;
				case GL_UNSIGNED_INT_2_10_10_10_REV:
					sysMemBuffersDesc.type = DVP_UNSIGNED_INT_2_10_10_10_REV;
					break;
				case GL_UNSIGNED_INT_8_8_8_8:
					sysMemBuffersDesc.type = DVP_UNSIGNED_INT_8_8_8_8;
					break;
				case GL_UNSIGNED_INT_10_10_10_2:
					sysMemBuffersDesc.type = DVP_UNSIGNED_INT_10_10_10_2;
					break;
				default:
					sysMemBuffersDesc.type = DVP_UNSIGNED_INT;
					break;
			}
			sysMemBuffersDesc.size = pDesc->width * pDesc->height * 4;
			sysMemBuffersDesc.bufAddr = mBuffer;
			DVP_CHECK(dvpCreateBuffer(&sysMemBuffersDesc, &mDvpSysMemHandle));
			DVP_CHECK(dvpBindToGLCtx(mDvpSysMemHandle));
			mDvpTextureHandle = dvpTextureHandle;
			mTextureHeight = pDesc->height;
		}
		catch (Exception &) {
			clean();
			throw;
		}
	}
	~TextureTransferDvp()
	{
		clean();
	}

	virtual void PerformTransfer()
	{
		// perform the transfer
		// tell DVP that the old texture buffer will no longer be used
		dvpMapBufferEndAPI(mDvpTextureHandle);
		// do we need this?
		mGpuSync->mReleaseValue++;
		dvpBegin();
		// Copy from system memory to GPU texture
		dvpMapBufferWaitDVP(mDvpTextureHandle);
		dvpMemcpyLined(mDvpSysMemHandle, mExtSync->mDvpSync, mExtSync->mAcquireValue, DVP_TIMEOUT_IGNORED,
			mDvpTextureHandle, mGpuSync->mDvpSync, mGpuSync->mReleaseValue, 0, mTextureHeight);
		dvpMapBufferEndDVP(mDvpTextureHandle);
		dvpEnd();
		dvpMapBufferWaitAPI(mDvpTextureHandle);
		// the transfer is now complete and the texture is ready for use
	}

private:
	static uint32_t			mBufferAddrAlignment;
	static uint32_t			mBufferGpuStrideAlignment;
	static uint32_t			mSemaphoreAddrAlignment;
	static uint32_t			mSemaphoreAllocSize;
	static uint32_t			mSemaphorePayloadOffset;
	static uint32_t			mSemaphorePayloadSize;

	void clean()
	{
		if (mDvpSysMemHandle) {
			dvpUnbindFromGLCtx(mDvpSysMemHandle);
			dvpDestroyBuffer(mDvpSysMemHandle);
		}
		if (mExtSync)
			delete mExtSync;
		if (mGpuSync)
			delete mGpuSync;
		if (mBuffer)
			_UnpinBuffer(mBuffer, mAllocatedSize);
	}
	SyncInfo*				mExtSync;
	SyncInfo*				mGpuSync;
	DVPBufferHandle			mDvpSysMemHandle;
	DVPBufferHandle			mDvpTextureHandle;
	uint32_t				mTextureHeight;
	uint32_t				mAllocatedSize;
	void*					mBuffer;
};

uint32_t	TextureTransferDvp::mBufferAddrAlignment;
uint32_t	TextureTransferDvp::mBufferGpuStrideAlignment;
uint32_t	TextureTransferDvp::mSemaphoreAddrAlignment;
uint32_t	TextureTransferDvp::mSemaphoreAllocSize;
uint32_t	TextureTransferDvp::mSemaphorePayloadOffset;
uint32_t	TextureTransferDvp::mSemaphorePayloadSize;

#endif

////////////////////////////////////////////
// TextureTransferOGL: transfer using standard OGL buffers
////////////////////////////////////////////

class TextureTransferOGL : public TextureTransfer
{
public:
	TextureTransferOGL(GLuint texId, TextureDesc *pDesc, void *address)
	{
		memcpy(&mDesc, pDesc, sizeof(mDesc));
		mTexId = texId;
		mBuffer = address;

		// as we cache transfer object, we will create one texture to hold the buffer
		glGenBuffers(1, &mUnpinnedTextureBuffer);
		// create a storage for it
		glBindBuffer(GL_PIXEL_UNPACK_BUFFER, mUnpinnedTextureBuffer);
		glBufferData(GL_PIXEL_UNPACK_BUFFER, pDesc->size, NULL, GL_DYNAMIC_DRAW);
		glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
	}
	~TextureTransferOGL()
	{
		glDeleteBuffers(1, &mUnpinnedTextureBuffer);
	}

	virtual void PerformTransfer()
	{
		glBindBuffer(GL_PIXEL_UNPACK_BUFFER, mUnpinnedTextureBuffer);
		glBufferSubData(GL_PIXEL_UNPACK_BUFFER, 0, mDesc.size, mBuffer);
		glBindTexture(GL_TEXTURE_2D, mTexId);
		// NULL for last arg indicates use current GL_PIXEL_UNPACK_BUFFER target as texture data
		glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, mDesc.width, mDesc.height, mDesc.format, mDesc.type, NULL);
		glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
	}
private:
	// intermediate texture to receive the buffer
	GLuint mUnpinnedTextureBuffer;
	// target texture to receive the image
	GLuint mTexId;
	// buffer
	void *mBuffer;
	// characteristic of the image
	TextureDesc mDesc;
};

////////////////////////////////////////////
// TextureTransferPMB: transfer using pinned memory buffer
////////////////////////////////////////////

class TextureTransferPMD : public TextureTransfer
{
public:
	TextureTransferPMD(GLuint texId, TextureDesc *pDesc, void *address, uint32_t allocatedSize)
	{
		memcpy(&mDesc, pDesc, sizeof(mDesc));
		mTexId = texId;
		mBuffer = address;
		mAllocatedSize = allocatedSize;

		_PinBuffer(address, allocatedSize);

		// as we cache transfer object, we will create one texture to hold the buffer
		glGenBuffers(1, &mPinnedTextureBuffer);
		// create a storage for it
		glBindBuffer(GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD, mPinnedTextureBuffer);
		glBufferData(GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD, pDesc->size, address, GL_STREAM_DRAW);
		glBindBuffer(GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD, 0);
	}
	~TextureTransferPMD()
	{
		glDeleteBuffers(1, &mPinnedTextureBuffer);
        if (mBuffer)
            _UnpinBuffer(mBuffer, mAllocatedSize);
    }

	virtual void PerformTransfer()
	{
		glBindBuffer(GL_PIXEL_UNPACK_BUFFER, mPinnedTextureBuffer);
		glBindTexture(GL_TEXTURE_2D, mTexId);
		// NULL for last arg indicates use current GL_PIXEL_UNPACK_BUFFER target as texture data
		glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, mDesc.width, mDesc.height, mDesc.format, mDesc.type, NULL);
		// wait for the trasnfer to complete
		GLsync fence = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
		glClientWaitSync(fence, GL_SYNC_FLUSH_COMMANDS_BIT, 40 * 1000 * 1000);	// timeout in nanosec
		glDeleteSync(fence);
		glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
	}
private:
	// intermediate texture to receive the buffer
	GLuint mPinnedTextureBuffer;
	// target texture to receive the image
	GLuint mTexId;
	// buffer
	void *mBuffer;
    // the allocated size
    uint32_t mAllocatedSize;
    // characteristic of the image
	TextureDesc mDesc;
};

bool TextureTransfer::_PinBuffer(void *address, uint32_t size)
{
#ifdef WIN32
	return VirtualLock(address, size);
#elif defined(_POSIX_MEMLOCK_RANGE)
    return !mlock(address, size);
#endif
}

void TextureTransfer::_UnpinBuffer(void* address, uint32_t size)
{
#ifdef WIN32
	VirtualUnlock(address, size);
#elif defined(_POSIX_MEMLOCK_RANGE)
    munlock(address, size);
#endif
}



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


// static members
bool		PinnedMemoryAllocator::mGPUDirectInitialized = false;
bool		PinnedMemoryAllocator::mHasDvp = false;
bool		PinnedMemoryAllocator::mHasAMDPinnedMemory = false;
size_t		PinnedMemoryAllocator::mReservedProcessMemory = 0;

bool PinnedMemoryAllocator::ReserveMemory(size_t size)
{
#ifdef WIN32
	// Increase the process working set size to allow pinning of memory.
	if (size <= mReservedProcessMemory)
		return true;
	SIZE_T dwMin = 0, dwMax = 0;
	HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_SET_QUOTA, FALSE, GetCurrentProcessId());
	if (!hProcess)
		return false;

	// Retrieve the working set size of the process.
	if (!dwMin && !GetProcessWorkingSetSize(hProcess, &dwMin, &dwMax))
		return false;

	BOOL res = SetProcessWorkingSetSize(hProcess, (size - mReservedProcessMemory) + dwMin, (size - mReservedProcessMemory) + dwMax);
	if (!res)
		return false;
	mReservedProcessMemory = size;
	CloseHandle(hProcess);
	return true;
#else
	struct rlimit rlim;
	if (getrlimit(RLIMIT_MEMLOCK, &rlim) == 0) {
		if (rlim.rlim_cur < size) {
			if (rlim.rlim_max < size)
				rlim.rlim_max = size;
			rlim.rlim_cur = size;
			return !setrlimit(RLIMIT_MEMLOCK, &rlim);
		}
	}
	return false;
#endif
}

PinnedMemoryAllocator::PinnedMemoryAllocator(unsigned cacheSize, size_t memSize) :
mRefCount(1U),
#ifdef WIN32
mDvpCaptureTextureHandle(0),
#endif
mTexId(0),
mBufferCacheSize(cacheSize)
{
	pthread_mutex_init(&mMutex, NULL);
	// do it once
	if (!mGPUDirectInitialized) {
#ifdef WIN32
		// In windows, AMD_pinned_memory option is not available, 
		// we must use special DVP API only available for Quadro cards
		const char* renderer = (const char *)glGetString(GL_RENDERER);
		mHasDvp = (strstr(renderer, "Quadro") != NULL);

		if (mHasDvp) {
			// In case the DLL is not in place, don't fail, just fallback on OpenGL
			if (dvpInitGLContext(DVP_DEVICE_FLAGS_SHARE_APP_CONTEXT) != DVP_STATUS_OK) {
				printf("Warning: Could not initialize DVP context, fallback on OpenGL transfer.\nInstall dvp.dll to take advantage of nVidia GPUDirect.\n");
				mHasDvp = false;
			}
		}
#endif
		if (GLEW_AMD_pinned_memory)
			mHasAMDPinnedMemory = true;

		mGPUDirectInitialized = true;
	}
	if (mHasDvp || mHasAMDPinnedMemory) {
		ReserveMemory(memSize);
	}
}

PinnedMemoryAllocator::~PinnedMemoryAllocator()
{
	void *address;
	// first clean the cache if not already done
	while (!mBufferCache.empty()) {
		address = mBufferCache.back();
		mBufferCache.pop_back();
		_ReleaseBuffer(address);
	}
	// clean preallocated buffers
	while (!mAllocatedSize.empty()) {
		address = mAllocatedSize.begin()->first;
		_ReleaseBuffer(address);
	}

#ifdef WIN32
	if (mDvpCaptureTextureHandle)
		dvpDestroyBuffer(mDvpCaptureTextureHandle);
#endif
}

void PinnedMemoryAllocator::TransferBuffer(void* address, TextureDesc* texDesc, GLuint texId)
{
	uint32_t allocatedSize = 0;
	TextureTransfer *pTransfer = NULL;

	Lock();
	if (mAllocatedSize.count(address) > 0)
		allocatedSize = mAllocatedSize[address];
	Unlock();
	if (!allocatedSize)
		// internal error!!
		return;
	if (mTexId != texId)
	{
		// first time we try to send data to the GPU, allocate a buffer for the texture
		glBindTexture(GL_TEXTURE_2D, texId);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
		glTexImage2D(GL_TEXTURE_2D, 0, texDesc->internalFormat, texDesc->width, texDesc->height, 0, texDesc->format, texDesc->type, NULL);
		glBindTexture(GL_TEXTURE_2D, 0);
		mTexId = texId;
	}
#ifdef WIN32
	if (mHasDvp)
	{
		if (!mDvpCaptureTextureHandle)
		{
			// bind DVP to the OGL texture
			DVP_CHECK(dvpCreateGPUTextureGL(texId, &mDvpCaptureTextureHandle));
		}
	}
#endif
	Lock();
	if (mPinnedBuffer.count(address) > 0)
	{
		pTransfer = mPinnedBuffer[address];
	}
	Unlock();
	if (!pTransfer)
	{
#ifdef WIN32
		if (mHasDvp)
			pTransfer = new TextureTransferDvp(mDvpCaptureTextureHandle, texDesc, address, allocatedSize);
		else
#endif
		if (mHasAMDPinnedMemory) {
			pTransfer = new TextureTransferPMD(texId, texDesc, address, allocatedSize);
		}
		else {
			pTransfer = new TextureTransferOGL(texId, texDesc, address);
		}
		if (pTransfer)
		{
			Lock();
			mPinnedBuffer[address] = pTransfer;
			Unlock();
		}
	}
	if (pTransfer)
		pTransfer->PerformTransfer();
}

// IUnknown methods
HRESULT STDMETHODCALLTYPE	PinnedMemoryAllocator::QueryInterface(REFIID /*iid*/, LPVOID* /*ppv*/)
{
	return E_NOTIMPL;
}

ULONG STDMETHODCALLTYPE		PinnedMemoryAllocator::AddRef(void)
{
	return atomic_add_uint32(&mRefCount, 1U);
}

ULONG STDMETHODCALLTYPE		PinnedMemoryAllocator::Release(void)
{
	uint32_t newCount = atomic_sub_uint32(&mRefCount, 1U);
	if (newCount == 0)
		delete this;
	return (ULONG)newCount;
}

// IDeckLinkMemoryAllocator methods
HRESULT STDMETHODCALLTYPE	PinnedMemoryAllocator::AllocateBuffer(dl_size_t bufferSize, void* *allocatedBuffer)
{
	Lock();
	if (mBufferCache.empty())
	{
		// Allocate memory on a page boundary
		// Note: aligned alloc exist in Blender but only for small alignment, use direct allocation then.
		// Note: the DeckLink API tries to allocate up to 65 buffer in advance, we will limit this to 3
		//       because we don't need any caching
		if (mAllocatedSize.size() >= mBufferCacheSize)
			*allocatedBuffer = NULL;
		else {
#ifdef WIN32
			*allocatedBuffer = VirtualAlloc(NULL, bufferSize, MEM_COMMIT | MEM_RESERVE | MEM_WRITE_WATCH, PAGE_READWRITE);
#else
			if (posix_memalign(allocatedBuffer, 4096, bufferSize) != 0)
				*allocatedBuffer = NULL;
#endif
			mAllocatedSize[*allocatedBuffer] = bufferSize;
		}
	}
	else {
		// Re-use most recently ReleaseBuffer'd address
		*allocatedBuffer = mBufferCache.back();
		mBufferCache.pop_back();
	}
	Unlock();
	return (*allocatedBuffer) ? S_OK : E_OUTOFMEMORY;
}

HRESULT STDMETHODCALLTYPE PinnedMemoryAllocator::ReleaseBuffer(void* buffer)
{
	HRESULT result = S_OK;
	Lock();
	if (mBufferCache.size() < mBufferCacheSize) {
		mBufferCache.push_back(buffer);
	}
	else {
		result = _ReleaseBuffer(buffer);
	}
	Unlock();
	return result;
}


HRESULT PinnedMemoryAllocator::_ReleaseBuffer(void* buffer)
{
	TextureTransfer *pTransfer;
	if (mAllocatedSize.count(buffer) == 0) {
		// Internal error!!
		return S_OK;
	}
	else {
		// No room left in cache, so un-pin (if it was pinned) and free this buffer
		if (mPinnedBuffer.count(buffer) > 0) {
			pTransfer = mPinnedBuffer[buffer];
			mPinnedBuffer.erase(buffer);
			delete pTransfer;
		}
#ifdef WIN32
		VirtualFree(buffer, 0, MEM_RELEASE);
#else
		free(buffer);
#endif
		mAllocatedSize.erase(buffer);
	}
	return S_OK;
}

HRESULT STDMETHODCALLTYPE	PinnedMemoryAllocator::Commit()
{
	return S_OK;
}

HRESULT STDMETHODCALLTYPE	PinnedMemoryAllocator::Decommit()
{
	void *buffer;
	Lock();
	while (!mBufferCache.empty()) {
		// Cleanup any frames allocated and pinned in AllocateBuffer() but not freed in ReleaseBuffer()
		buffer = mBufferCache.back();
		mBufferCache.pop_back();
		_ReleaseBuffer(buffer);
	}
	Unlock();
	return S_OK;
}


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

CaptureDelegate::CaptureDelegate(VideoDeckLink* pOwner) : mpOwner(pOwner)
{
}

HRESULT	CaptureDelegate::VideoInputFrameArrived(IDeckLinkVideoInputFrame* inputFrame, IDeckLinkAudioInputPacket* /*audioPacket*/)
{
	if (!inputFrame) {
		// It's possible to receive a NULL inputFrame, but a valid audioPacket. Ignore audio-only frame.
		return S_OK;
	}
	if ((inputFrame->GetFlags() & bmdFrameHasNoInputSource) == bmdFrameHasNoInputSource) {
		// let's not bother transferring frames if there is no source
		return S_OK;
	}
	mpOwner->VideoFrameArrived(inputFrame);
	return S_OK;
}

HRESULT	CaptureDelegate::VideoInputFormatChanged(BMDVideoInputFormatChangedEvents notificationEvents, IDeckLinkDisplayMode *newDisplayMode, BMDDetectedVideoInputFormatFlags detectedSignalFlags)
{
	return S_OK;
}




// macro for exception handling and logging
#define CATCH_EXCP catch (Exception & exp) \
{ exp.report(); m_status = SourceError; }

// class VideoDeckLink


// constructor
VideoDeckLink::VideoDeckLink (HRESULT * hRslt) : VideoBase(),
mDLInput(NULL),
mUse3D(false),
mFrameWidth(0),
mFrameHeight(0),
mpAllocator(NULL),
mpCaptureDelegate(NULL),
mpCacheFrame(NULL),
mClosing(false)
{
	mDisplayMode = (BMDDisplayMode)0;
	mPixelFormat = (BMDPixelFormat)0;
	pthread_mutex_init(&mCacheMutex, NULL);
}

// destructor
VideoDeckLink::~VideoDeckLink ()
{
	LockCache();
	mClosing = true;
	if (mpCacheFrame)
	{
		mpCacheFrame->Release();
		mpCacheFrame = NULL;
	}
	UnlockCache();
	if (mDLInput != NULL)
	{
		// Cleanup for Capture
		mDLInput->StopStreams();
		mDLInput->SetCallback(NULL);
		mDLInput->DisableVideoInput();
		mDLInput->DisableAudioInput();
		mDLInput->FlushStreams();
		if (mDLInput->Release() != 0) {
			printf("Reference count not NULL on DeckLink device when closing it, please report!\n");
		}
		mDLInput = NULL;
	}
	
	if (mpAllocator)
	{
		// if the device was properly cleared, this should be 0
		if (mpAllocator->Release() != 0) {
			printf("Reference count not NULL on Allocator when closing it, please report!\n");
		}
		mpAllocator = NULL;
	}
	if (mpCaptureDelegate)
	{
		delete mpCaptureDelegate;
		mpCaptureDelegate = NULL;
	}
}

void VideoDeckLink::refresh(void)
{
	m_avail = false;
}

// release components
bool VideoDeckLink::release()
{
	// release
	return true;
}

// open video file
void VideoDeckLink::openFile (char *filename)
{
	// only live capture on this device
	THRWEXCP(SourceVideoOnlyCapture, S_OK);
}


// open video capture device
void VideoDeckLink::openCam (char *format, short camIdx)
{
	IDeckLinkDisplayModeIterator*	pDLDisplayModeIterator;
	BMDDisplayModeSupport			modeSupport;
	IDeckLinkDisplayMode*			pDLDisplayMode;
	IDeckLinkIterator*				pIterator;
	BMDTimeValue					frameDuration;
	BMDTimeScale					frameTimescale;
	IDeckLink*						pDL;
	uint32_t displayFlags, inputFlags; 
	char *pPixel, *p3D, *pEnd, *pSize;
	size_t len;
	int i, modeIdx, cacheSize;

	// format is constructed as <displayMode>/<pixelFormat>[/3D][:<cacheSize>]
	// <displayMode> takes the form of BMDDisplayMode identifier minus the 'bmdMode' prefix.
	//               This implementation understands all the modes defined in SDK 10.3.1 but you can alternatively
	//               use the 4 characters internal representation of the mode (e.g. 'HD1080p24' == '24ps')
	// <pixelFormat> takes the form of BMDPixelFormat identifier minus the 'bmdFormat' prefix.
	//               This implementation understand all the formats defined in SDK 10.32.1 but you can alternatively
	//               use the 4 characters internal representation of the format (e.g. '10BitRGB' == 'r210')
	// Not all combinations of mode and pixel format are possible and it also depends on the card!
	// Use /3D postfix if you are capturing a 3D stream with frame packing
	// Example: To capture FullHD 1920x1080@24Hz with 3D packing and 4:4:4 10 bits RGB pixel format, use
	// "HD1080p24/10BitRGB/3D"  (same as "24ps/r210/3D")
	// (this will be the normal capture format for FullHD on the DeckLink 4k extreme)

	if ((pSize = strchr(format, ':')) != NULL) {
		cacheSize = strtol(pSize+1, &pEnd, 10);
	}
	else {
		cacheSize = 8;
		pSize = format + strlen(format);
	}
	if ((pPixel = strchr(format, '/')) == NULL ||
		((p3D = strchr(pPixel + 1, '/')) != NULL && strncmp(p3D, "/3D", pSize-p3D)))
		THRWEXCP(VideoDeckLinkBadFormat, S_OK);
	mUse3D = (p3D) ? true : false;
	// to simplify pixel format parsing
	if (!p3D)
		p3D = pSize;

	// read the mode
	len = (size_t)(pPixel - format);
	// accept integer display mode

	try {
		// throws if bad mode
		decklink_ReadDisplayMode(format, len, &mDisplayMode);
		// found a valid mode, remember that we do not look for an index
		modeIdx = -1;
	}
	catch (Exception &) {
		// accept also purely numerical mode as a mode index
		modeIdx = strtol(format, &pEnd, 10);
		if (pEnd != pPixel || modeIdx < 0)
			// not a pure number, give up
			throw;
	}

	// skip /
	pPixel++;
	len = (size_t)(p3D - pPixel);
	// throws if bad format
	decklink_ReadPixelFormat(pPixel, len, &mPixelFormat);

	// Caution: DeckLink API used from this point, make sure entity are released before throwing
	// open the card
	pIterator = BMD_CreateDeckLinkIterator();
	if (pIterator)  {
		i = 0;
		while (pIterator->Next(&pDL) == S_OK) {
			if (i == camIdx) {
				if (pDL->QueryInterface(IID_IDeckLinkInput, (void**)&mDLInput) != S_OK)
					mDLInput = NULL;
				pDL->Release();
				break;
			}
			i++;
			pDL->Release();
		}
		pIterator->Release();
	}
	if (!mDLInput)
		THRWEXCP(VideoDeckLinkOpenCard, S_OK);

	
	// check if display mode and pixel format are supported
	if (mDLInput->GetDisplayModeIterator(&pDLDisplayModeIterator) != S_OK)
		THRWEXCP(DeckLinkInternalError, S_OK);

	pDLDisplayMode = NULL;
	displayFlags = (mUse3D) ? bmdDisplayModeSupports3D : 0;
	inputFlags = (mUse3D) ? bmdVideoInputDualStream3D : bmdVideoInputFlagDefault;
	while (pDLDisplayModeIterator->Next(&pDLDisplayMode) == S_OK)
	{
		if (modeIdx == 0 || pDLDisplayMode->GetDisplayMode() == mDisplayMode) {
			// in case we get here because of modeIdx, make sure we have mDisplayMode set
			mDisplayMode = pDLDisplayMode->GetDisplayMode();
			if ((pDLDisplayMode->GetFlags() & displayFlags) == displayFlags &&
			    mDLInput->DoesSupportVideoMode(mDisplayMode, mPixelFormat, inputFlags, &modeSupport, NULL) == S_OK &&
			    modeSupport == bmdDisplayModeSupported)
			{
				break;
			}
		}
		pDLDisplayMode->Release();
		pDLDisplayMode = NULL;
		if (modeIdx-- == 0) {
			// reached the correct mode index but it does not meet the pixel format, give up
			break;
		}
	}
	pDLDisplayModeIterator->Release();

	if (pDLDisplayMode == NULL)
		THRWEXCP(VideoDeckLinkBadFormat, S_OK);

	mFrameWidth = pDLDisplayMode->GetWidth();
	mFrameHeight = pDLDisplayMode->GetHeight();
	mTextureDesc.height = (mUse3D) ? 2 * mFrameHeight : mFrameHeight;
	pDLDisplayMode->GetFrameRate(&frameDuration, &frameTimescale);
	pDLDisplayMode->Release();
	// for information, in case the application wants to know
	m_size[0] = mFrameWidth;
	m_size[1] = mTextureDesc.height;
	m_frameRate = (float)frameTimescale / (float)frameDuration;

	switch (mPixelFormat)
	{
	case bmdFormat8BitYUV:
		// 2 pixels per word
		mTextureDesc.stride = mFrameWidth * 2;
		mTextureDesc.width = mFrameWidth / 2;
		mTextureDesc.internalFormat = GL_RGBA;
		mTextureDesc.format = GL_BGRA;
		mTextureDesc.type = GL_UNSIGNED_BYTE;
		break;
	case bmdFormat10BitYUV:
		// 6 pixels in 4 words, rounded to 48 pixels
		mTextureDesc.stride = ((mFrameWidth + 47) / 48) * 128;
		mTextureDesc.width = mTextureDesc.stride/4;
		mTextureDesc.internalFormat = GL_RGB10_A2;
		mTextureDesc.format = GL_BGRA;
		mTextureDesc.type = GL_UNSIGNED_INT_2_10_10_10_REV;
		break;
	case bmdFormat8BitARGB:
		mTextureDesc.stride = mFrameWidth * 4;
		mTextureDesc.width = mFrameWidth;
		mTextureDesc.internalFormat = GL_RGBA;
		mTextureDesc.format = GL_BGRA;
		mTextureDesc.type = GL_UNSIGNED_INT_8_8_8_8;
		break;
	case bmdFormat8BitBGRA:
		mTextureDesc.stride = mFrameWidth * 4;
		mTextureDesc.width = mFrameWidth;
		mTextureDesc.internalFormat = GL_RGBA;
		mTextureDesc.format = GL_BGRA;
		mTextureDesc.type = GL_UNSIGNED_BYTE;
		break;
	case bmdFormat10BitRGBXLE:
		// 1 pixel per word, rounded to 64 pixels
		mTextureDesc.stride = ((mFrameWidth + 63) / 64) * 256;
		mTextureDesc.width = mTextureDesc.stride/4;
		mTextureDesc.internalFormat = GL_RGB10_A2;
		mTextureDesc.format = GL_RGBA;
		mTextureDesc.type = GL_UNSIGNED_INT_10_10_10_2;
		break;
	case bmdFormat10BitRGBX:
	case bmdFormat10BitRGB:
		// 1 pixel per word, rounded to 64 pixels
		mTextureDesc.stride = ((mFrameWidth + 63) / 64) * 256;
		mTextureDesc.width = mTextureDesc.stride/4;
		mTextureDesc.internalFormat = GL_R32UI;
		mTextureDesc.format = GL_RED_INTEGER;
		mTextureDesc.type = GL_UNSIGNED_INT;
		break;
	case bmdFormat12BitRGB:
	case bmdFormat12BitRGBLE:
		// 8 pixels in 9 word
		mTextureDesc.stride = (mFrameWidth * 36) / 8;
		mTextureDesc.width = mTextureDesc.stride/4;
		mTextureDesc.internalFormat = GL_R32UI;
		mTextureDesc.format = GL_RED_INTEGER;
		mTextureDesc.type = GL_UNSIGNED_INT;
		break;
	default:
		// for unknown pixel format, this will be resolved when a frame arrives
		mTextureDesc.format = GL_RED_INTEGER;
		mTextureDesc.type = GL_UNSIGNED_INT;
		break;
	}
	// reserve memory for cache frame + 1 to accomodate for pixel format that we don't know yet
	// note: we can't use stride as it is not yet known if the pixel format is unknown
	//       use instead the frame width as in worst case it's not much different (e.g. HD720/10BITYUV: 1296 pixels versus 1280)
	// note: some pixel format take more than 4 bytes take that into account (9/8 versus 1)
	mpAllocator = new PinnedMemoryAllocator(cacheSize, mFrameWidth*mTextureDesc.height * 4 * (1+cacheSize*9/8));

	if (mDLInput->SetVideoInputFrameMemoryAllocator(mpAllocator) != S_OK)
		THRWEXCP(DeckLinkInternalError, S_OK);

	mpCaptureDelegate = new CaptureDelegate(this);
	if (mDLInput->SetCallback(mpCaptureDelegate) != S_OK)
		THRWEXCP(DeckLinkInternalError, S_OK);

	if (mDLInput->EnableVideoInput(mDisplayMode, mPixelFormat, ((mUse3D) ? bmdVideoInputDualStream3D : bmdVideoInputFlagDefault)) != S_OK)
		// this shouldn't failed, we tested above
		THRWEXCP(DeckLinkInternalError, S_OK); 

	// just in case it is needed to capture from certain cards, we don't check error because we don't need audio
	mDLInput->EnableAudioInput(bmdAudioSampleRate48kHz, bmdAudioSampleType16bitInteger, 2);

	// open base class
	VideoBase::openCam(format, camIdx);

	// ready to capture, will start when application calls play()
}

// play video
bool VideoDeckLink::play (void)
{
	try
	{
		// if object is able to play
		if (VideoBase::play())
		{
			mDLInput->FlushStreams();
			return (mDLInput->StartStreams() == S_OK);
		}
	}
	CATCH_EXCP;
	return false;
}


// pause video
bool VideoDeckLink::pause (void)
{
	try
	{
		if (VideoBase::pause())
		{
			mDLInput->PauseStreams();
			return true;
		}
	}
	CATCH_EXCP;
	return false;
}

// stop video
bool VideoDeckLink::stop (void)
{
	try
	{
		VideoBase::stop();
		mDLInput->StopStreams();
		return true;
	}
	CATCH_EXCP;
	return false;
}


// set video range
void VideoDeckLink::setRange (double start, double stop)
{
}

// set framerate
void VideoDeckLink::setFrameRate (float rate)
{
}


// image calculation
// send cache frame directly to GPU
void VideoDeckLink::calcImage (unsigned int texId, double ts)
{
	IDeckLinkVideoInputFrame* pFrame;
	LockCache();
	pFrame = mpCacheFrame;
	mpCacheFrame = NULL;
	UnlockCache();
	if (pFrame) {
		// BUG: the dvpBindToGLCtx function fails the first time it is used, don't know why.
		// This causes an exception to be thrown.
		// This should be fixed but in the meantime we will catch the exception because
		// it is crucial that we release the frame to keep the reference count right on the DeckLink device
		try {
			uint32_t rowSize = pFrame->GetRowBytes();
			uint32_t textureSize = rowSize * pFrame->GetHeight();
			void* videoPixels = NULL;
			void* rightEyePixels = NULL;
			if (!mTextureDesc.stride) {
				// we could not compute the texture size earlier (unknown pixel size)
				// let's do it now
				mTextureDesc.stride = rowSize;
				mTextureDesc.width = mTextureDesc.stride / 4;
			}
			if (mTextureDesc.stride != rowSize) {
				// unexpected frame size, ignore
				// TBD: print a warning
			}
			else {
				pFrame->GetBytes(&videoPixels);
				if (mUse3D) {
					IDeckLinkVideoFrame3DExtensions *if3DExtensions = NULL;
					IDeckLinkVideoFrame *rightEyeFrame = NULL;
					if (pFrame->QueryInterface(IID_IDeckLinkVideoFrame3DExtensions, (void **)&if3DExtensions) == S_OK &&
						if3DExtensions->GetFrameForRightEye(&rightEyeFrame) == S_OK) {
						rightEyeFrame->GetBytes(&rightEyePixels);
						textureSize += ((uint64_t)rightEyePixels - (uint64_t)videoPixels);
					}
					if (rightEyeFrame)
						rightEyeFrame->Release();
					if (if3DExtensions)
						if3DExtensions->Release();
				}
				mTextureDesc.size = mTextureDesc.width * mTextureDesc.height * 4;
				if (mTextureDesc.size == textureSize) {
					// this means that both left and right frame are contiguous and that there is no padding
					// do the transfer
					mpAllocator->TransferBuffer(videoPixels, &mTextureDesc, texId);
				}
			}
		} 
		catch (Exception &) {
			pFrame->Release();
			throw;
		}
		// this will trigger PinnedMemoryAllocator::RealaseBuffer
		pFrame->Release();
	}
	// currently we don't pass the image to the application
	m_avail = false;
}

// A frame is available from the board
// Called from an internal thread, just pass the frame to the main thread
void VideoDeckLink::VideoFrameArrived(IDeckLinkVideoInputFrame* inputFrame)
{
	IDeckLinkVideoInputFrame* pOldFrame = NULL;
	LockCache();
	if (!mClosing)
	{
		pOldFrame = mpCacheFrame;
		mpCacheFrame = inputFrame;
		inputFrame->AddRef();
	}
	UnlockCache();
	// old frame no longer needed, just release it
	if (pOldFrame)
		pOldFrame->Release();
}

// python methods

// object initialization
static int VideoDeckLink_init(PyObject *pySelf, PyObject *args, PyObject *kwds)
{
	static const char *kwlist[] = { "format", "capture", NULL };
	PyImage *self = reinterpret_cast<PyImage*>(pySelf);
	// see openCam for a description of format
	char * format = NULL;
	// capture device number, i.e. DeckLink card number, default first one
	short capt = 0;

	if (!GLEW_VERSION_1_5) {
		PyErr_SetString(PyExc_RuntimeError, "VideoDeckLink requires at least OpenGL 1.5");
		return -1;
	}
	// get parameters
	if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|h",
		const_cast<char**>(kwlist), &format, &capt))
		return -1; 

	try {
		// create video object
		Video_init<VideoDeckLink>(self);

		// open video source, control comes back to VideoDeckLink::openCam
		Video_open(getVideo(self), format, capt);
	}
	catch (Exception & exp) {
		exp.report();
		return -1;
	}
	// initialization succeded
	return 0;
}

// methods structure
static PyMethodDef videoMethods[] =
{ // methods from VideoBase class
	{"play", (PyCFunction)Video_play, METH_NOARGS, "Play (restart) video"},
	{"pause", (PyCFunction)Video_pause, METH_NOARGS, "pause video"},
	{"stop", (PyCFunction)Video_stop, METH_NOARGS, "stop video (play will replay it from start)"},
	{"refresh", (PyCFunction)Video_refresh, METH_VARARGS, "Refresh video - get its status"},
	{NULL}
};
// attributes structure
static PyGetSetDef videoGetSets[] =
{ // methods from VideoBase class
	{(char*)"status", (getter)Video_getStatus, NULL, (char*)"video status", NULL},
	{(char*)"framerate", (getter)Video_getFrameRate, NULL, (char*)"frame rate", NULL},
	// attributes from ImageBase class
	{(char*)"valid", (getter)Image_valid, NULL, (char*)"bool to tell if an image is available", NULL},
	{(char*)"image", (getter)Image_getImage, NULL, (char*)"image data", NULL},
	{(char*)"size", (getter)Image_getSize, NULL, (char*)"image size", NULL},
	{(char*)"scale", (getter)Image_getScale, (setter)Image_setScale, (char*)"fast scale of image (near neighbor)", NULL},
	{(char*)"flip", (getter)Image_getFlip, (setter)Image_setFlip, (char*)"flip image vertically", NULL},
	{(char*)"filter", (getter)Image_getFilter, (setter)Image_setFilter, (char*)"pixel filter", NULL},
	{NULL}
};

// python type declaration
PyTypeObject VideoDeckLinkType =
{ 
	PyVarObject_HEAD_INIT(NULL, 0)
	"VideoTexture.VideoDeckLink",   /*tp_name*/
	sizeof(PyImage),          /*tp_basicsize*/
	0,                         /*tp_itemsize*/
	(destructor)Image_dealloc, /*tp_dealloc*/
	0,                         /*tp_print*/
	0,                         /*tp_getattr*/
	0,                         /*tp_setattr*/
	0,                         /*tp_compare*/
	0,                         /*tp_repr*/
	0,                         /*tp_as_number*/
	0,                         /*tp_as_sequence*/
	0,                         /*tp_as_mapping*/
	0,                         /*tp_hash */
	0,                         /*tp_call*/
	0,                         /*tp_str*/
	0,                         /*tp_getattro*/
	0,                         /*tp_setattro*/
	&imageBufferProcs,         /*tp_as_buffer*/
	Py_TPFLAGS_DEFAULT,        /*tp_flags*/
	"DeckLink video source",       /* tp_doc */
	0,		               /* tp_traverse */
	0,		               /* tp_clear */
	0,		               /* tp_richcompare */
	0,		               /* tp_weaklistoffset */
	0,		               /* tp_iter */
	0,		               /* tp_iternext */
	videoMethods,    /* tp_methods */
	0,                   /* tp_members */
	videoGetSets,          /* tp_getset */
	0,                         /* tp_base */
	0,                         /* tp_dict */
	0,                         /* tp_descr_get */
	0,                         /* tp_descr_set */
	0,                         /* tp_dictoffset */
	(initproc)VideoDeckLink_init,     /* tp_init */
	0,                         /* tp_alloc */
	Image_allocNew,           /* tp_new */
};



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

#endif		// WITH_GAMEENGINE_DECKLINK