]> git.sesse.net Git - casparcg/blob - modules/decklink/util/util.h
9e3b5b27ab8a3ac24444e405d32cd5858aa3150d
[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 "../interop/DeckLinkAPI_h.h"
29
30 #include <boost/lexical_cast.hpp>
31
32 #include <atlbase.h>
33
34 #include <string>
35
36 namespace caspar { namespace decklink {
37         
38 static BMDDisplayMode get_decklink_video_format(core::video_format fmt) 
39 {
40         switch(fmt.value())
41         {
42         case core::video_format::pal:                   return bmdModePAL;
43         case core::video_format::ntsc:                  return bmdModeNTSC;
44         case core::video_format::x576p2500:             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::x720p5994:             return bmdModeHD720p5994;
48         case core::video_format::x720p6000:             return bmdModeHD720p60;
49         case core::video_format::x1080p2397:    return bmdModeHD1080p2398;
50         case core::video_format::x1080p2400:    return bmdModeHD1080p24;
51         case core::video_format::x1080i5000:    return bmdModeHD1080i50;
52         case core::video_format::x1080i5994:    return bmdModeHD1080i5994;
53         case core::video_format::x1080i6000:    return bmdModeHD1080i6000;
54         case core::video_format::x1080p2500:    return bmdModeHD1080p25;
55         case core::video_format::x1080p2997:    return bmdModeHD1080p2997;
56         case core::video_format::x1080p3000:    return bmdModeHD1080p30;
57         case core::video_format::x1080p5000:    return bmdModeHD1080p50;
58         default:                                                                return (BMDDisplayMode)ULONG_MAX;
59         }
60 }
61
62 static core::video_format get_caspar_video_format(BMDDisplayMode fmt) 
63 {
64         switch(fmt)
65         {
66         case bmdModePAL:                                                return core::video_format::pal;         
67         case bmdModeNTSC:                                               return core::video_format::ntsc;                
68         case bmdModeHD720p50:                                   return core::video_format::x720p5000;   
69         case bmdModeHD720p5994:                                 return core::video_format::x720p5994;   
70         case bmdModeHD720p60:                                   return core::video_format::x720p6000;   
71         case bmdModeHD1080p2398:                                return core::video_format::x1080p2397;  
72         case bmdModeHD1080p24:                                  return core::video_format::x1080p2400;  
73         case bmdModeHD1080i50:                                  return core::video_format::x1080i5000;  
74         case bmdModeHD1080i5994:                                return core::video_format::x1080i5994;  
75         case bmdModeHD1080i6000:                                return core::video_format::x1080i6000;  
76         case bmdModeHD1080p25:                                  return core::video_format::x1080p2500;  
77         case bmdModeHD1080p2997:                                return core::video_format::x1080p2997;  
78         case bmdModeHD1080p30:                                  return core::video_format::x1080p3000;  
79         case bmdModeHD1080p50:                                  return core::video_format::x1080p5000;  
80         default:                                                                return core::video_format::invalid;     
81         }
82 }
83
84 template<typename T, typename F>
85 BMDDisplayMode get_display_mode(const T& device, BMDDisplayMode format, BMDPixelFormat pix_fmt, F flag)
86 {
87         CComPtr<IDeckLinkDisplayModeIterator> iterator;
88         CComPtr<IDeckLinkDisplayMode>             mode;
89         
90         if(SUCCEEDED(device->GetDisplayModeIterator(&iterator)))
91         {
92                 while(SUCCEEDED(iterator->Next(&mode)) && 
93                                 mode != nullptr && 
94                                 mode->GetDisplayMode() != format){}
95         }
96
97         if(!mode)
98                 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info("Device could not find requested video-format.") 
99                                                                                                  << arg_value_info(boost::lexical_cast<std::string>(format))
100                                                                                                  << arg_name_info("format"));
101                 
102         BMDDisplayModeSupport displayModeSupport;
103         if(FAILED(device->DoesSupportVideoMode(mode->GetDisplayMode(), pix_fmt, flag, &displayModeSupport, nullptr)) || displayModeSupport == bmdDisplayModeNotSupported)
104                 CASPAR_LOG(warning) << L"Device does not support video-format: " << mode->GetDisplayMode();
105                 //CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info("Device does not support requested video-format.")
106                 //                                                                               << arg_value_info(boost::lexical_cast<std::string>(format))
107                 //                                                                               << arg_name_info("format"));
108         else if(displayModeSupport == bmdDisplayModeSupportedWithConversion)
109                 CASPAR_LOG(warning) << L"Device supports video-format with conversion: " << mode->GetDisplayMode();
110
111         return mode->GetDisplayMode();
112 }
113
114 template<typename T, typename F>
115 static BMDDisplayMode get_display_mode(const T& device, core::video_format fmt, BMDPixelFormat pix_fmt, F flag)
116 {       
117         return get_display_mode(device, get_decklink_video_format(fmt), pix_fmt, flag);
118 }
119
120 template<typename T>
121 static std::wstring version(T& iterator)
122 {
123         CComQIPtr<IDeckLinkAPIInformation> info = iterator;
124         if (!info)
125                 return L"Unknown";
126         
127         BSTR ver;               
128         info->GetString(BMDDeckLinkAPIVersion, &ver);
129                 
130         return ver;                                     
131 }
132
133 static CComPtr<IDeckLink> get_device(size_t device_index)
134 {
135         CComPtr<IDeckLinkIterator> pDecklinkIterator;
136         if(FAILED(pDecklinkIterator.CoCreateInstance(CLSID_CDeckLinkIterator)))
137                 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info("Decklink drivers not found."));
138                 
139         size_t n = 0;
140         CComPtr<IDeckLink> decklink;
141         while(n < device_index && pDecklinkIterator->Next(&decklink) == S_OK){++n;}     
142
143         if(n != device_index || !decklink)
144                 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info("Decklink device not found.") << arg_name_info("device_index") << arg_value_info(boost::lexical_cast<std::string>(device_index)));
145                 
146         return decklink;
147 }
148
149 template <typename T>
150 static std::wstring get_model_name(const T& device)
151 {       
152         BSTR pModelName;
153         device->GetModelName(&pModelName);
154         return std::wstring(pModelName);
155 }
156
157 }}