]> git.sesse.net Git - casparcg/blob - modules/bluefish/util/blue_velvet.cpp
Update previous files to be more in line with CasparCG coding styles.
[casparcg] / modules / bluefish / util / blue_velvet.cpp
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 #include "../StdAfx.h"
23 #include "blue_velvet.h"
24 #include <common/utf.h> 
25 #include <core/video_format.h>
26 #include <BlueVelvetCUtils.h>
27
28 #if defined(__APPLE__)
29 #include <dlfcn.h>
30 #endif
31
32 #if defined(_WIN32)
33         #define GET_PROCADDR_FOR_FUNC(name, module) { name = (pFunc_##name)GetProcAddress(reinterpret_cast<HMODULE>(module), #name); if(!name) { return false; } }
34 //      #define GET_PROCADDR_FOR_FUNC(name, module) { name = (pFunc_##name)GetProcAddress(module, #name); if(!name) { return false; } }
35 #elif defined(__APPLE__)
36         #define GET_PROCADDR_FOR_FUNC(name, module) { name = (pFunc_##name)dlsym(module, #name); if(!name) { return false; } }
37 #endif
38
39 namespace caspar { namespace bluefish {
40
41         bvc_wrapper::bvc_wrapper()
42         {
43                 bvc_ = nullptr;
44                 h_module_ = nullptr;
45                 init_function_pointers();
46                 bvc_ = std::shared_ptr<void>(bfcFactory(), bfcDestroy);
47                 
48                 if (!bvc_)
49                         CASPAR_THROW_EXCEPTION(not_supported() << msg_info("Bluefish drivers not found."));
50         }
51
52         bool bvc_wrapper::init_function_pointers()
53         {
54                 bool res = false;
55                 bfcGetVersion = nullptr;
56                 bfcFactory = nullptr;
57                 bfcDestroy = nullptr;
58                 bfcEnumerate = nullptr;
59                 bfcQueryCardType = nullptr;
60                 bfcAttach = nullptr;
61                 bfcDetach = nullptr;
62                 bfcQueryCardProperty32 = nullptr;
63                 bfcQueryCardProperty64 = nullptr;
64                 bfcSetCardProperty32 = nullptr;
65                 bfcSetCardProperty64 = nullptr;
66                 bfcGetCardSerialNumber = nullptr;
67                 bfcGetCardFwVersion = nullptr;
68                 bfcWaitVideoSyncAsync = nullptr;
69                 bfcWaitVideoInputSync = nullptr;
70                 bfcWaitVideoOutputSync = nullptr;
71                 bfcGetVideoOutputCurrentFieldCount = nullptr;
72                 bfcGetVideoInputCurrentFieldCount = nullptr;
73                 bfcVideoCaptureStart = nullptr;
74                 bfcVideoCaptureStop = nullptr;
75                 bfcVideoPlaybackStart = nullptr;
76                 bfcVideoPlaybackStop = nullptr;
77                 bfcVideoPlaybackAllocate = nullptr;
78                 bfcVideoPlaybackPresent = nullptr;
79                 bfcVideoPlaybackRelease = nullptr;
80                 bfcGetCaptureVideoFrameInfoEx = nullptr;
81                 bfcRenderBufferCapture = nullptr;
82                 bfcRenderBufferUpdate = nullptr;
83                 bfcGetRenderBufferCount = nullptr;
84                 bfcEncodeHancFrameEx = nullptr;
85                 bfcDecodeHancFrameEx = nullptr;
86 #if defined(_WIN32)
87                 bfcSystemBufferReadAsync = nullptr;
88                 bfcSystemBufferWriteAsync = nullptr;
89 #else
90                 bfcSystemBufferRead = nullptr;
91                 bfcSystemBufferWrite = nullptr;
92 #endif
93                 bfcGetBytesForGroupPixels = nullptr;
94                 bfcGetPixelsPerLine = nullptr;
95                 bfcGetLinesPerFrame = nullptr;
96                 bfcGetBytesPerLine = nullptr;
97                 bfcGetBytesPerFrame = nullptr;
98                 bfcGetGoldenValue = nullptr;
99                 bfcGetVBILines = nullptr;
100                 bfcGetVANCGoldenValue = nullptr;
101                 bfcGetVANCLineBytes = nullptr;
102                 bfcGetVANCLineCount = nullptr;
103                 bfcGetWindowsDriverHandle = nullptr;
104                 bfcUtilsGetStringForCardType = nullptr;
105                 bfcUtilsGetStringForBlueProductId = nullptr;
106                 bfcUtilsGetStringForVideoMode = nullptr;
107                 bfcUtilsGetStringForMemoryFormat = nullptr;
108                 bfcUtilsGetMR2Routing = nullptr;
109                 bfcUtilsSetMR2Routing = nullptr;
110                 bfcUtilsGetAudioOutputRouting = nullptr;
111                 bfcUtilsSetAudioOutputRouting = nullptr;
112                 bfcUtilsIsVideoModeProgressive = nullptr;
113                 bfcUtilsIsVideoMode1001Framerate = nullptr;
114                 bfcUtilsGetFpsForVideoMode = nullptr;
115                 bfcUtilsGetVideoModeForFrameInfo = nullptr;
116                 bfcUtilsGetFrameInfoForVideoMode = nullptr;
117                 bfcUtilsGetAudioSamplesPerFrame = nullptr;
118
119 #if defined(_WIN32)
120 #ifdef _DEBUG
121                 h_module_ = std::shared_ptr<void>(LoadLibraryExA("BlueVelvetC64_d.dll", NULL, 0), FreeLibrary);
122 #else
123                 h_module_ = std::shared_ptr<void>(LoadLibraryExA("BlueVelvetC64.dll", NULL, 0), FreeLibrary);
124 #endif
125                 
126 #elif defined(__APPLE__)
127                 // Look for the framework and load it accordingly.
128                 char* libraryPath("/Library/Frameworks/BlueVelvetC.framework");         // full path may not be required, OSX might check in /l/f by default - MUST TEST!
129                 h_module_ = std::shared_ptr<void>(dlopen(libraryPath, RTLD_NOW), dlclose);
130 #endif
131                 
132                 if (h_module_)
133                 {
134                         GET_PROCADDR_FOR_FUNC(bfcGetVersion, h_module_.get());
135                         GET_PROCADDR_FOR_FUNC(bfcFactory, h_module_.get());
136                         GET_PROCADDR_FOR_FUNC(bfcDestroy, h_module_.get());
137                         GET_PROCADDR_FOR_FUNC(bfcEnumerate, h_module_.get());
138                         GET_PROCADDR_FOR_FUNC(bfcQueryCardType, h_module_.get());
139                         GET_PROCADDR_FOR_FUNC(bfcAttach, h_module_.get());
140                         GET_PROCADDR_FOR_FUNC(bfcDetach, h_module_.get());
141                         GET_PROCADDR_FOR_FUNC(bfcQueryCardProperty32, h_module_.get());
142                         GET_PROCADDR_FOR_FUNC(bfcQueryCardProperty64, h_module_.get());
143                         GET_PROCADDR_FOR_FUNC(bfcSetCardProperty32, h_module_.get());
144                         GET_PROCADDR_FOR_FUNC(bfcSetCardProperty64, h_module_.get());
145                         GET_PROCADDR_FOR_FUNC(bfcGetCardSerialNumber, h_module_.get());
146                         GET_PROCADDR_FOR_FUNC(bfcGetCardFwVersion, h_module_.get());
147                         GET_PROCADDR_FOR_FUNC(bfcWaitVideoSyncAsync, h_module_.get());
148                         GET_PROCADDR_FOR_FUNC(bfcWaitVideoInputSync, h_module_.get());
149                         GET_PROCADDR_FOR_FUNC(bfcWaitVideoOutputSync, h_module_.get());
150                         GET_PROCADDR_FOR_FUNC(bfcGetVideoOutputCurrentFieldCount, h_module_.get());
151                         GET_PROCADDR_FOR_FUNC(bfcGetVideoInputCurrentFieldCount, h_module_.get());
152                         GET_PROCADDR_FOR_FUNC(bfcVideoCaptureStart, h_module_.get());
153                         GET_PROCADDR_FOR_FUNC(bfcVideoCaptureStop, h_module_.get());
154                         GET_PROCADDR_FOR_FUNC(bfcVideoPlaybackStart, h_module_.get());
155                         GET_PROCADDR_FOR_FUNC(bfcVideoPlaybackStop, h_module_.get());
156                         GET_PROCADDR_FOR_FUNC(bfcVideoPlaybackAllocate, h_module_.get());
157                         GET_PROCADDR_FOR_FUNC(bfcVideoPlaybackPresent, h_module_.get());
158                         GET_PROCADDR_FOR_FUNC(bfcVideoPlaybackRelease, h_module_.get());
159                         GET_PROCADDR_FOR_FUNC(bfcGetCaptureVideoFrameInfoEx, h_module_.get());
160                         GET_PROCADDR_FOR_FUNC(bfcRenderBufferCapture, h_module_.get());
161                         GET_PROCADDR_FOR_FUNC(bfcRenderBufferUpdate, h_module_.get());
162                         GET_PROCADDR_FOR_FUNC(bfcGetRenderBufferCount, h_module_.get());
163                         GET_PROCADDR_FOR_FUNC(bfcEncodeHancFrameEx, h_module_.get());
164                         GET_PROCADDR_FOR_FUNC(bfcDecodeHancFrameEx, h_module_.get());
165                         GET_PROCADDR_FOR_FUNC(bfcSystemBufferReadAsync, h_module_.get());
166                         GET_PROCADDR_FOR_FUNC(bfcSystemBufferWriteAsync, h_module_.get());
167                         GET_PROCADDR_FOR_FUNC(bfcGetBytesForGroupPixels, h_module_.get());
168                         GET_PROCADDR_FOR_FUNC(bfcGetPixelsPerLine, h_module_.get());
169                         GET_PROCADDR_FOR_FUNC(bfcGetLinesPerFrame, h_module_.get());
170                         GET_PROCADDR_FOR_FUNC(bfcGetBytesPerLine, h_module_.get());
171                         GET_PROCADDR_FOR_FUNC(bfcGetBytesPerFrame, h_module_.get());
172                         GET_PROCADDR_FOR_FUNC(bfcGetGoldenValue, h_module_.get());
173                         GET_PROCADDR_FOR_FUNC(bfcGetVBILines, h_module_.get());
174                         GET_PROCADDR_FOR_FUNC(bfcGetVANCGoldenValue, h_module_.get());
175                         GET_PROCADDR_FOR_FUNC(bfcGetVANCLineBytes, h_module_.get());
176                         GET_PROCADDR_FOR_FUNC(bfcGetVANCLineCount, h_module_.get());
177                         GET_PROCADDR_FOR_FUNC(bfcGetWindowsDriverHandle, h_module_.get());
178                         GET_PROCADDR_FOR_FUNC(bfcUtilsGetStringForCardType, h_module_.get());
179                         GET_PROCADDR_FOR_FUNC(bfcUtilsGetStringForBlueProductId, h_module_.get());
180                         GET_PROCADDR_FOR_FUNC(bfcUtilsGetStringForVideoMode, h_module_.get());
181                         GET_PROCADDR_FOR_FUNC(bfcUtilsGetStringForMemoryFormat, h_module_.get());
182                         GET_PROCADDR_FOR_FUNC(bfcUtilsGetMR2Routing, h_module_.get());
183                         GET_PROCADDR_FOR_FUNC(bfcUtilsSetMR2Routing, h_module_.get());
184                         GET_PROCADDR_FOR_FUNC(bfcUtilsGetAudioOutputRouting, h_module_.get());
185                         GET_PROCADDR_FOR_FUNC(bfcUtilsSetAudioOutputRouting, h_module_.get());
186                         GET_PROCADDR_FOR_FUNC(bfcUtilsIsVideoModeProgressive, h_module_.get());
187                         GET_PROCADDR_FOR_FUNC(bfcUtilsIsVideoMode1001Framerate, h_module_.get());
188                         GET_PROCADDR_FOR_FUNC(bfcUtilsGetFpsForVideoMode, h_module_.get());
189                         GET_PROCADDR_FOR_FUNC(bfcUtilsGetVideoModeForFrameInfo, h_module_.get());
190                         GET_PROCADDR_FOR_FUNC(bfcUtilsGetFrameInfoForVideoMode, h_module_.get());
191                         GET_PROCADDR_FOR_FUNC(bfcUtilsGetAudioSamplesPerFrame, h_module_.get());
192                         res = true;
193                 }
194                 return res;
195         }
196
197         const char * bvc_wrapper::get_version()
198         {
199                 return bfcGetVersion();
200         }
201
202         BLUE_UINT32 bvc_wrapper::attach(int iDeviceId)
203         {
204                 return bfcAttach((BLUEVELVETC_HANDLE)bvc_.get(), iDeviceId);
205         }
206
207         BLUE_UINT32 bvc_wrapper::detach()
208         {
209                 return bfcDetach((BLUEVELVETC_HANDLE)bvc_.get());
210         }
211
212         BLUE_UINT32 bvc_wrapper::get_card_property32(const int iProperty, unsigned int & nValue)
213         {
214                 if (bvc_)
215                         return (BLUE_UINT32)bfcQueryCardProperty32((BLUEVELVETC_HANDLE)bvc_.get(), iProperty, nValue);
216                 else
217                         return 0;
218         }
219
220         BLUE_UINT32 bvc_wrapper::set_card_property32(const int iProperty, const unsigned int nValue)
221         {
222                 return bfcSetCardProperty32((BLUEVELVETC_HANDLE)bvc_.get(), iProperty, nValue);
223         }
224
225         BLUE_UINT32 bvc_wrapper::enumerate(int & iDevices)
226         {
227                 return bfcEnumerate((BLUEVELVETC_HANDLE)bvc_.get(), iDevices);
228         }
229
230         BLUE_UINT32 bvc_wrapper::query_card_type(int & iCardType, int iDeviceID)
231         {
232                 return bfcQueryCardType((BLUEVELVETC_HANDLE)bvc_.get(), iCardType, iDeviceID);
233         }
234
235         BLUE_UINT32 bvc_wrapper::system_buffer_write(unsigned char * pPixels, unsigned long ulSize, unsigned long ulBufferID, unsigned long ulOffset)
236         {
237                 return bfcSystemBufferWriteAsync((BLUEVELVETC_HANDLE)bvc_.get(), pPixels, ulSize, nullptr, ulBufferID, ulOffset);
238         }
239
240         BLUE_UINT32 bvc_wrapper::system_buffer_read(unsigned char* pPixels, unsigned long ulSize, unsigned long ulBufferID, unsigned long ulOffset)
241         {
242                 return bfcSystemBufferReadAsync((BLUEVELVETC_HANDLE)bvc_.get(), pPixels, ulSize, nullptr, ulBufferID, ulOffset);
243         }
244
245         BLUE_UINT32 bvc_wrapper::video_playback_stop(int iWait, int iFlush)
246         {
247                 return bfcVideoPlaybackStop((BLUEVELVETC_HANDLE)bvc_.get(), iWait, iFlush);
248         }
249
250         BLUE_UINT32 bvc_wrapper::wait_video_output_sync(unsigned long ulUpdateType, unsigned long & ulFieldCount)
251         {
252                 return bfcWaitVideoOutputSync((BLUEVELVETC_HANDLE)bvc_.get(), ulUpdateType, ulFieldCount);
253         }
254
255         BLUE_UINT32 bvc_wrapper::wait_video_input_sync(unsigned long ulUpdateType, unsigned long & ulFieldCount)
256         {
257                 return bfcWaitVideoInputSync((BLUEVELVETC_HANDLE)bvc_.get(), ulUpdateType, ulFieldCount);
258         }
259
260         BLUE_UINT32 bvc_wrapper::render_buffer_update( unsigned long ulBufferID)
261         {
262                 return bfcRenderBufferUpdate((BLUEVELVETC_HANDLE)bvc_.get(), ulBufferID);
263         }
264
265         BLUE_UINT32 bvc_wrapper::render_buffer_capture(unsigned long ulBufferID)
266         {
267                 return bfcRenderBufferCapture((BLUEVELVETC_HANDLE)bvc_.get(), ulBufferID);
268         }
269
270         BLUE_UINT32 bvc_wrapper::encode_hanc_frame(unsigned int nCardType, hanc_stream_info_struct * pHancEncodeInfo, void * pAudioBuffer, unsigned int nAudioChannels, unsigned int nAudioSamples, unsigned int nSampleType, unsigned int nAudioFlags)
271         {
272                 return bfcEncodeHancFrameEx((BLUEVELVETC_HANDLE)bvc_.get(), CRD_BLUE_NEUTRON, pHancEncodeInfo, pAudioBuffer, nAudioChannels, nAudioSamples, nSampleType, nAudioFlags);
273         }
274
275         BLUE_UINT32 bvc_wrapper::decode_hanc_frame(unsigned int nCardType, unsigned int * pHancBuffer, hanc_decode_struct * pHancDecodeInfo)
276         {
277                 return bfcDecodeHancFrameEx((BLUEVELVETC_HANDLE)bvc_.get(), CRD_BLUE_NEUTRON, pHancBuffer, pHancDecodeInfo);
278         }
279
280         BLUE_UINT32 bvc_wrapper::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)
281         {
282                 return bfcUtilsGetFrameInfoForVideoMode(nVideoMode, nWidth, nHeight, nRate, bIs1001, bIsProgressive);
283         }
284
285         BLUE_UINT32 bvc_wrapper::get_bytes_per_frame(EVideoMode nVideoMode, EMemoryFormat nMemoryFormat, EUpdateMethod nUpdateMethod, unsigned int& nBytesPerFrame)
286         {
287                 return bfcGetBytesPerFrame(nVideoMode, nMemoryFormat, nUpdateMethod, nBytesPerFrame);
288         }
289
290
291 EVideoMode vid_fmt_from_video_format(const core::video_format& fmt) 
292 {
293         switch(fmt)
294         {
295         case core::video_format::pal:                   return VID_FMT_PAL;
296         case core::video_format::ntsc:                  return VID_FMT_NTSC;
297         case core::video_format::x576p2500:             return VID_FMT_INVALID; //not supported
298         case core::video_format::x720p2398:             return VID_FMT_720P_2398;
299         case core::video_format::x720p2400:             return VID_FMT_720P_2400;
300         case core::video_format::x720p2500:             return VID_FMT_720P_2500;
301         case core::video_format::x720p5000:             return VID_FMT_720P_5000;
302         case core::video_format::x720p2997:             return VID_FMT_720P_2997;
303         case core::video_format::x720p5994:             return VID_FMT_720P_5994;
304         case core::video_format::x720p3000:             return VID_FMT_720P_3000;
305         case core::video_format::x720p6000:             return VID_FMT_720P_6000;
306         case core::video_format::x1080p2398:    return VID_FMT_1080P_2397;
307         case core::video_format::x1080p2400:    return VID_FMT_1080P_2400;
308         case core::video_format::x1080i5000:    return VID_FMT_1080I_5000;
309         case core::video_format::x1080i5994:    return VID_FMT_1080I_5994;
310         case core::video_format::x1080i6000:    return VID_FMT_1080I_6000;
311         case core::video_format::x1080p2500:    return VID_FMT_1080P_2500;
312         case core::video_format::x1080p2997:    return VID_FMT_1080P_2997;
313         case core::video_format::x1080p3000:    return VID_FMT_1080P_3000;
314         case core::video_format::x1080p5000:    return VID_FMT_1080P_5000;
315         case core::video_format::x1080p5994:    return VID_FMT_1080P_5994;
316         case core::video_format::x1080p6000:    return VID_FMT_1080P_6000;
317         default:                                                                return VID_FMT_INVALID;
318         }
319 }
320
321 bool is_epoch_card(bvc_wrapper& blue)
322 {
323         int device_id = 1;
324         int card_type = 0;
325         blue.query_card_type(card_type, device_id);
326
327         switch(card_type)
328         {
329                 case CRD_BLUE_EPOCH_HORIZON:
330                 case CRD_BLUE_EPOCH_CORE:
331                 case CRD_BLUE_EPOCH_ULTRA:
332                 case CRD_BLUE_EPOCH_2K_HORIZON:
333                 case CRD_BLUE_EPOCH_2K_CORE:
334                 case CRD_BLUE_EPOCH_2K_ULTRA:
335                 case CRD_BLUE_CREATE_HD:
336                 case CRD_BLUE_CREATE_2K:
337                 case CRD_BLUE_CREATE_2K_ULTRA:
338                 case CRD_BLUE_SUPER_NOVA:
339                 case CRD_BLUE_SUPER_NOVA_S_PLUS:
340                 case CRD_BLUE_NEUTRON:
341                 case CRD_BLUE_EPOCH_CG:
342                 return true;
343         default:
344                 return false;
345         }
346 }
347
348 bool is_epoch_neutron_1i2o_card(bvc_wrapper& blue)
349 {
350         BLUE_UINT32 val = 0;
351         blue.get_card_property32(EPOCH_GET_PRODUCT_ID, val);
352         if (val == ORAC_NEUTRON_1_IN_2_OUT_FIRMWARE_PRODUCTID)
353                 return true;
354         else
355                 return false;
356 }
357
358 bool is_epoch_neutron_3o_card(bvc_wrapper& blue)
359 {
360         BLUE_UINT32 val = 0;
361         blue.get_card_property32(EPOCH_GET_PRODUCT_ID, val);
362
363         if (val == ORAC_NEUTRON_0_IN_3_OUT_FIRMWARE_PRODUCTID)
364                 return true;
365         else
366                 return false;
367 }
368
369 std::wstring get_card_desc(bvc_wrapper& blue, int device_id)
370 {
371         int card_type = 0;
372         blue.query_card_type(card_type, device_id);
373
374         switch(card_type)
375         {
376                 case CRD_BLUEDEEP_LT:                           return L"Deepblue LT";// D64 Lite
377                 case CRD_BLUEDEEP_SD:                           return L"Iridium SD";// Iridium SD
378                 case CRD_BLUEDEEP_AV:                           return L"Iridium AV";// Iridium AV
379                 case CRD_BLUEDEEP_IO:                           return L"Deepblue IO";// D64 Full
380                 case CRD_BLUEWILD_AV:                           return L"Wildblue AV";// D64 AV
381                 case CRD_IRIDIUM_HD:                            return L"Iridium HD";// * Iridium HD
382                 case CRD_BLUEWILD_RT:                           return L"Wildblue RT";// D64 RT
383                 case CRD_BLUEWILD_HD:                           return L"Wildblue HD";// * BadAss G2
384                 case CRD_REDDEVIL:                                      return L"Iridium Full";// Iridium Full
385                 case CRD_BLUEDEEP_HD:   
386                 case CRD_BLUEDEEP_HDS:                          return L"Reserved for \"BasAss G2";// * BadAss G2 variant, proposed, reserved
387                 case CRD_BLUE_ENVY:                                     return L"Blue Envy"; // Mini Din 
388                 case CRD_BLUE_PRIDE:                            return L"Blue Pride";//Mini Din Output 
389                 case CRD_BLUE_GREED:                            return L"Blue Greed";
390                 case CRD_BLUE_INGEST:                           return L"Blue Ingest";
391                 case CRD_BLUE_SD_DUALLINK:                      return L"Blue SD Duallink";
392                 case CRD_BLUE_CATALYST:                         return L"Blue Catalyst";
393                 case CRD_BLUE_SD_DUALLINK_PRO:          return L"Blue SD Duallink Pro";
394                 case CRD_BLUE_SD_INGEST_PRO:            return L"Blue SD Ingest pro";
395                 case CRD_BLUE_SD_DEEPBLUE_LITE_PRO:     return L"Blue SD Deepblue lite Pro";
396                 case CRD_BLUE_SD_SINGLELINK_PRO:        return L"Blue SD Singlelink Pro";
397                 case CRD_BLUE_SD_IRIDIUM_AV_PRO:        return L"Blue SD Iridium AV Pro";
398                 case CRD_BLUE_SD_FIDELITY:                      return L"Blue SD Fidelity";
399                 case CRD_BLUE_SD_FOCUS:                         return L"Blue SD Focus";
400                 case CRD_BLUE_SD_PRIME:                         return L"Blue SD Prime";
401                 case CRD_BLUE_EPOCH_2K_CORE:            return L"Blue Epoch 2K Core";
402                 case CRD_BLUE_EPOCH_2K_ULTRA:           return L"Blue Epoch 2K Ultra";
403                 case CRD_BLUE_EPOCH_HORIZON:            return L"Blue Epoch Horizon";
404                 case CRD_BLUE_EPOCH_CORE:                       return L"Blue Epoch Core";
405                 case CRD_BLUE_EPOCH_ULTRA:                      return L"Blue Epoch Ultra";
406                 case CRD_BLUE_CREATE_HD:                        return L"Blue Create HD";
407                 case CRD_BLUE_CREATE_2K:                        return L"Blue Create 2K";
408                 case CRD_BLUE_CREATE_2K_ULTRA:          return L"Blue Create 2K Ultra";
409                 case CRD_BLUE_SUPER_NOVA:                       return L"Blue SuperNova";
410                 case CRD_BLUE_SUPER_NOVA_S_PLUS:        return L"Blue SuperNova s+";
411                 case CRD_BLUE_NEUTRON:                          return L"Blue Neutron 4k";
412                 case CRD_BLUE_EPOCH_CG:                         return L"Blue Epopch CG";
413                 default:                                                        return L"Unknown";
414         }
415 }
416
417 EVideoMode get_video_mode(bvc_wrapper& blue, const core::video_format_desc& format_desc)
418 {
419         EVideoMode vid_fmt = VID_FMT_INVALID;
420         BLUE_UINT32 invalid_fmt = 0;
421         BErr err = 0;
422         err = blue.get_card_property32(INVALID_VIDEO_MODE_FLAG, invalid_fmt);
423         vid_fmt = vid_fmt_from_video_format(format_desc.format);
424
425         if (vid_fmt == VID_FMT_INVALID)
426                 CASPAR_THROW_EXCEPTION(not_supported() << msg_info(L"video-mode not supported: " + format_desc.name));
427
428
429         if ((unsigned int)vid_fmt >= invalid_fmt)
430                 CASPAR_THROW_EXCEPTION(not_supported() << msg_info(L"video-mode not supported - Outside of valid range: " + format_desc.name));
431
432         return vid_fmt;
433 }
434
435 spl::shared_ptr<bvc_wrapper> create_blue()
436 {
437         auto pWrap = new bvc_wrapper();
438         return spl::shared_ptr<bvc_wrapper>(pWrap);
439 }
440
441 spl::shared_ptr<bvc_wrapper> create_blue(int device_index)
442 {
443         auto blue = create_blue();
444         
445         if(BLUE_FAIL(blue->attach(device_index)))
446                 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info("Failed to attach device."));
447
448         return blue;
449 }
450
451 core::video_format video_format_from_vid_fmt(EVideoMode fmt)
452 {
453         switch (fmt)
454         {
455         case VID_FMT_PAL: return core::video_format::pal;
456         case VID_FMT_NTSC: return core::video_format::ntsc;
457         case VID_FMT_720P_2398: return core::video_format::x720p2398;
458         case VID_FMT_720P_2400: return core::video_format::x720p2400;
459         case VID_FMT_720P_2500: return core::video_format::x720p2500;
460         case VID_FMT_720P_5000: return core::video_format::x720p5000;
461         case VID_FMT_720P_2997: return core::video_format::x720p2997;
462         case VID_FMT_720P_5994: return core::video_format::x720p5994;
463         case VID_FMT_720P_3000: return core::video_format::x720p3000;
464         case VID_FMT_720P_6000: return core::video_format::x720p6000;
465         case VID_FMT_1080P_2397: return core::video_format::x1080p2398;
466         case VID_FMT_1080P_2400: return core::video_format::x1080p2400;
467         case VID_FMT_1080I_5000: return core::video_format::x1080i5000;
468         case VID_FMT_1080I_5994: return core::video_format::x1080i5994;
469         case VID_FMT_1080I_6000: return core::video_format::x1080i6000;
470         case VID_FMT_1080P_2500: return core::video_format::x1080p2500;
471         case VID_FMT_1080P_2997: return core::video_format::x1080p2997;
472         case VID_FMT_1080P_3000: return core::video_format::x1080p3000;
473         case VID_FMT_1080P_5000: return core::video_format::x1080p5000;
474         case VID_FMT_1080P_5994: return core::video_format::x1080p5994;
475         case VID_FMT_1080P_6000: return core::video_format::x1080p6000;
476         default: return core::video_format::invalid;
477         }
478 }
479
480 core::video_format_desc get_format_desc(bvc_wrapper& blue, EVideoMode vid_fmt, EMemoryFormat mem_fmt)
481 {
482         core::video_format_desc fmt;
483         unsigned int width, height, duration = 0, time_scale = 0, rate = 0, bIs1001 = 0, bIsProgressive = 0, size = 0;
484         std::vector<int>        audio_cadence;
485         core::field_mode video_field_mode = core::field_mode::progressive;
486
487         blue.get_frame_info_for_video_mode(vid_fmt, width, height, rate, bIs1001, bIsProgressive);
488         blue.get_bytes_per_frame(vid_fmt, mem_fmt, UPD_FMT_FRAME, size);
489
490         switch (vid_fmt)
491         {
492         case VID_FMT_NTSC:
493         case VID_FMT_1080I_5994:
494                 duration = 30000;
495                 time_scale = 1001;
496                 audio_cadence = { 1601,1602,1601,1602,1602 };
497                 video_field_mode = core::field_mode::upper;
498                 break;
499         case VID_FMT_2048_1080P_2500:
500         case VID_FMT_2048_1080PSF_2500:
501         case VID_FMT_576I_5000:
502         case VID_FMT_1080P_2500:
503         case VID_FMT_1080I_5000:
504         case VID_FMT_1080PSF_2500:
505         case VID_FMT_720P_2500:
506                 duration = 25000;
507                 time_scale = 1000;
508                 audio_cadence = { 1920,1920,1920,1920,1920 };
509                 break;
510
511         case VID_FMT_720P_5994:
512         case VID_FMT_2048_1080P_5994:
513         case VID_FMT_1080P_5994:
514                 duration = 60000;
515                 time_scale = 1001;
516                 audio_cadence = { 801,800,801,800,800 };
517                 break;
518
519         case VID_FMT_1080P_6000:
520         case VID_FMT_2048_1080P_6000:
521         case VID_FMT_720P_6000:
522                 duration = 60000;
523                 time_scale = 1000;
524                 audio_cadence = { 801,800,801,800,800 };
525                 break;
526
527         case VID_FMT_1080PSF_2397:
528         case VID_FMT_1080P_2397:
529         case VID_FMT_720P_2398:
530         case VID_FMT_2048_1080PSF_2397:
531         case VID_FMT_2048_1080P_2397:
532                 duration = 24000;
533                 time_scale = 1000;
534                 break;
535
536         case VID_FMT_1080PSF_2400:
537         case VID_FMT_1080P_2400:
538         case VID_FMT_720P_2400:
539         case VID_FMT_2048_1080PSF_2400:
540         case VID_FMT_2048_1080P_2400:
541                 duration = 24000;
542                 time_scale = 1000;
543                 break;
544
545         case VID_FMT_1080I_6000:
546         case VID_FMT_1080PSF_3000:
547                 duration = 30000;
548                 time_scale = 1000;
549                 break;
550
551         case VID_FMT_720P_2997:
552         case VID_FMT_1080P_2997:
553         case VID_FMT_2048_1080PSF_2997:
554         case VID_FMT_2048_1080P_2997:
555         case VID_FMT_1080PSF_2997:
556                 duration = 30000;
557                 time_scale = 1001;
558                 break;
559
560         case VID_FMT_720P_3000:
561         case VID_FMT_1080P_3000:
562         case VID_FMT_2048_1080PSF_3000:
563         case VID_FMT_2048_1080P_3000:
564                 duration = 30000;
565                 time_scale = 1001;
566                 break;
567
568         case VID_FMT_720P_5000:
569         case VID_FMT_1080P_5000:
570         case VID_FMT_2048_1080P_5000:
571                 audio_cadence = { 960,960,960,960,960 };
572                 duration = 50000;
573                 time_scale = 1000;
574                 break;
575                 /*case VID_FMT_1080P_4800:
576                 case VID_FMT_2048_1080P_4800:
577                 fmt.duration = 48000;
578                 fmt.time_scale = 1000;
579                 break;*/
580         }
581         fmt = core::video_format_desc(video_format_from_vid_fmt(vid_fmt), width, height, width, height, video_field_mode, time_scale, duration, std::wstring(L""), audio_cadence);
582         fmt.size = size;
583         return fmt;
584 }
585
586 }}