]> git.sesse.net Git - casparcg/blob - modules/decklink/util/util.h
Only memcpy on Linux if the decklink card will attempt DMA directly from BGRA buffer...
[casparcg] / modules / decklink / util / util.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 <common/except.h>
25 #include <common/log.h>
26 #include <core/video_format.h>
27
28 #include "../decklink_api.h"
29
30 #include <boost/lexical_cast.hpp>
31
32 #include <string>
33
34 namespace caspar { namespace decklink {
35         
36 static BMDDisplayMode get_decklink_video_format(core::video_format fmt) 
37 {
38         switch(fmt)
39         {
40         case core::video_format::pal:                   return bmdModePAL;
41         case core::video_format::ntsc:                  return bmdModeNTSC;
42         case core::video_format::x576p2500:             return (BMDDisplayMode)ULONG_MAX;
43         case core::video_format::x720p2398:             return (BMDDisplayMode)ULONG_MAX;
44         case core::video_format::x720p2400:             return (BMDDisplayMode)ULONG_MAX;
45         case core::video_format::x720p2500:             return (BMDDisplayMode)ULONG_MAX;
46         case core::video_format::x720p5000:             return bmdModeHD720p50;
47         case core::video_format::x720p2997:             return (BMDDisplayMode)ULONG_MAX;
48         case core::video_format::x720p5994:             return bmdModeHD720p5994;
49         case core::video_format::x720p3000:             return (BMDDisplayMode)ULONG_MAX;
50         case core::video_format::x720p6000:             return bmdModeHD720p60;
51         case core::video_format::x1080p2398:    return bmdModeHD1080p2398;
52         case core::video_format::x1080p2400:    return bmdModeHD1080p24;
53         case core::video_format::x1080i5000:    return bmdModeHD1080i50;
54         case core::video_format::x1080i5994:    return bmdModeHD1080i5994;
55         case core::video_format::x1080i6000:    return bmdModeHD1080i6000;
56         case core::video_format::x1080p2500:    return bmdModeHD1080p25;
57         case core::video_format::x1080p2997:    return bmdModeHD1080p2997;
58         case core::video_format::x1080p3000:    return bmdModeHD1080p30;
59         case core::video_format::x1080p5000:    return bmdModeHD1080p50;
60         case core::video_format::x1080p5994:    return bmdModeHD1080p5994;
61         case core::video_format::x1080p6000:    return bmdModeHD1080p6000;
62         case core::video_format::x1556p2398:    return bmdMode2k2398;
63         case core::video_format::x1556p2400:    return bmdMode2k24;
64         case core::video_format::x1556p2500:    return bmdMode2k25;
65         case core::video_format::dci1080p2398:  return bmdMode2kDCI2398;
66         case core::video_format::dci1080p2400:  return bmdMode2kDCI24;
67         case core::video_format::dci1080p2500:  return bmdMode2kDCI25;
68         case core::video_format::x2160p2398:    return bmdMode4K2160p2398;
69         case core::video_format::x2160p2400:    return bmdMode4K2160p24;
70         case core::video_format::x2160p2500:    return bmdMode4K2160p25;
71         case core::video_format::x2160p2997:    return bmdMode4K2160p2997;
72         case core::video_format::x2160p3000:    return bmdMode4K2160p30;
73         case core::video_format::dci2160p2398:  return bmdMode4kDCI2398;
74         case core::video_format::dci2160p2400:  return bmdMode4kDCI24;
75         case core::video_format::dci2160p2500:  return bmdMode4kDCI25;
76         default:                                                                return (BMDDisplayMode)ULONG_MAX;
77         }
78 }
79
80 static core::video_format get_caspar_video_format(BMDDisplayMode fmt) 
81 {
82         switch(fmt)
83         {
84         case bmdModePAL:                                                return core::video_format::pal;
85         case bmdModeNTSC:                                               return core::video_format::ntsc;
86         case bmdModeHD720p50:                                   return core::video_format::x720p5000;
87         case bmdModeHD720p5994:                                 return core::video_format::x720p5994;
88         case bmdModeHD720p60:                                   return core::video_format::x720p6000;
89         case bmdModeHD1080p2398:                                return core::video_format::x1080p2398;
90         case bmdModeHD1080p24:                                  return core::video_format::x1080p2400;
91         case bmdModeHD1080i50:                                  return core::video_format::x1080i5000;
92         case bmdModeHD1080i5994:                                return core::video_format::x1080i5994;
93         case bmdModeHD1080i6000:                                return core::video_format::x1080i6000;
94         case bmdModeHD1080p25:                                  return core::video_format::x1080p2500;
95         case bmdModeHD1080p2997:                                return core::video_format::x1080p2997;
96         case bmdModeHD1080p30:                                  return core::video_format::x1080p3000;
97         case bmdModeHD1080p50:                                  return core::video_format::x1080p5000;
98         case bmdModeHD1080p5994:                                return core::video_format::x1080p5994;
99         case bmdModeHD1080p6000:                                return core::video_format::x1080p6000;
100         case bmdMode2k2398:                                             return core::video_format::x1556p2398;
101         case bmdMode2k24:                                               return core::video_format::x1556p2400;
102         case bmdMode2k25:                                               return core::video_format::x1556p2500;
103         case bmdMode2kDCI2398:                                  return core::video_format::dci1080p2398;
104         case bmdMode2kDCI24:                                    return core::video_format::dci1080p2400;
105         case bmdMode2kDCI25:                                    return core::video_format::dci1080p2500;
106         case bmdMode4K2160p2398:                                return core::video_format::x2160p2398;
107         case bmdMode4K2160p24:                                  return core::video_format::x2160p2400;
108         case bmdMode4K2160p25:                                  return core::video_format::x2160p2500;
109         case bmdMode4K2160p2997:                                return core::video_format::x2160p2997;
110         case bmdMode4K2160p30:                                  return core::video_format::x2160p3000;
111         case bmdMode4kDCI2398:                                  return core::video_format::dci2160p2398;
112         case bmdMode4kDCI24:                                    return core::video_format::dci2160p2400;
113         case bmdMode4kDCI25:                                    return core::video_format::dci2160p2500;
114         default:                                                                return core::video_format::invalid;
115         }
116 }
117
118 static std::wstring get_mode_name(const com_ptr<IDeckLinkDisplayMode>& mode)
119 {
120         String mode_name;
121         mode->GetName(&mode_name);
122         return u16(mode_name);
123 }
124
125 template<typename T, typename F>
126 BMDDisplayMode get_display_mode(const T& device, BMDDisplayMode format, BMDPixelFormat pix_fmt, F flag, bool& will_attempt_dma)
127 {
128     IDeckLinkDisplayMode* m = nullptr;
129     IDeckLinkDisplayModeIterator* iter;
130     if(SUCCEEDED(device->GetDisplayModeIterator(&iter)))
131         {
132         auto iterator = wrap_raw<com_ptr>(iter, true);
133         while(SUCCEEDED(iterator->Next(&m)) &&
134                 m != nullptr &&
135                 m->GetDisplayMode() != format)
136         {
137             m->Release();
138         }
139         }
140
141     if(!m)
142                 CASPAR_THROW_EXCEPTION(user_error() << msg_info("Device could not find requested video-format: " + boost::lexical_cast<std::string>(format)));
143
144         com_ptr<IDeckLinkDisplayMode> mode = wrap_raw<com_ptr>(m, true);
145
146         BMDDisplayModeSupport displayModeSupport;
147         will_attempt_dma = false;
148
149         if (FAILED(device->DoesSupportVideoMode(mode->GetDisplayMode(), pix_fmt, flag, &displayModeSupport, nullptr)))
150                 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(L"Could not determine whether device supports requested video format: " + get_mode_name(mode)));
151         else if (displayModeSupport == bmdDisplayModeNotSupported)
152                 CASPAR_LOG(warning) << L"Device does not support video-format: " << get_mode_name(mode);
153         else if (displayModeSupport == bmdDisplayModeSupportedWithConversion)
154                 CASPAR_LOG(warning) << L"Device supports video-format with conversion: " << get_mode_name(mode);
155         else
156                 will_attempt_dma = true;
157
158         return mode->GetDisplayMode();
159 }
160
161 template<typename T, typename F>
162 static BMDDisplayMode get_display_mode(const T& device, core::video_format fmt, BMDPixelFormat pix_fmt, F flag, bool& will_attempt_dma)
163 {       
164         return get_display_mode(device, get_decklink_video_format(fmt), pix_fmt, flag, will_attempt_dma);
165 }
166
167 template<typename T>
168 static std::wstring version(T iterator)
169 {
170     auto info = iface_cast<IDeckLinkAPIInformation>(iterator);
171         if (!info)
172                 return L"Unknown";
173         
174     String ver;
175     info->GetString(BMDDeckLinkAPIVersion, &ver);
176                 
177     return u16(ver);
178 }
179
180 static com_ptr<IDeckLink> get_device(size_t device_index)
181 {
182     auto pDecklinkIterator = create_iterator();
183                 
184         size_t n = 0;
185     com_ptr<IDeckLink> decklink;
186     IDeckLink* current = nullptr;
187     while(n < device_index && pDecklinkIterator->Next(&current) == S_OK)
188     {
189         ++n;
190         decklink = wrap_raw<com_ptr>(current);
191         current->Release();
192     }
193
194         if(n != device_index || !decklink)
195                 CASPAR_THROW_EXCEPTION(user_error() << msg_info("Decklink device " + boost::lexical_cast<std::string>(device_index) + " not found."));
196                 
197         return decklink;
198 }
199
200 template <typename T>
201 static std::wstring get_model_name(const T& device)
202 {       
203     String pModelName;
204         device->GetModelName(&pModelName);
205     return u16(pModelName);
206 }
207
208 class reference_signal_detector
209 {
210         com_iface_ptr<IDeckLinkOutput>  output_;
211         BMDReferenceStatus                              last_reference_status_  = static_cast<BMDReferenceStatus>(-1);
212 public:
213         reference_signal_detector(const com_iface_ptr<IDeckLinkOutput>& output)
214                 : output_(output)
215         {
216         }
217
218         template<typename Print>
219         void detect_change(const Print& print)
220         {
221                 BMDReferenceStatus reference_status;
222
223                 if (output_->GetReferenceStatus(&reference_status) != S_OK)
224                 {
225                         CASPAR_LOG(error) << print() << L" Reference signal: failed while querying status";
226                 }
227                 else if (reference_status != last_reference_status_)
228                 {
229                         last_reference_status_ = reference_status;
230
231                         if (reference_status == 0)
232                                 CASPAR_LOG(info) << print() << L" Reference signal: not detected.";
233                         else if (reference_status & bmdReferenceNotSupportedByHardware)
234                                 CASPAR_LOG(info) << print() << L" Reference signal: not supported by hardware.";
235                         else if (reference_status & bmdReferenceLocked)
236                                 CASPAR_LOG(info) << print() << L" Reference signal: locked.";
237                         else
238                                 CASPAR_LOG(info) << print() << L" Reference signal: Unhandled enum bitfield: " << reference_status;
239                 }
240         }
241 };
242
243 }}