2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
4 * This file is part of CasparCG (www.casparcg.com).
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
19 * Author: Robert Nagy, ronag89@gmail.com
26 #include <BlueVelvetC.h>
28 #include <common/memory.h>
29 #include <common/except.h>
33 namespace caspar { namespace bluefish {
37 // Define a different type for each of the function ptrs.
38 typedef const char* (__cdecl *pFunc_bfcGetVersion)();
39 typedef BLUEVELVETC_HANDLE(__cdecl *pFunc_bfcFactory)();
40 typedef void(__cdecl *pFunc_bfcDestroy)(BLUEVELVETC_HANDLE pHandle);
41 typedef int(__cdecl *pFunc_bfcEnumerate)(BLUEVELVETC_HANDLE pHandle, int& iDevices);
42 typedef int(__cdecl *pFunc_bfcQueryCardType)(BLUEVELVETC_HANDLE pHandle, int& iCardType, int iDeviceID);
43 typedef int(__cdecl *pFunc_bfcAttach)(BLUEVELVETC_HANDLE pHandle, int iDeviceId);
44 typedef int(__cdecl *pFunc_bfcDetach)(BLUEVELVETC_HANDLE pHandle);
45 typedef int(__cdecl *pFunc_bfcQueryCardProperty32)(BLUEVELVETC_HANDLE pHandle, const int iProperty, unsigned int& nValue);
46 typedef int(__cdecl *pFunc_bfcQueryCardProperty64)(BLUEVELVETC_HANDLE pHandle, const int iProperty, unsigned long long& ullValue);
47 typedef int(__cdecl *pFunc_bfcSetCardProperty32)(BLUEVELVETC_HANDLE pHandle, const int iProperty, const unsigned int nValue);
48 typedef int(__cdecl *pFunc_bfcSetCardProperty64)(BLUEVELVETC_HANDLE pHandle, const int iProperty, const unsigned long long ullValue);
49 typedef int(__cdecl *pFunc_bfcGetCardSerialNumber)(BLUEVELVETC_HANDLE pHandle, char* pSerialNumber, unsigned int nStringSize);
50 typedef int(__cdecl *pFunc_bfcGetCardFwVersion)(BLUEVELVETC_HANDLE pHandle, unsigned int& nValue);
51 typedef int(__cdecl *pFunc_bfcWaitVideoSyncAsync)(BLUEVELVETC_HANDLE pHandle, OVERLAPPED* pOverlap, blue_video_sync_struct* pSyncData);
52 typedef int(__cdecl *pFunc_bfcWaitVideoInputSync)(BLUEVELVETC_HANDLE pHandle, unsigned long ulUpdateType, unsigned long& ulFieldCount);
53 typedef int(__cdecl *pFunc_bfcWaitVideoOutputSync)(BLUEVELVETC_HANDLE pHandle, unsigned long ulUpdateType, unsigned long& ulFieldCount);
54 typedef int(__cdecl *pFunc_bfcGetVideoOutputCurrentFieldCount)(BLUEVELVETC_HANDLE pHandle, unsigned long& ulFieldCount);
55 typedef int(__cdecl *pFunc_bfcGetVideoInputCurrentFieldCount)(BLUEVELVETC_HANDLE pHandle, unsigned long& ulFieldCount);
56 typedef int(__cdecl *pFunc_bfcVideoCaptureStart)(BLUEVELVETC_HANDLE pHandle);
57 typedef int(__cdecl *pFunc_bfcVideoCaptureStop)(BLUEVELVETC_HANDLE pHandle);
58 typedef int(__cdecl *pFunc_bfcVideoPlaybackStart)(BLUEVELVETC_HANDLE pHandle, int iStep, int iLoop);
59 typedef int(__cdecl *pFunc_bfcVideoPlaybackStop)(BLUEVELVETC_HANDLE pHandle, int iWait, int iFlush);
60 typedef int(__cdecl *pFunc_bfcVideoPlaybackAllocate)(BLUEVELVETC_HANDLE pHandle, void** pAddress, unsigned long& ulBufferID, unsigned long& ulUnderrun);
61 typedef int(__cdecl *pFunc_bfcVideoPlaybackPresent)(BLUEVELVETC_HANDLE pHandle, unsigned long& ulUniqueID, unsigned long ulBufferID, unsigned long ulCount, int iKeep, int iOdd);
62 typedef int(__cdecl *pFunc_bfcVideoPlaybackRelease)(BLUEVELVETC_HANDLE pHandle, unsigned long ulBufferID);
63 typedef int(__cdecl *pFunc_bfcGetCaptureVideoFrameInfoEx)(BLUEVELVETC_HANDLE pHandle, OVERLAPPED* pOverlap, struct blue_videoframe_info_ex& VideoFrameInfo, int iCompostLater, unsigned int* nCaptureFifoSize);
64 typedef int(__cdecl *pFunc_bfcRenderBufferCapture)(BLUEVELVETC_HANDLE pHandle, unsigned long ulBufferID);
65 typedef int(__cdecl *pFunc_bfcRenderBufferUpdate)(BLUEVELVETC_HANDLE pHandle, unsigned long ulBufferID);
66 typedef int(__cdecl *pFunc_bfcGetRenderBufferCount)(BLUEVELVETC_HANDLE pHandle, unsigned long& ulCount);
67 typedef int(__cdecl *pFunc_bfcEncodeHancFrameEx)(BLUEVELVETC_HANDLE pHandle, unsigned int nCardType, struct hanc_stream_info_struct* pHancEncodeInfo, void* pAudioBuffer, unsigned int nAudioChannels, unsigned int nAudioSamples, unsigned int nSampleType, unsigned int nAudioFlags);
68 typedef int(__cdecl *pFunc_bfcDecodeHancFrameEx)(BLUEVELVETC_HANDLE pHandle, unsigned int nCardType, unsigned int* pHancBuffer, struct hanc_decode_struct* pHancDecodeInfo);
70 typedef int(__cdecl *pFunc_bfcSystemBufferReadAsync)(BLUEVELVETC_HANDLE pHandle, unsigned char* pPixels, unsigned long ulSize, OVERLAPPED* pOverlap, unsigned long ulBufferID, unsigned long ulOffset);
71 typedef int(__cdecl *pFunc_bfcSystemBufferWriteAsync)(BLUEVELVETC_HANDLE pHandle, unsigned char* pPixels, unsigned long ulSize, OVERLAPPED* pOverlap, unsigned long ulBufferID, unsigned long ulOffset);
73 typedef int(__cdecl *pFunc_bfcSystemBufferRead)(BLUEVELVETC_HANDLE pHandle, unsigned char* pPixels, unsigned long ulSize, unsigned long ulBufferID, unsigned long ulOffset;
74 typedef int(__cdecl *pFunc_bfcSystemBufferWrite)(BLUEVELVETC_HANDLE pHandle, unsigned char* pPixels, unsigned long ulSize, unsigned long ulBufferID, unsigned long ulOffset;
76 typedef int(__cdecl *pFunc_bfcGetBytesForGroupPixels)(EMemoryFormat nMemoryFormat, unsigned int nVideoWidth, unsigned int& nVideoPitchBytes);
77 typedef int(__cdecl *pFunc_bfcGetPixelsPerLine)(EVideoMode nVideoMode, unsigned int& nPixelsPerLine);
78 typedef int(__cdecl *pFunc_bfcGetLinesPerFrame)(EVideoMode nVideoMode, EUpdateMethod nUpdateMethod, unsigned int& nLinesPerFrame);
79 typedef int(__cdecl *pFunc_bfcGetBytesPerLine)(EVideoMode nVideoMode, EMemoryFormat nMemoryFormat, unsigned int& nBytesPerLine);
80 typedef int(__cdecl *pFunc_bfcGetBytesPerFrame)(EVideoMode nVideoMode, EMemoryFormat nMemoryFormat, EUpdateMethod nUpdateMethod, unsigned int& nBytesPerFrame);
81 typedef int(__cdecl *pFunc_bfcGetGoldenValue)(EVideoMode nVideoMode, EMemoryFormat nMemoryFormat, EUpdateMethod nUpdateMethod, unsigned int& nGoldenFrameSize);
82 typedef int(__cdecl *pFunc_bfcGetVBILines)(EVideoMode nVideoMode, EDMADataType nDataType, unsigned int& nVBILinesPerFrame);
83 typedef int(__cdecl *pFunc_bfcGetVANCGoldenValue)(unsigned int nCardType, EVideoMode nVideoMode, EMemoryFormat nMemoryFormat, EDMADataType nDataFormat, unsigned int& nVANCGoldenValue);
84 typedef int(__cdecl *pFunc_bfcGetVANCLineBytes)(unsigned int nCardType, EVideoMode nVideoMode, EMemoryFormat nMemoryFormat, unsigned int& nVANCLineBytes);
85 typedef int(__cdecl *pFunc_bfcGetVANCLineCount)(unsigned int nCardType, EVideoMode nVideoMode, EDMADataType nDataFormat, unsigned int& nVANCLineCount);
86 typedef int(__cdecl *pFunc_bfcGetWindowsDriverHandle)(BLUEVELVETC_HANDLE pHandle, HANDLE* pDriverHandle);
87 typedef int(__cdecl *pFunc_bfcSetDynamicMemoryFormatChange)(BLUEVELVETC_HANDLE pHandle, OVERLAPPED* pOverlap, unsigned int nUniqueId, EMemoryFormat nMemoryFormat);
89 //BlueVelvetC utils functions
90 typedef char*(__cdecl *pFunc_bfcUtilsGetStringForCardType)(const int iCardType);
91 typedef char*(__cdecl *pFunc_bfcUtilsGetStringForBlueProductId)(const unsigned int nProductId);
92 typedef char*(__cdecl *pFunc_bfcUtilsGetStringForVideoMode)(const unsigned int nVideoMode);
93 typedef char*(__cdecl *pFunc_bfcUtilsGetStringForMemoryFormat)(const unsigned int nMemoryFormat);
94 typedef int(__cdecl *pFunc_bfcUtilsGetMR2Routing)(const BLUEVELVETC_HANDLE pHandle, unsigned int& nSrcNode, const unsigned int nDestNode, unsigned int& nLinkType);
95 typedef int(__cdecl *pFunc_bfcUtilsSetMR2Routing)(const BLUEVELVETC_HANDLE pHandle, const unsigned int nSrcNode, const unsigned int nDestNode, const unsigned int nLinkType);
96 typedef int(__cdecl *pFunc_bfcUtilsGetAudioOutputRouting)(const BLUEVELVETC_HANDLE pHandle, const unsigned int nAudioConnectorType, unsigned int& nAudioSourceChannelId, unsigned int nAudioConnectorId);
97 typedef int(__cdecl *pFunc_bfcUtilsSetAudioOutputRouting)(const BLUEVELVETC_HANDLE pHandle, const unsigned int nAudioConnectorType, unsigned int nAudioSourceChannelId, unsigned int nAudioConnectorId);
98 typedef bool(__cdecl *pFunc_bfcUtilsIsVideoModeProgressive)(const unsigned int nVideoMode);
99 typedef bool(__cdecl *pFunc_bfcUtilsIsVideoMode1001Framerate)(const unsigned int nVideoMode);
100 typedef int(__cdecl *pFunc_bfcUtilsGetFpsForVideoMode)(const unsigned int nVideoMode);
101 typedef int(__cdecl *pFunc_bfcUtilsGetVideoModeForFrameInfo)(const BLUE_UINT32 nWidth, const BLUE_UINT32 nHeight, const BLUE_UINT32 nRate, const BLUE_UINT32 bIs1001, const BLUE_UINT32 bIsProgressive, BLUE_UINT32& nVideoMode);
102 typedef int(__cdecl *pFunc_bfcUtilsGetFrameInfoForVideoMode)(const BLUE_UINT32 nVideoMode, BLUE_UINT32& nWidth, BLUE_UINT32& nHeight, BLUE_UINT32& nRate, BLUE_UINT32& bIs1001, BLUE_UINT32& bIsProgressive);
103 typedef int(__cdecl *pFunc_bfcUtilsGetAudioSamplesPerFrame)(const BLUE_UINT32 nVideoMode, const BLUE_UINT32 nFrameNo);
108 bvc_wrapper(); // bfcFactory + function pointer lookups
110 const char* get_version();
112 BLUE_UINT32 enumerate(int& iDevices);
113 BLUE_UINT32 query_card_type(int& iCardType, int iDeviceID);
115 BLUE_UINT32 attach(int iDeviceId);
116 BLUE_UINT32 detach();
118 BLUE_UINT32 get_card_property32(const int iProperty, unsigned int& nValue);
119 BLUE_UINT32 set_card_property32(const int iProperty, const unsigned int nValue);
121 BLUE_UINT32 system_buffer_write(unsigned char* pPixels, unsigned long ulSize, unsigned long ulBufferID, unsigned long ulOffset);
122 BLUE_UINT32 system_buffer_read(unsigned char* pPixels, unsigned long ulSize, unsigned long ulBufferID, unsigned long ulOffset);
124 BLUE_UINT32 video_playback_stop(int iWait, int iFlush);
125 BLUE_UINT32 video_playback_start(int wait, int loop);
126 BLUE_UINT32 video_playback_allocate(unsigned long& buffer_id, unsigned long& underrun);
127 BLUE_UINT32 video_playback_present(unsigned long buffer_id, unsigned long count, unsigned long keep, unsigned long odd);
129 BLUE_UINT32 wait_video_output_sync(unsigned long ulUpdateType, unsigned long& ulFieldCount);
130 BLUE_UINT32 wait_video_input_sync(unsigned long ulUpdateType, unsigned long & ulFieldCount);
132 BLUE_UINT32 render_buffer_update(unsigned long ulBufferID);
133 BLUE_UINT32 render_buffer_capture(unsigned long ulBufferID);
135 BLUE_UINT32 encode_hanc_frame(unsigned int nCardType, struct hanc_stream_info_struct* pHancEncodeInfo, void* pAudioBuffer, unsigned int nAudioChannels, unsigned int nAudioSamples, unsigned int nSampleType, unsigned int nAudioFlags);
136 BLUE_UINT32 decode_hanc_frame(unsigned int nCardType, unsigned int* pHancBuffer, struct hanc_decode_struct* pHancDecodeInfo);
138 BLUE_UINT32 get_frame_info_for_video_mode(const unsigned int nVideoMode, unsigned int& nWidth, unsigned int& nHeight, unsigned int& nRate, unsigned int& bIs1001, unsigned int& bIsProgressive);
139 BLUE_UINT32 get_bytes_per_frame(EVideoMode nVideoMode, EMemoryFormat nMemoryFormat, EUpdateMethod nUpdateMethod, unsigned int& nBytesPerFrame);
142 bool init_function_pointers();
144 std::shared_ptr<void> h_module_;
145 std::shared_ptr<void> bvc_;
147 //BlueVelvetC function pointers members
148 pFunc_bfcGetVersion bfcGetVersion = nullptr;
149 pFunc_bfcFactory bfcFactory = nullptr;
150 pFunc_bfcDestroy bfcDestroy = nullptr;
151 pFunc_bfcEnumerate bfcEnumerate = nullptr;
152 pFunc_bfcQueryCardType bfcQueryCardType = nullptr;
153 pFunc_bfcAttach bfcAttach = nullptr;
154 pFunc_bfcDetach bfcDetach = nullptr;
155 pFunc_bfcQueryCardProperty32 bfcQueryCardProperty32 = nullptr;
156 pFunc_bfcQueryCardProperty64 bfcQueryCardProperty64 = nullptr;
157 pFunc_bfcSetCardProperty32 bfcSetCardProperty32 = nullptr;
158 pFunc_bfcSetCardProperty64 bfcSetCardProperty64 = nullptr;
159 pFunc_bfcGetCardSerialNumber bfcGetCardSerialNumber = nullptr;
160 pFunc_bfcGetCardFwVersion bfcGetCardFwVersion = nullptr;
161 pFunc_bfcWaitVideoSyncAsync bfcWaitVideoSyncAsync = nullptr;
162 pFunc_bfcWaitVideoInputSync bfcWaitVideoInputSync = nullptr;
163 pFunc_bfcWaitVideoOutputSync bfcWaitVideoOutputSync = nullptr;
164 pFunc_bfcGetVideoOutputCurrentFieldCount bfcGetVideoOutputCurrentFieldCount = nullptr;
165 pFunc_bfcGetVideoInputCurrentFieldCount bfcGetVideoInputCurrentFieldCount = nullptr;
166 pFunc_bfcVideoCaptureStart bfcVideoCaptureStart = nullptr;
167 pFunc_bfcVideoCaptureStop bfcVideoCaptureStop = nullptr;
168 pFunc_bfcVideoPlaybackStart bfcVideoPlaybackStart = nullptr;
169 pFunc_bfcVideoPlaybackStop bfcVideoPlaybackStop = nullptr;
170 pFunc_bfcVideoPlaybackAllocate bfcVideoPlaybackAllocate = nullptr;
171 pFunc_bfcVideoPlaybackPresent bfcVideoPlaybackPresent = nullptr;
172 pFunc_bfcVideoPlaybackRelease bfcVideoPlaybackRelease = nullptr;
173 pFunc_bfcGetCaptureVideoFrameInfoEx bfcGetCaptureVideoFrameInfoEx = nullptr;
174 pFunc_bfcRenderBufferCapture bfcRenderBufferCapture = nullptr;
175 pFunc_bfcRenderBufferUpdate bfcRenderBufferUpdate = nullptr;
176 pFunc_bfcGetRenderBufferCount bfcGetRenderBufferCount = nullptr;
177 pFunc_bfcEncodeHancFrameEx bfcEncodeHancFrameEx = nullptr;
178 pFunc_bfcDecodeHancFrameEx bfcDecodeHancFrameEx = nullptr;
180 pFunc_bfcSystemBufferReadAsync bfcSystemBufferReadAsync = nullptr;
181 pFunc_bfcSystemBufferWriteAsync bfcSystemBufferWriteAsync = nullptr;
183 pFunc_bfcSystemBufferRead bfcSystemBufferRead = nullptr;
184 pFunc_bfcSystemBufferWrite bfcSystemBufferWrite = nullptr;
186 pFunc_bfcGetBytesForGroupPixels bfcGetBytesForGroupPixels = nullptr;
187 pFunc_bfcGetPixelsPerLine bfcGetPixelsPerLine = nullptr;
188 pFunc_bfcGetLinesPerFrame bfcGetLinesPerFrame = nullptr;
189 pFunc_bfcGetBytesPerLine bfcGetBytesPerLine = nullptr;
190 pFunc_bfcGetBytesPerFrame bfcGetBytesPerFrame = nullptr;
191 pFunc_bfcGetGoldenValue bfcGetGoldenValue = nullptr;
192 pFunc_bfcGetVBILines bfcGetVBILines = nullptr;
193 pFunc_bfcGetVANCGoldenValue bfcGetVANCGoldenValue = nullptr;
194 pFunc_bfcGetVANCLineBytes bfcGetVANCLineBytes = nullptr;
195 pFunc_bfcGetVANCLineCount bfcGetVANCLineCount = nullptr;
196 pFunc_bfcGetWindowsDriverHandle bfcGetWindowsDriverHandle = nullptr;
197 pFunc_bfcUtilsGetStringForCardType bfcUtilsGetStringForCardType = nullptr;
198 pFunc_bfcUtilsGetStringForBlueProductId bfcUtilsGetStringForBlueProductId = nullptr;
199 pFunc_bfcUtilsGetStringForVideoMode bfcUtilsGetStringForVideoMode = nullptr;
200 pFunc_bfcUtilsGetStringForMemoryFormat bfcUtilsGetStringForMemoryFormat = nullptr;
201 pFunc_bfcUtilsGetMR2Routing bfcUtilsGetMR2Routing = nullptr;
202 pFunc_bfcUtilsSetMR2Routing bfcUtilsSetMR2Routing = nullptr;
203 pFunc_bfcUtilsGetAudioOutputRouting bfcUtilsGetAudioOutputRouting = nullptr;
204 pFunc_bfcUtilsSetAudioOutputRouting bfcUtilsSetAudioOutputRouting = nullptr;
205 pFunc_bfcUtilsIsVideoModeProgressive bfcUtilsIsVideoModeProgressive = nullptr;
206 pFunc_bfcUtilsIsVideoMode1001Framerate bfcUtilsIsVideoMode1001Framerate = nullptr;
207 pFunc_bfcUtilsGetFpsForVideoMode bfcUtilsGetFpsForVideoMode = nullptr;
208 pFunc_bfcUtilsGetVideoModeForFrameInfo bfcUtilsGetVideoModeForFrameInfo = nullptr;
209 pFunc_bfcUtilsGetFrameInfoForVideoMode bfcUtilsGetFrameInfoForVideoMode = nullptr;
210 pFunc_bfcUtilsGetAudioSamplesPerFrame bfcUtilsGetAudioSamplesPerFrame = nullptr;
214 spl::shared_ptr<bvc_wrapper> create_blue();
215 spl::shared_ptr<bvc_wrapper> create_blue(int device_index);
217 bool is_epoch_card(bvc_wrapper& blue);
218 bool is_epoch_neutron_1i2o_card(bvc_wrapper& blue);
219 bool is_epoch_neutron_3o_card(bvc_wrapper& blue);
220 std::wstring get_card_desc(bvc_wrapper& blue, int device_id);
221 EVideoMode get_video_mode(bvc_wrapper& blue, const core::video_format_desc& format_desc);
222 core::video_format_desc get_format_desc(bvc_wrapper& blue, EVideoMode vid_fmt, EMemoryFormat mem_fmt);