]> git.sesse.net Git - casparcg/blob - modules/bluefish/util/blue_velvet.cpp
b5f45d5118de75959d509d96300b9185ebdeddcb
[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
24 #include "blue_velvet.h"
25
26 #include <common/utf.h>
27
28 #include <core/video_format.h>
29
30 namespace caspar { namespace bluefish {
31         
32 CBlueVelvet4* (*BlueVelvetFactory4)() = nullptr;
33 void (*BlueVelvetDestroy)(CBlueVelvet4* pObj) = nullptr;
34 const char*     (*BlueVelvetVersion)() = nullptr;
35 BLUE_UINT32 (*encode_hanc_frame)(struct hanc_stream_info_struct * hanc_stream_ptr, void * audio_pcm_ptr,BLUE_UINT32 no_audio_ch,BLUE_UINT32 no_audio_samples,BLUE_UINT32 nTypeOfSample,BLUE_UINT32 emb_audio_flag) = nullptr;
36 BLUE_UINT32 (*encode_hanc_frame_ex)(BLUE_UINT32 card_type, struct hanc_stream_info_struct * hanc_stream_ptr, void * audio_pcm_ptr, BLUE_UINT32 no_audio_ch,     BLUE_UINT32 no_audio_samples, BLUE_UINT32 nTypeOfSample, BLUE_UINT32 emb_audio_flag) = nullptr;
37
38 void blue_velvet_initialize()
39 {
40 #ifdef _DEBUG
41         std::string module_str = "BlueVelvet64_d.dll";
42 #else
43         std::string module_str = "BlueVelvet64.dll";
44 #endif
45
46         auto module = LoadLibrary(u16(module_str).c_str());
47         if(!module)
48                 LoadLibrary(u16(std::string(getenv("SystemDrive")) + "\\Program Files\\Bluefish444\\Driver\\" + module_str).c_str());
49         if(!module)
50                 LoadLibrary(u16(std::string(getenv("SystemDrive")) + "\\Program Files (x86)\\BlueFish444\\Driver\\" + module_str).c_str());
51         if(!module)
52                 CASPAR_THROW_EXCEPTION(file_not_found() << msg_info("Could not find BlueVelvet3.dll. Required drivers are not installed."));
53         static std::shared_ptr<void> lib(module, FreeLibrary);
54         BlueVelvetFactory4 = reinterpret_cast<decltype(BlueVelvetFactory4)>(GetProcAddress(module, "BlueVelvetFactory4"));
55         BlueVelvetDestroy  = reinterpret_cast<decltype(BlueVelvetDestroy)>(GetProcAddress(module, "BlueVelvetDestroy"));
56         BlueVelvetVersion  = reinterpret_cast<decltype(BlueVelvetVersion)>(GetProcAddress(module, "BlueVelvetVersion"));
57 }
58
59 void blue_hanc_initialize()
60 {
61 #ifdef _DEBUG
62         std::string module_str = "BlueHancUtils64_d.dll";
63 #else
64         std::string module_str = "BlueHancUtils64.dll";
65 #endif
66         
67         auto module = LoadLibrary(u16(module_str).c_str());
68         if(!module)
69                 LoadLibrary(u16(std::string(getenv("SystemDrive")) + "\\Program Files\\Bluefish444\\Driver\\" + module_str).c_str());
70         if(!module)
71                 LoadLibrary(u16(std::string(getenv("SystemDrive")) + "\\Program Files (x86)\\BlueFish444\\Driver\\" + module_str).c_str());
72         if(!module)
73                 CASPAR_THROW_EXCEPTION(file_not_found() << msg_info("Could not find BlueHancUtils.dll. Required drivers are not installed."));
74         static std::shared_ptr<void> lib(module, FreeLibrary);
75         encode_hanc_frame        = reinterpret_cast<decltype(encode_hanc_frame)>(GetProcAddress(module, "encode_hanc_frame"));
76         encode_hanc_frame_ex = reinterpret_cast<decltype(encode_hanc_frame_ex)>(GetProcAddress(module, "encode_hanc_frame_ex"));
77 }
78
79 void blue_initialize()
80 {
81         blue_hanc_initialize();
82         blue_velvet_initialize();
83 }
84
85 EVideoMode vid_fmt_from_video_format(const core::video_format& fmt) 
86 {
87         switch(fmt.value())
88         {
89         case core::video_format::pal:                   return VID_FMT_PAL;
90         case core::video_format::ntsc:                  return VID_FMT_NTSC;
91         case core::video_format::x576p2500:             return VID_FMT_INVALID; //not supported
92         case core::video_format::x720p2500:             return VID_FMT_720P_2500;
93         case core::video_format::x720p5000:             return VID_FMT_720P_5000;
94         case core::video_format::x720p5994:             return VID_FMT_720P_5994;
95         case core::video_format::x720p6000:             return VID_FMT_720P_6000;
96         case core::video_format::x1080p2397:    return VID_FMT_1080P_2397;
97         case core::video_format::x1080p2400:    return VID_FMT_1080P_2400;
98         case core::video_format::x1080i5000:    return VID_FMT_1080I_5000;
99         case core::video_format::x1080i5994:    return VID_FMT_1080I_5994;
100         case core::video_format::x1080i6000:    return VID_FMT_1080I_6000;
101         case core::video_format::x1080p2500:    return VID_FMT_1080P_2500;
102         case core::video_format::x1080p2997:    return VID_FMT_1080P_2997;
103         case core::video_format::x1080p3000:    return VID_FMT_1080P_3000;
104         case core::video_format::x1080p5000:    return VID_FMT_1080P_5000;
105         default:                                                                return VID_FMT_INVALID;
106         }
107 }
108
109 bool is_epoch_card(CBlueVelvet4& blue)
110 {
111         switch(blue.has_video_cardtype())
112         {
113         case CRD_BLUE_EPOCH_HORIZON:
114         case CRD_BLUE_EPOCH_CORE:
115         case CRD_BLUE_EPOCH_ULTRA:
116         case CRD_BLUE_EPOCH_2K_HORIZON:
117         case CRD_BLUE_EPOCH_2K_CORE:
118         case CRD_BLUE_EPOCH_2K_ULTRA:
119         case CRD_BLUE_CREATE_HD:
120         case CRD_BLUE_CREATE_2K:
121         case CRD_BLUE_CREATE_2K_ULTRA:
122         case CRD_BLUE_SUPER_NOVA:
123                 return true;
124         default:
125                 return false;
126         }
127 }
128
129 std::wstring get_card_desc(CBlueVelvet4& blue)
130 {
131         switch(blue.has_video_cardtype()) 
132         {
133         case CRD_BLUEDEEP_LT:                           return L"Deepblue LT";// D64 Lite
134         case CRD_BLUEDEEP_SD:                           return L"Iridium SD";// Iridium SD
135         case CRD_BLUEDEEP_AV:                           return L"Iridium AV";// Iridium AV
136         case CRD_BLUEDEEP_IO:                           return L"Deepblue IO";// D64 Full
137         case CRD_BLUEWILD_AV:                           return L"Wildblue AV";// D64 AV
138         case CRD_IRIDIUM_HD:                            return L"Iridium HD";// * Iridium HD
139         case CRD_BLUEWILD_RT:                           return L"Wildblue RT";// D64 RT
140         case CRD_BLUEWILD_HD:                           return L"Wildblue HD";// * BadAss G2
141         case CRD_REDDEVIL:                                      return L"Iridium Full";// Iridium Full
142         case CRD_BLUEDEEP_HD:   
143         case CRD_BLUEDEEP_HDS:                          return L"Reserved for \"BasAss G2";// * BadAss G2 variant, proposed, reserved
144         case CRD_BLUE_ENVY:                                     return L"Blue Envy"; // Mini Din 
145         case CRD_BLUE_PRIDE:                            return L"Blue Pride";//Mini Din Output 
146         case CRD_BLUE_GREED:                            return L"Blue Greed";
147         case CRD_BLUE_INGEST:                           return L"Blue Ingest";
148         case CRD_BLUE_SD_DUALLINK:                      return L"Blue SD Duallink";
149         case CRD_BLUE_CATALYST:                         return L"Blue Catalyst";
150         case CRD_BLUE_SD_DUALLINK_PRO:          return L"Blue SD Duallink Pro";
151         case CRD_BLUE_SD_INGEST_PRO:            return L"Blue SD Ingest pro";
152         case CRD_BLUE_SD_DEEPBLUE_LITE_PRO:     return L"Blue SD Deepblue lite Pro";
153         case CRD_BLUE_SD_SINGLELINK_PRO:        return L"Blue SD Singlelink Pro";
154         case CRD_BLUE_SD_IRIDIUM_AV_PRO:        return L"Blue SD Iridium AV Pro";
155         case CRD_BLUE_SD_FIDELITY:                      return L"Blue SD Fidelity";
156         case CRD_BLUE_SD_FOCUS:                         return L"Blue SD Focus";
157         case CRD_BLUE_SD_PRIME:                         return L"Blue SD Prime";
158         case CRD_BLUE_EPOCH_2K_CORE:            return L"Blue Epoch 2K Core";
159         case CRD_BLUE_EPOCH_2K_ULTRA:           return L"Blue Epoch 2K Ultra";
160         case CRD_BLUE_EPOCH_HORIZON:            return L"Blue Epoch Horizon";
161         case CRD_BLUE_EPOCH_CORE:                       return L"Blue Epoch Core";
162         case CRD_BLUE_EPOCH_ULTRA:                      return L"Blue Epoch Ultra";
163         case CRD_BLUE_CREATE_HD:                        return L"Blue Create HD";
164         case CRD_BLUE_CREATE_2K:                        return L"Blue Create 2K";
165         case CRD_BLUE_CREATE_2K_ULTRA:          return L"Blue Create 2K Ultra";
166         default:                                                        return L"Unknown";
167         }
168 }
169
170 EVideoMode get_video_mode(CBlueVelvet4& blue, const core::video_format_desc& format_desc)
171 {
172         EVideoMode vid_fmt = VID_FMT_INVALID;
173         auto desiredVideoFormat = vid_fmt_from_video_format(format_desc.format);
174         int videoModeCount = blue.count_video_mode();
175         for(int videoModeIndex = 1; videoModeIndex <= videoModeCount; ++videoModeIndex) 
176         {
177                 EVideoMode videoMode = blue.enum_video_mode(videoModeIndex);
178                 if(videoMode == desiredVideoFormat) 
179                         vid_fmt = videoMode;                    
180         }
181         if(vid_fmt == VID_FMT_INVALID)
182                 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info("video-mode not supported.") << arg_value_info(format_desc.name));
183
184         return vid_fmt;
185 }
186
187 spl::shared_ptr<CBlueVelvet4> create_blue()
188 {
189         if(!BlueVelvetFactory4 || !encode_hanc_frame || !encode_hanc_frame)
190                 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info("Bluefish drivers not found."));
191
192         return spl::shared_ptr<CBlueVelvet4>(BlueVelvetFactory4(), BlueVelvetDestroy);
193 }
194
195 spl::shared_ptr<CBlueVelvet4> create_blue(int device_index)
196 {
197         auto blue = create_blue();
198         
199         if(BLUE_FAIL(blue->device_attach(device_index, FALSE))) 
200                 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info("Failed to attach device."));
201
202         return blue;
203 }
204
205 }}