1 /*
2 * Blackmagic DeckLink output
3 * Copyright (c) 2013-2014 Ramiro Polla, Luca Barbato, Deti Fliegl
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include <DeckLinkAPI.h>
23 #ifdef _WIN32
24 #include <DeckLinkAPI_i.c>
25 #else
26 #include <DeckLinkAPIDispatch.cpp>
27 #endif
28
29 #include <pthread.h>
31
32 extern "C" {
36 }
37
39
40 #ifdef _WIN32
41 IDeckLinkIterator *CreateDeckLinkIteratorInstance(void)
42 {
43 IDeckLinkIterator *iter;
44
45 if (CoInitialize(
NULL) < 0) {
48 }
49
50 if (CoCreateInstance(CLSID_CDeckLinkIterator,
NULL, CLSCTX_ALL,
51 IID_IDeckLinkIterator, (
void**) &iter) !=
S_OK) {
54 }
55
56 return iter;
57 }
58 #endif
59
60 #ifdef _WIN32
62 {
64 int l = WideCharToMultiByte(CP_UTF8, 0, w, -1, 0, 0, 0, 0);
66 if (s)
67 WideCharToMultiByte(CP_UTF8, 0, w, -1, s, l, 0, 0);
69 }
70 #define DECKLINK_STR OLECHAR *
71 #define DECKLINK_STRDUP dup_wchar_to_utf8
72 #define DECKLINK_FREE(s) SysFreeString(s)
73 #define DECKLINK_BOOL BOOL
74 #elif defined(__APPLE__)
75 static char *dup_cfstring_to_utf8(CFStringRef w)
76 {
77 char s[256];
78 CFStringGetCString(w, s, 255, kCFStringEncodingUTF8);
80 }
81 #define DECKLINK_STR const __CFString *
82 #define DECKLINK_STRDUP dup_cfstring_to_utf8
83 #define DECKLINK_FREE(s) free((void *) s)
84 #define DECKLINK_BOOL bool
85 #else
86 #define DECKLINK_STR const char *
87 #define DECKLINK_STRDUP av_strdup
88 /* free() is needed for a string returned by the DeckLink SDL. */
89 #define DECKLINK_FREE(s) free((void *) s)
90 #define DECKLINK_BOOL bool
91 #endif
92
94 {
96 HRESULT hr = This->GetDisplayName(&tmpDisplayName);
98 return hr;
101 return hr;
102 }
103
105 {
108 BMDDeckLinkAttributeID attr_id = (cfg_id == bmdDeckLinkConfigAudioInputConnection) ? BMDDeckLinkAudioInputConnections : BMDDeckLinkVideoInputConnections;
109 int64_t bmd_input = (cfg_id == bmdDeckLinkConfigAudioInputConnection) ? (int64_t)
ctx->audio_input : (int64_t)
ctx->video_input;
110 const char *type_name = (cfg_id == bmdDeckLinkConfigAudioInputConnection) ? "audio" : "video";
111 int64_t supported_connections = 0;
113
114 if (bmd_input) {
115 res =
ctx->attr->GetInt(attr_id, &supported_connections);
119 }
120 if ((supported_connections & bmd_input) != bmd_input) {
121 av_log(avctx,
AV_LOG_ERROR,
"Device does not support selected %s input.\n", type_name);
123 }
124 res =
ctx->cfg->SetInt(cfg_id, bmd_input);
128 }
129 }
130 return 0;
131 }
132
135 int tb_num, int tb_den,
137 {
140 BMDDisplayModeSupport support;
141 IDeckLinkDisplayModeIterator *itermode;
142 IDeckLinkDisplayMode *
mode;
143 int i = 1;
145
146 av_log(avctx,
AV_LOG_DEBUG,
"Trying to find mode for frame size %dx%d, frame timing %d/%d, direction %d, mode number %d\n",
147 width, height, tb_num, tb_den, direction, num);
148
149 if (
ctx->duplex_mode) {
151
152 if (
ctx->attr->GetFlag(BMDDeckLinkSupportsDuplexModeConfiguration, &duplex_supported) !=
S_OK)
153 duplex_supported = false;
154
155 if (duplex_supported) {
156 res =
ctx->cfg->SetInt(bmdDeckLinkConfigDuplexMode,
ctx->duplex_mode == 2 ? bmdDuplexModeFull : bmdDuplexModeHalf);
159 else
160 av_log(avctx,
AV_LOG_VERBOSE,
"Successfully set duplex mode to %s duplex.\n",
ctx->duplex_mode == 2 ?
"full" :
"half");
161 } else {
163 }
164 }
165
167 int ret;
169 if (ret < 0)
170 return ret;
172 if (ret < 0)
173 return ret;
174 res =
ctx->dli->GetDisplayModeIterator (&itermode);
175 } else {
176 res =
ctx->dlo->GetDisplayModeIterator (&itermode);
177 }
178
182 }
183
185 ctx->bmd_mode = bmdModeUnknown;
186 while ((
ctx->bmd_mode == bmdModeUnknown) && itermode->Next(&mode) ==
S_OK) {
187 BMDTimeValue bmd_tb_num, bmd_tb_den;
188 int bmd_width = mode->GetWidth();
189 int bmd_height = mode->GetHeight();
190
191 mode->GetFrameRate(&bmd_tb_num, &bmd_tb_den);
193
194 if ((bmd_width == width && bmd_height == height &&
195 !
av_cmp_q(mode_tb, target_tb)) || i == num) {
196 ctx->bmd_mode = mode->GetDisplayMode();
197 ctx->bmd_width = bmd_width;
198 ctx->bmd_height = bmd_height;
199 ctx->bmd_tb_den = bmd_tb_den;
200 ctx->bmd_tb_num = bmd_tb_num;
201 ctx->bmd_field_dominance = mode->GetFieldDominance();
203 bmd_width, bmd_height, 1/
av_q2d(mode_tb),
204 (
ctx->bmd_field_dominance==bmdLowerFieldFirst ||
ctx->bmd_field_dominance==bmdUpperFieldFirst)?
"(i)":
"");
205 }
206
207 mode->Release();
208 i++;
209 }
210
211 itermode->Release();
212
213 if (
ctx->bmd_mode == bmdModeUnknown)
214 return -1;
216 if (
ctx->dli->DoesSupportVideoMode(
ctx->bmd_mode, bmdFormat8BitYUV,
217 bmdVideoOutputFlagDefault,
219 return -1;
220 } else {
221 if (
ctx->dlo->DoesSupportVideoMode(
ctx->bmd_mode, bmdFormat8BitYUV,
222 bmdVideoOutputFlagDefault,
224 return -1;
225 }
226 if (support == bmdDisplayModeSupported)
227 return 0;
228
229 return -1;
230 }
231
234 }
235
237 {
238 IDeckLink *dl =
NULL;
239 IDeckLinkIterator *iter = CreateDeckLinkIteratorInstance();
240 if (!iter) {
243 }
245 while (iter->Next(&dl) ==
S_OK) {
246 const char *displayName;
250 dl->Release();
251 }
252 iter->Release();
253 return 0;
254 }
255
257 {
260 IDeckLinkDisplayModeIterator *itermode;
261 IDeckLinkDisplayMode *
mode;
262 int i=0;
264
266 int ret;
268 if (ret < 0)
269 return ret;
271 if (ret < 0)
272 return ret;
273 res =
ctx->dli->GetDisplayModeIterator (&itermode);
274 } else {
275 res =
ctx->dlo->GetDisplayModeIterator (&itermode);
276 }
277
281 }
282
285 while (itermode->Next(&mode) ==
S_OK) {
286 BMDTimeValue tb_num, tb_den;
287 mode->GetFrameRate(&tb_num, &tb_den);
289 ++i,mode->GetWidth(), mode->GetHeight(),
290 (int) tb_den, (int) tb_num);
291 switch (mode->GetFieldDominance()) {
292 case bmdLowerFieldFirst:
294 case bmdUpperFieldFirst:
296 }
298 mode->Release();
299 }
300
301 itermode->Release();
302
303 return 0;
304 }
305
307 {
310
316 ctx->attr->Release();
321 }
322
324 {
327 IDeckLink *dl =
NULL;
328 IDeckLinkIterator *iter = CreateDeckLinkIteratorInstance();
329 if (!iter) {
332 }
333
334 while (iter->Next(&dl) ==
S_OK) {
335 const char *displayName;
337 if (!strcmp(name, displayName)) {
340 break;
341 }
343 dl->Release();
344 }
345 iter->Release();
348
349 if (
ctx->dl->QueryInterface(IID_IDeckLinkConfiguration, (
void **)&
ctx->cfg) !=
S_OK) {
350 av_log(avctx,
AV_LOG_ERROR,
"Could not get configuration interface for '%s'\n", name);
353 }
354
355 if (
ctx->dl->QueryInterface(IID_IDeckLinkAttributes, (
void **)&
ctx->attr) !=
S_OK) {
359 }
360
361 return 0;
362 }
#define AV_LOG_WARNING
Something somehow does not look correct.
int ff_decklink_init_device(AVFormatContext *avctx, const char *name)
static double av_q2d(AVRational a)
Convert an AVRational to a double.
#define AV_LOG_VERBOSE
Detailed information.
int ff_decklink_set_format(AVFormatContext *avctx, int width, int height, int tb_num, int tb_den, decklink_direction_t direction, int num)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
static char * dup_wchar_to_utf8(wchar_t *w)
char filename[1024]
input or output filename
int ff_decklink_list_formats(AVFormatContext *avctx, decklink_direction_t direction)
#define AV_LOG_INFO
Standard information.
char * av_strdup(const char *s)
Duplicate a string.
int ff_decklink_list_devices(AVFormatContext *avctx)
static AVRational av_make_q(int num, int den)
Create an AVRational.
static int decklink_select_input(AVFormatContext *avctx, BMDDeckLinkConfigurationID cfg_id)
HRESULT ff_decklink_get_display_name(IDeckLink *This, const char **displayName)
Rational number (pair of numerator and denominator).
void ff_decklink_cleanup(AVFormatContext *avctx)
static int av_cmp_q(AVRational a, AVRational b)
Compare two rationals.
void * priv_data
Format private data.
#define AVERROR_EXTERNAL
Generic error in an external library.