]> git.sesse.net Git - casparcg/blob - modules/bluefish/util/blue_velvet.h
0f1fec754ac50280c0dfa629ae704d4bfe3d7c5e
[casparcg] / modules / bluefish / util / blue_velvet.h
1 /*
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
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.
10 *
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.
15 *
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/>.
18 *
19 * Author: Robert Nagy, ronag89@gmail.com
20 */
21
22 #pragma once
23
24 #include <Windows.h>
25
26 #include <bluefish/interop/BlueVelvetC.h>
27
28 #include <common/memory.h>
29 #include <common/except.h>
30
31 #include <core/fwd.h>
32
33 namespace caspar { namespace bluefish {
34
35
36 template<typename T>
37 int set_card_property(T& pSdk, ULONG prop, ULONG value)
38 {
39         return (pSdk->SetCardProperty32(prop, value));
40 }
41
42 template<typename T>
43 int get_card_property(T& pSdk, ULONG prop, ULONG& value)
44 {
45         unsigned int val = 0;
46         int retVal = pSdk->QueryCardProperty32(prop, val);
47         value = val;
48         return retVal;
49 }
50
51 class BvcWrapper
52 {
53         // Define a different type for each of the function ptrs.
54         typedef const char* (__cdecl *pFunc_bfcGetVersion)();
55         typedef BLUEVELVETC_HANDLE(__cdecl *pFunc_bfcFactory)();
56         typedef void(__cdecl *pFunc_bfcDestroy)(BLUEVELVETC_HANDLE pHandle);
57         typedef int(__cdecl *pFunc_bfcEnumerate)(BLUEVELVETC_HANDLE pHandle, int& iDevices);
58         typedef int(__cdecl *pFunc_bfcQueryCardType)(BLUEVELVETC_HANDLE pHandle, int& iCardType, int iDeviceID);
59         typedef int(__cdecl *pFunc_bfcAttach)(BLUEVELVETC_HANDLE pHandle, int iDeviceId);
60         typedef int(__cdecl *pFunc_bfcDetach)(BLUEVELVETC_HANDLE pHandle);
61         typedef int(__cdecl *pFunc_bfcQueryCardProperty32)(BLUEVELVETC_HANDLE pHandle, const int iProperty, unsigned int& nValue);
62         typedef int(__cdecl *pFunc_bfcQueryCardProperty64)(BLUEVELVETC_HANDLE pHandle, const int iProperty, unsigned long long& ullValue);
63         typedef int(__cdecl *pFunc_bfcSetCardProperty32)(BLUEVELVETC_HANDLE pHandle, const int iProperty, const unsigned int nValue);
64         typedef int(__cdecl *pFunc_bfcSetCardProperty64)(BLUEVELVETC_HANDLE pHandle, const int iProperty, const unsigned long long ullValue);
65         typedef int(__cdecl *pFunc_bfcGetCardSerialNumber)(BLUEVELVETC_HANDLE pHandle, char* pSerialNumber, unsigned int nStringSize);
66         typedef int(__cdecl *pFunc_bfcGetCardFwVersion)(BLUEVELVETC_HANDLE pHandle, unsigned int& nValue);
67         typedef int(__cdecl *pFunc_bfcWaitVideoSyncAsync)(BLUEVELVETC_HANDLE pHandle, OVERLAPPED* pOverlap, blue_video_sync_struct* pSyncData);
68         typedef int(__cdecl *pFunc_bfcWaitVideoInputSync)(BLUEVELVETC_HANDLE pHandle, unsigned long ulUpdateType, unsigned long& ulFieldCount);
69         typedef int(__cdecl *pFunc_bfcWaitVideoOutputSync)(BLUEVELVETC_HANDLE pHandle, unsigned long ulUpdateType, unsigned long& ulFieldCount);
70         typedef int(__cdecl *pFunc_bfcGetVideoOutputCurrentFieldCount)(BLUEVELVETC_HANDLE pHandle, unsigned long& ulFieldCount);
71         typedef int(__cdecl *pFunc_bfcGetVideoInputCurrentFieldCount)(BLUEVELVETC_HANDLE pHandle, unsigned long& ulFieldCount);
72         typedef int(__cdecl *pFunc_bfcVideoCaptureStart)(BLUEVELVETC_HANDLE pHandle);
73         typedef int(__cdecl *pFunc_bfcVideoCaptureStop)(BLUEVELVETC_HANDLE pHandle);
74         typedef int(__cdecl *pFunc_bfcVideoPlaybackStart)(BLUEVELVETC_HANDLE pHandle, int iStep, int iLoop);
75         typedef int(__cdecl *pFunc_bfcVideoPlaybackStop)(BLUEVELVETC_HANDLE pHandle, int iWait, int iFlush);
76         typedef int(__cdecl *pFunc_bfcVideoPlaybackAllocate)(BLUEVELVETC_HANDLE pHandle, void** pAddress, unsigned long& ulBufferID, unsigned long& ulUnderrun);
77         typedef int(__cdecl *pFunc_bfcVideoPlaybackPresent)(BLUEVELVETC_HANDLE pHandle, unsigned long& ulUniqueID, unsigned long ulBufferID, unsigned long ulCount, int iKeep, int iOdd);
78         typedef int(__cdecl *pFunc_bfcVideoPlaybackRelease)(BLUEVELVETC_HANDLE pHandle, unsigned long ulBufferID);
79         typedef int(__cdecl *pFunc_bfcGetCaptureVideoFrameInfoEx)(BLUEVELVETC_HANDLE pHandle, OVERLAPPED* pOverlap, struct blue_videoframe_info_ex& VideoFrameInfo, int iCompostLater, unsigned int* nCaptureFifoSize);
80         typedef int(__cdecl *pFunc_bfcRenderBufferCapture)(BLUEVELVETC_HANDLE pHandle, unsigned long ulBufferID);
81         typedef int(__cdecl *pFunc_bfcRenderBufferUpdate)(BLUEVELVETC_HANDLE pHandle, unsigned long ulBufferID);
82         typedef int(__cdecl *pFunc_bfcGetRenderBufferCount)(BLUEVELVETC_HANDLE pHandle, unsigned long& ulCount);
83         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);
84         typedef int(__cdecl *pFunc_bfcDecodeHancFrameEx)(BLUEVELVETC_HANDLE pHandle, unsigned int nCardType, unsigned int* pHancBuffer, struct hanc_decode_struct* pHancDecodeInfo);
85 #if defined(_WIN32)
86         typedef int(__cdecl *pFunc_bfcSystemBufferReadAsync)(BLUEVELVETC_HANDLE pHandle, unsigned char* pPixels, unsigned long ulSize, OVERLAPPED* pOverlap, unsigned long ulBufferID, unsigned long ulOffset);
87         typedef int(__cdecl *pFunc_bfcSystemBufferWriteAsync)(BLUEVELVETC_HANDLE pHandle, unsigned char* pPixels, unsigned long ulSize, OVERLAPPED* pOverlap, unsigned long ulBufferID, unsigned long ulOffset);
88 #else
89         typedef int(__cdecl *pFunc_bfcSystemBufferRead)(BLUEVELVETC_HANDLE pHandle, unsigned char* pPixels, unsigned long ulSize, unsigned long ulBufferID, unsigned long ulOffset;
90         typedef int(__cdecl *pFunc_bfcSystemBufferWrite)(BLUEVELVETC_HANDLE pHandle, unsigned char* pPixels, unsigned long ulSize, unsigned long ulBufferID, unsigned long ulOffset;
91 #endif
92         typedef int(__cdecl *pFunc_bfcGetBytesForGroupPixels)(EMemoryFormat nMemoryFormat, unsigned int nVideoWidth, unsigned int& nVideoPitchBytes);
93         typedef int(__cdecl *pFunc_bfcGetPixelsPerLine)(EVideoMode nVideoMode, unsigned int& nPixelsPerLine);
94         typedef int(__cdecl *pFunc_bfcGetLinesPerFrame)(EVideoMode nVideoMode, EUpdateMethod nUpdateMethod, unsigned int& nLinesPerFrame);
95         typedef int(__cdecl *pFunc_bfcGetBytesPerLine)(EVideoMode nVideoMode, EMemoryFormat nMemoryFormat, unsigned int& nBytesPerLine);
96         typedef int(__cdecl *pFunc_bfcGetBytesPerFrame)(EVideoMode nVideoMode, EMemoryFormat nMemoryFormat, EUpdateMethod nUpdateMethod, unsigned int& nBytesPerFrame);
97         typedef int(__cdecl *pFunc_bfcGetGoldenValue)(EVideoMode nVideoMode, EMemoryFormat nMemoryFormat, EUpdateMethod nUpdateMethod, unsigned int& nGoldenFrameSize);
98         typedef int(__cdecl *pFunc_bfcGetVBILines)(EVideoMode nVideoMode, EDMADataType nDataType, unsigned int& nVBILinesPerFrame);
99         typedef int(__cdecl *pFunc_bfcGetVANCGoldenValue)(unsigned int nCardType, EVideoMode nVideoMode, EMemoryFormat nMemoryFormat, EDMADataType nDataFormat, unsigned int& nVANCGoldenValue);
100         typedef int(__cdecl *pFunc_bfcGetVANCLineBytes)(unsigned int nCardType, EVideoMode nVideoMode, EMemoryFormat nMemoryFormat, unsigned int& nVANCLineBytes);
101         typedef int(__cdecl *pFunc_bfcGetVANCLineCount)(unsigned int nCardType, EVideoMode nVideoMode, EDMADataType nDataFormat, unsigned int& nVANCLineCount);
102         typedef int(__cdecl *pFunc_bfcGetWindowsDriverHandle)(BLUEVELVETC_HANDLE pHandle, HANDLE* pDriverHandle);
103         typedef int(__cdecl *pFunc_bfcSetDynamicMemoryFormatChange)(BLUEVELVETC_HANDLE pHandle, OVERLAPPED* pOverlap, unsigned int nUniqueId, EMemoryFormat nMemoryFormat);
104
105         //BlueVelvetC utils functions
106         typedef char*(__cdecl *pFunc_bfcUtilsGetStringForCardType)(const int iCardType);
107         typedef char*(__cdecl *pFunc_bfcUtilsGetStringForBlueProductId)(const unsigned int nProductId);
108         typedef char*(__cdecl *pFunc_bfcUtilsGetStringForVideoMode)(const unsigned int nVideoMode);
109         typedef char*(__cdecl *pFunc_bfcUtilsGetStringForMemoryFormat)(const unsigned int nMemoryFormat);
110         typedef int(__cdecl *pFunc_bfcUtilsGetMR2Routing)(const BLUEVELVETC_HANDLE pHandle, unsigned int& nSrcNode, const unsigned int nDestNode, unsigned int& nLinkType);
111         typedef int(__cdecl *pFunc_bfcUtilsSetMR2Routing)(const BLUEVELVETC_HANDLE pHandle, const unsigned int nSrcNode, const unsigned int nDestNode, const unsigned int nLinkType);
112         typedef int(__cdecl *pFunc_bfcUtilsGetAudioOutputRouting)(const BLUEVELVETC_HANDLE pHandle, const unsigned int nAudioConnectorType, unsigned int& nAudioSourceChannelId, unsigned int nAudioConnectorId);
113         typedef int(__cdecl *pFunc_bfcUtilsSetAudioOutputRouting)(const BLUEVELVETC_HANDLE pHandle, const unsigned int nAudioConnectorType, unsigned int nAudioSourceChannelId, unsigned int nAudioConnectorId);
114         typedef bool(__cdecl *pFunc_bfcUtilsIsVideoModeProgressive)(const unsigned int nVideoMode);
115         typedef bool(__cdecl *pFunc_bfcUtilsIsVideoMode1001Framerate)(const unsigned int nVideoMode);
116         typedef int(__cdecl *pFunc_bfcUtilsGetFpsForVideoMode)(const unsigned int nVideoMode);
117         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);
118         typedef int(__cdecl *pFunc_bfcUtilsGetFrameInfoForVideoMode)(const BLUE_UINT32 nVideoMode, BLUE_UINT32&  nWidth, BLUE_UINT32& nHeight, BLUE_UINT32& nRate, BLUE_UINT32& bIs1001, BLUE_UINT32& bIsProgressive);
119         typedef int(__cdecl *pFunc_bfcUtilsGetAudioSamplesPerFrame)(const BLUE_UINT32 nVideoMode, const BLUE_UINT32 nFrameNo);
120
121
122 public:
123
124         BvcWrapper();                                           // bfcFactory
125         ~BvcWrapper();                                          // bfcDestory
126
127         bool            IsBvcValid();
128         const char*     GetVersion();
129
130         BLUE_UINT32 Enumerate(int& iDevices);
131         BLUE_UINT32 QueryCardType(int& iCardType, int iDeviceID);
132
133         BLUE_UINT32 Attach(int iDeviceId);
134         BLUE_UINT32 Detach();
135
136         BLUE_UINT32 QueryCardProperty32(const int iProperty, unsigned int& nValue);
137         BLUE_UINT32 SetCardProperty32(const int iProperty, const unsigned int nValue);
138         
139         BLUE_UINT32 SystemBufferWrite(unsigned char* pPixels, unsigned long ulSize, unsigned long ulBufferID, unsigned long ulOffset);
140         BLUE_UINT32 SystemBufferRead(unsigned char* pPixels, unsigned long ulSize, unsigned long ulBufferID, unsigned long ulOffset);
141
142         BLUE_UINT32 VideoPlaybackStop(int iWait, int iFlush);
143         BLUE_UINT32 WaitVideoOutputSync(unsigned long ulUpdateType, unsigned long& ulFieldCount);
144         BLUE_UINT32 WaitVideoInputSync(unsigned long ulUpdateType, unsigned long & ulFieldCount);
145
146         BLUE_UINT32 RenderBufferUpdate(unsigned long ulBufferID);
147         BLUE_UINT32 RenderBufferCapture(unsigned long ulBufferID);
148
149         BLUE_UINT32 EncodeHancFrameEx(unsigned int nCardType, struct hanc_stream_info_struct* pHancEncodeInfo, void* pAudioBuffer, unsigned int nAudioChannels, unsigned int nAudioSamples, unsigned int nSampleType, unsigned int nAudioFlags);
150         BLUE_UINT32 DecodeHancFrameEx(unsigned int nCardType, unsigned int* pHancBuffer, struct hanc_decode_struct* pHancDecodeInfo);
151
152         BLUE_UINT32 GetFrameInfoForVideoVode(const unsigned int nVideoMode, unsigned int&  nWidth, unsigned int& nHeight, unsigned int& nRate, unsigned int& bIs1001, unsigned int& bIsProgressive);
153         BLUE_UINT32 GetBytesPerFrame(EVideoMode nVideoMode, EMemoryFormat nMemoryFormat, EUpdateMethod nUpdateMethod, unsigned int& nBytesPerFrame);
154
155 private:
156         bool                            InitFunctionsPointers();
157         BLUEVELVETC_HANDLE      bvc;
158 #if defined(_WIN32)
159         HMODULE                         hModule;
160 #else
161         void*                           hModule;
162 #endif
163
164 //BlueVelvetC function pointers members
165         pFunc_bfcGetVersion bfcGetVersion;
166         pFunc_bfcFactory bfcFactory;
167         pFunc_bfcDestroy bfcDestroy;
168         pFunc_bfcEnumerate bfcEnumerate;
169         pFunc_bfcQueryCardType bfcQueryCardType;
170         pFunc_bfcAttach bfcAttach;
171         pFunc_bfcDetach bfcDetach;
172         pFunc_bfcQueryCardProperty32 bfcQueryCardProperty32;
173         pFunc_bfcQueryCardProperty64 bfcQueryCardProperty64;
174         pFunc_bfcSetCardProperty32 bfcSetCardProperty32;
175         pFunc_bfcSetCardProperty64 bfcSetCardProperty64;
176         pFunc_bfcGetCardSerialNumber bfcGetCardSerialNumber;
177         pFunc_bfcGetCardFwVersion bfcGetCardFwVersion;
178         pFunc_bfcWaitVideoSyncAsync bfcWaitVideoSyncAsync;
179         pFunc_bfcWaitVideoInputSync bfcWaitVideoInputSync;
180         pFunc_bfcWaitVideoOutputSync bfcWaitVideoOutputSync;
181         pFunc_bfcGetVideoOutputCurrentFieldCount bfcGetVideoOutputCurrentFieldCount;
182         pFunc_bfcGetVideoInputCurrentFieldCount bfcGetVideoInputCurrentFieldCount;
183         pFunc_bfcVideoCaptureStart bfcVideoCaptureStart;
184         pFunc_bfcVideoCaptureStop bfcVideoCaptureStop;
185         pFunc_bfcVideoPlaybackStart bfcVideoPlaybackStart;
186         pFunc_bfcVideoPlaybackStop bfcVideoPlaybackStop;
187         pFunc_bfcVideoPlaybackAllocate bfcVideoPlaybackAllocate;
188         pFunc_bfcVideoPlaybackPresent bfcVideoPlaybackPresent;
189         pFunc_bfcVideoPlaybackRelease bfcVideoPlaybackRelease;
190         pFunc_bfcGetCaptureVideoFrameInfoEx bfcGetCaptureVideoFrameInfoEx;
191         pFunc_bfcRenderBufferCapture bfcRenderBufferCapture;
192         pFunc_bfcRenderBufferUpdate bfcRenderBufferUpdate;
193         pFunc_bfcGetRenderBufferCount bfcGetRenderBufferCount;
194         pFunc_bfcEncodeHancFrameEx bfcEncodeHancFrameEx;
195         pFunc_bfcDecodeHancFrameEx bfcDecodeHancFrameEx;
196 #if defined(_WIN32)
197         pFunc_bfcSystemBufferReadAsync bfcSystemBufferReadAsync;
198         pFunc_bfcSystemBufferWriteAsync bfcSystemBufferWriteAsync;
199 #else
200         pFunc_bfcSystemBufferRead bfcSystemBufferRead;
201     pFunc_bfcSystemBufferWrite bfcSystemBufferWrite;
202 #endif
203         pFunc_bfcGetBytesForGroupPixels bfcGetBytesForGroupPixels;
204         pFunc_bfcGetPixelsPerLine bfcGetPixelsPerLine;
205         pFunc_bfcGetLinesPerFrame bfcGetLinesPerFrame;
206         pFunc_bfcGetBytesPerLine bfcGetBytesPerLine;
207         pFunc_bfcGetBytesPerFrame bfcGetBytesPerFrame;
208         pFunc_bfcGetGoldenValue bfcGetGoldenValue;
209         pFunc_bfcGetVBILines bfcGetVBILines;
210         pFunc_bfcGetVANCGoldenValue bfcGetVANCGoldenValue;
211         pFunc_bfcGetVANCLineBytes bfcGetVANCLineBytes;
212         pFunc_bfcGetVANCLineCount bfcGetVANCLineCount;
213         pFunc_bfcGetWindowsDriverHandle bfcGetWindowsDriverHandle;
214         pFunc_bfcUtilsGetStringForCardType bfcUtilsGetStringForCardType;
215         pFunc_bfcUtilsGetStringForBlueProductId bfcUtilsGetStringForBlueProductId;
216         pFunc_bfcUtilsGetStringForVideoMode bfcUtilsGetStringForVideoMode;
217         pFunc_bfcUtilsGetStringForMemoryFormat bfcUtilsGetStringForMemoryFormat;
218         pFunc_bfcUtilsGetMR2Routing bfcUtilsGetMR2Routing;
219         pFunc_bfcUtilsSetMR2Routing bfcUtilsSetMR2Routing;
220         pFunc_bfcUtilsGetAudioOutputRouting bfcUtilsGetAudioOutputRouting;
221         pFunc_bfcUtilsSetAudioOutputRouting bfcUtilsSetAudioOutputRouting;
222         pFunc_bfcUtilsIsVideoModeProgressive bfcUtilsIsVideoModeProgressive;
223         pFunc_bfcUtilsIsVideoMode1001Framerate bfcUtilsIsVideoMode1001Framerate;
224         pFunc_bfcUtilsGetFpsForVideoMode bfcUtilsGetFpsForVideoMode;
225         pFunc_bfcUtilsGetVideoModeForFrameInfo bfcUtilsGetVideoModeForFrameInfo;
226         pFunc_bfcUtilsGetFrameInfoForVideoMode bfcUtilsGetFrameInfoForVideoMode;
227         pFunc_bfcUtilsGetAudioSamplesPerFrame bfcUtilsGetAudioSamplesPerFrame;
228
229 };
230
231 spl::shared_ptr<BvcWrapper> create_blue();
232 spl::shared_ptr<BvcWrapper> create_blue(int device_index);
233
234 //spl::shared_ptr<BvcWrapper> create_blue();
235
236 bool is_epoch_card(BvcWrapper& blue);
237 bool is_epoch_neutron_1i2o_card(BvcWrapper& blue);
238 bool is_epoch_neutron_3o_card(BvcWrapper& blue);
239 std::wstring get_card_desc(BvcWrapper& blue, int device_index);
240 EVideoMode get_video_mode(BvcWrapper& blue, const core::video_format_desc& format_desc);
241 core::video_format_desc get_format_desc(BvcWrapper& blue, EVideoMode vid_fmt, EMemoryFormat mem_fmt);
242
243
244 }}