2 * Blackmagic DeckLink output
3 * Copyright (c) 2013-2014 Ramiro Polla, Luca Barbato, Deti Fliegl
5 * This file is part of FFmpeg.
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.
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.
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
22 #include <DeckLinkAPI.h>
24 #include <DeckLinkAPI_i.c>
26 #include <DeckLinkAPIDispatch.cpp>
30 #include "libavformat/avformat.h"
31 #include "libavformat/internal.h"
32 #include "libavutil/imgutils.h"
33 #include "libavutil/intreadwrite.h"
34 #include "libavutil/bswap.h"
37 #include "decklink_common.h"
40 IDeckLinkIterator *CreateDeckLinkIteratorInstance(void)
42 IDeckLinkIterator *iter;
44 if (CoInitialize(NULL) < 0) {
45 av_log(NULL, AV_LOG_ERROR, "COM initialization failed.\n");
49 if (CoCreateInstance(CLSID_CDeckLinkIterator, NULL, CLSCTX_ALL,
50 IID_IDeckLinkIterator, (void**) &iter) != S_OK) {
51 av_log(NULL, AV_LOG_ERROR, "DeckLink drivers not installed.\n");
60 static char *dup_wchar_to_utf8(wchar_t *w)
63 int l = WideCharToMultiByte(CP_UTF8, 0, w, -1, 0, 0, 0, 0);
64 s = (char *) av_malloc(l);
66 WideCharToMultiByte(CP_UTF8, 0, w, -1, s, l, 0, 0);
69 #define DECKLINK_STR OLECHAR *
70 #define DECKLINK_STRDUP dup_wchar_to_utf8
71 #define DECKLINK_FREE(s) SysFreeString(s)
72 #define DECKLINK_BOOL BOOL
73 #elif defined(__APPLE__)
74 static char *dup_cfstring_to_utf8(CFStringRef w)
77 CFStringGetCString(w, s, 255, kCFStringEncodingUTF8);
80 #define DECKLINK_STR const __CFString *
81 #define DECKLINK_STRDUP dup_cfstring_to_utf8
82 #define DECKLINK_FREE(s) free((void *) s)
83 #define DECKLINK_BOOL bool
85 #define DECKLINK_STR const char *
86 #define DECKLINK_STRDUP av_strdup
87 /* free() is needed for a string returned by the DeckLink SDL. */
88 #define DECKLINK_FREE(s) free((void *) s)
89 #define DECKLINK_BOOL bool
92 HRESULT ff_decklink_get_display_name(IDeckLink *This, const char **displayName)
94 DECKLINK_STR tmpDisplayName;
95 HRESULT hr = This->GetDisplayName(&tmpDisplayName);
98 *displayName = DECKLINK_STRDUP(tmpDisplayName);
99 DECKLINK_FREE(tmpDisplayName);
103 static int decklink_select_input(AVFormatContext *avctx, BMDDeckLinkConfigurationID cfg_id)
105 struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
106 struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
107 BMDDeckLinkAttributeID attr_id = (cfg_id == bmdDeckLinkConfigAudioInputConnection) ? BMDDeckLinkAudioInputConnections : BMDDeckLinkVideoInputConnections;
108 int64_t bmd_input = (cfg_id == bmdDeckLinkConfigAudioInputConnection) ? (int64_t)ctx->audio_input : (int64_t)ctx->video_input;
109 const char *type_name = (cfg_id == bmdDeckLinkConfigAudioInputConnection) ? "audio" : "video";
110 int64_t supported_connections = 0;
114 res = ctx->attr->GetInt(attr_id, &supported_connections);
116 av_log(avctx, AV_LOG_ERROR, "Failed to query supported %s inputs.\n", type_name);
117 return AVERROR_EXTERNAL;
119 if ((supported_connections & bmd_input) != bmd_input) {
120 av_log(avctx, AV_LOG_ERROR, "Device does not support selected %s input.\n", type_name);
121 return AVERROR(ENOSYS);
123 res = ctx->cfg->SetInt(cfg_id, bmd_input);
125 av_log(avctx, AV_LOG_ERROR, "Failed to select %s input.\n", type_name);
126 return AVERROR_EXTERNAL;
132 static DECKLINK_BOOL field_order_eq(enum AVFieldOrder field_order, BMDFieldDominance bmd_field_order)
134 if (field_order == AV_FIELD_UNKNOWN)
136 if ((field_order == AV_FIELD_TT || field_order == AV_FIELD_TB) && bmd_field_order == bmdUpperFieldFirst)
138 if ((field_order == AV_FIELD_BB || field_order == AV_FIELD_BT) && bmd_field_order == bmdLowerFieldFirst)
140 if (field_order == AV_FIELD_PROGRESSIVE && (bmd_field_order == bmdProgressiveFrame || bmd_field_order == bmdProgressiveSegmentedFrame))
145 int ff_decklink_set_format(AVFormatContext *avctx,
146 int width, int height,
147 int tb_num, int tb_den,
148 enum AVFieldOrder field_order,
149 decklink_direction_t direction, int num)
151 struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
152 struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
153 BMDDisplayModeSupport support;
154 IDeckLinkDisplayModeIterator *itermode;
155 IDeckLinkDisplayMode *mode;
159 av_log(avctx, AV_LOG_DEBUG, "Trying to find mode for frame size %dx%d, frame timing %d/%d, field order %d, direction %d, mode number %d, format code %s\n",
160 width, height, tb_num, tb_den, field_order, direction, num, (cctx->format_code) ? cctx->format_code : "(unset)");
162 if (ctx->duplex_mode) {
163 DECKLINK_BOOL duplex_supported = false;
165 if (ctx->attr->GetFlag(BMDDeckLinkSupportsDuplexModeConfiguration, &duplex_supported) != S_OK)
166 duplex_supported = false;
168 if (duplex_supported) {
169 res = ctx->cfg->SetInt(bmdDeckLinkConfigDuplexMode, ctx->duplex_mode == 2 ? bmdDuplexModeFull : bmdDuplexModeHalf);
171 av_log(avctx, AV_LOG_WARNING, "Setting duplex mode failed.\n");
173 av_log(avctx, AV_LOG_VERBOSE, "Successfully set duplex mode to %s duplex.\n", ctx->duplex_mode == 2 ? "full" : "half");
175 av_log(avctx, AV_LOG_WARNING, "Unable to set duplex mode, because it is not supported.\n");
179 if (direction == DIRECTION_IN) {
181 ret = decklink_select_input(avctx, bmdDeckLinkConfigAudioInputConnection);
184 ret = decklink_select_input(avctx, bmdDeckLinkConfigVideoInputConnection);
187 res = ctx->dli->GetDisplayModeIterator (&itermode);
189 res = ctx->dlo->GetDisplayModeIterator (&itermode);
193 av_log(avctx, AV_LOG_ERROR, "Could not get Display Mode Iterator\n");
197 char format_buf[] = " ";
198 if (cctx->format_code)
199 memcpy(format_buf, cctx->format_code, FFMIN(strlen(cctx->format_code), sizeof(format_buf)));
200 BMDDisplayMode target_mode = (BMDDisplayMode)AV_RB32(format_buf);
201 AVRational target_tb = av_make_q(tb_num, tb_den);
202 ctx->bmd_mode = bmdModeUnknown;
203 while ((ctx->bmd_mode == bmdModeUnknown) && itermode->Next(&mode) == S_OK) {
204 BMDTimeValue bmd_tb_num, bmd_tb_den;
205 int bmd_width = mode->GetWidth();
206 int bmd_height = mode->GetHeight();
207 BMDDisplayMode bmd_mode = mode->GetDisplayMode();
208 BMDFieldDominance bmd_field_dominance = mode->GetFieldDominance();
210 mode->GetFrameRate(&bmd_tb_num, &bmd_tb_den);
211 AVRational mode_tb = av_make_q(bmd_tb_num, bmd_tb_den);
213 if ((bmd_width == width &&
214 bmd_height == height &&
215 !av_cmp_q(mode_tb, target_tb) &&
216 field_order_eq(field_order, bmd_field_dominance))
218 || target_mode == bmd_mode) {
219 ctx->bmd_mode = bmd_mode;
220 ctx->bmd_width = bmd_width;
221 ctx->bmd_height = bmd_height;
222 ctx->bmd_tb_den = bmd_tb_den;
223 ctx->bmd_tb_num = bmd_tb_num;
224 ctx->bmd_field_dominance = bmd_field_dominance;
225 av_log(avctx, AV_LOG_INFO, "Found Decklink mode %d x %d with rate %.2f%s\n",
226 bmd_width, bmd_height, 1/av_q2d(mode_tb),
227 (ctx->bmd_field_dominance==bmdLowerFieldFirst || ctx->bmd_field_dominance==bmdUpperFieldFirst)?"(i)":"");
236 if (ctx->bmd_mode == bmdModeUnknown)
238 if (direction == DIRECTION_IN) {
239 if (ctx->dli->DoesSupportVideoMode(ctx->bmd_mode, bmdFormat8BitYUV,
240 bmdVideoOutputFlagDefault,
241 &support, NULL) != S_OK)
244 if (ctx->dlo->DoesSupportVideoMode(ctx->bmd_mode, bmdFormat8BitYUV,
245 bmdVideoOutputFlagDefault,
246 &support, NULL) != S_OK)
249 if (support == bmdDisplayModeSupported)
255 int ff_decklink_set_format(AVFormatContext *avctx, decklink_direction_t direction, int num) {
256 return ff_decklink_set_format(avctx, 0, 0, 0, 0, AV_FIELD_UNKNOWN, direction, num);
259 int ff_decklink_list_devices(AVFormatContext *avctx)
261 IDeckLink *dl = NULL;
262 IDeckLinkIterator *iter = CreateDeckLinkIteratorInstance();
264 av_log(avctx, AV_LOG_ERROR, "Could not create DeckLink iterator\n");
267 av_log(avctx, AV_LOG_INFO, "Blackmagic DeckLink devices:\n");
268 while (iter->Next(&dl) == S_OK) {
269 const char *displayName;
270 ff_decklink_get_display_name(dl, &displayName);
271 av_log(avctx, AV_LOG_INFO, "\t'%s'\n", displayName);
272 av_free((void *) displayName);
279 int ff_decklink_list_formats(AVFormatContext *avctx, decklink_direction_t direction)
281 struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
282 struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
283 IDeckLinkDisplayModeIterator *itermode;
284 IDeckLinkDisplayMode *mode;
285 uint32_t format_code;
288 if (direction == DIRECTION_IN) {
290 ret = decklink_select_input(avctx, bmdDeckLinkConfigAudioInputConnection);
293 ret = decklink_select_input(avctx, bmdDeckLinkConfigVideoInputConnection);
296 res = ctx->dli->GetDisplayModeIterator (&itermode);
298 res = ctx->dlo->GetDisplayModeIterator (&itermode);
302 av_log(avctx, AV_LOG_ERROR, "Could not get Display Mode Iterator\n");
306 av_log(avctx, AV_LOG_INFO, "Supported formats for '%s':\n\tformat_code\tdescription",
308 while (itermode->Next(&mode) == S_OK) {
309 BMDTimeValue tb_num, tb_den;
310 mode->GetFrameRate(&tb_num, &tb_den);
311 format_code = av_bswap32(mode->GetDisplayMode());
312 av_log(avctx, AV_LOG_INFO, "\n\t%.4s\t\t%ldx%ld at %d/%d fps",
313 (char*) &format_code, mode->GetWidth(), mode->GetHeight(),
314 (int) tb_den, (int) tb_num);
315 switch (mode->GetFieldDominance()) {
316 case bmdLowerFieldFirst:
317 av_log(avctx, AV_LOG_INFO, " (interlaced, lower field first)"); break;
318 case bmdUpperFieldFirst:
319 av_log(avctx, AV_LOG_INFO, " (interlaced, upper field first)"); break;
323 av_log(avctx, AV_LOG_INFO, "\n");
330 void ff_decklink_cleanup(AVFormatContext *avctx)
332 struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
333 struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
340 ctx->attr->Release();
347 int ff_decklink_init_device(AVFormatContext *avctx, const char* name)
349 struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
350 struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
351 IDeckLink *dl = NULL;
352 IDeckLinkIterator *iter = CreateDeckLinkIteratorInstance();
354 av_log(avctx, AV_LOG_ERROR, "Could not create DeckLink iterator\n");
355 return AVERROR_EXTERNAL;
358 while (iter->Next(&dl) == S_OK) {
359 const char *displayName;
360 ff_decklink_get_display_name(dl, &displayName);
361 if (!strcmp(name, displayName)) {
362 av_free((void *)displayName);
366 av_free((void *)displayName);
371 return AVERROR(ENXIO);
373 if (ctx->dl->QueryInterface(IID_IDeckLinkConfiguration, (void **)&ctx->cfg) != S_OK) {
374 av_log(avctx, AV_LOG_ERROR, "Could not get configuration interface for '%s'\n", name);
375 ff_decklink_cleanup(avctx);
376 return AVERROR_EXTERNAL;
379 if (ctx->dl->QueryInterface(IID_IDeckLinkAttributes, (void **)&ctx->attr) != S_OK) {
380 av_log(avctx, AV_LOG_ERROR, "Could not get attributes interface for '%s'\n", name);
381 ff_decklink_cleanup(avctx);
382 return AVERROR_EXTERNAL;