decklink_common.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  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. /* Include internal.h first to avoid conflict between winsock.h (used by
  22. * DeckLink headers) and winsock2.h (used by libavformat) in MSVC++ builds */
  23. extern "C" {
  24. #include "libavformat/internal.h"
  25. }
  26. #include <DeckLinkAPI.h>
  27. #ifdef _WIN32
  28. #include <DeckLinkAPI_i.c>
  29. #else
  30. /* The file provided by the SDK is known to be missing prototypes, which doesn't
  31. cause issues with GCC since the warning doesn't apply to C++ files. However
  32. Clang does complain (and warnings are treated as errors), so suppress the
  33. warning just for this one file */
  34. #ifdef __clang__
  35. #pragma clang diagnostic push
  36. #pragma clang diagnostic ignored "-Wmissing-prototypes"
  37. #endif
  38. #include <DeckLinkAPIDispatch.cpp>
  39. #ifdef __clang__
  40. #pragma clang diagnostic pop
  41. #endif
  42. #endif
  43. extern "C" {
  44. #include "libavformat/avformat.h"
  45. #include "libavutil/imgutils.h"
  46. #include "libavutil/intreadwrite.h"
  47. #include "libavutil/bswap.h"
  48. #include "avdevice.h"
  49. }
  50. #include "decklink_common.h"
  51. static IDeckLinkIterator *decklink_create_iterator(AVFormatContext *avctx)
  52. {
  53. IDeckLinkIterator *iter;
  54. #ifdef _WIN32
  55. if (CoInitialize(NULL) < 0) {
  56. av_log(avctx, AV_LOG_ERROR, "COM initialization failed.\n");
  57. return NULL;
  58. }
  59. if (CoCreateInstance(CLSID_CDeckLinkIterator, NULL, CLSCTX_ALL,
  60. IID_IDeckLinkIterator, (void**) &iter) != S_OK) {
  61. iter = NULL;
  62. }
  63. #else
  64. iter = CreateDeckLinkIteratorInstance();
  65. #endif
  66. if (!iter) {
  67. av_log(avctx, AV_LOG_ERROR, "Could not create DeckLink iterator. "
  68. "Make sure you have DeckLink drivers " BLACKMAGIC_DECKLINK_API_VERSION_STRING " or newer installed.\n");
  69. } else {
  70. IDeckLinkAPIInformation *api;
  71. int64_t version;
  72. #ifdef _WIN32
  73. if (CoCreateInstance(CLSID_CDeckLinkAPIInformation, NULL, CLSCTX_ALL,
  74. IID_IDeckLinkAPIInformation, (void**) &api) != S_OK) {
  75. api = NULL;
  76. }
  77. #else
  78. api = CreateDeckLinkAPIInformationInstance();
  79. #endif
  80. if (api && api->GetInt(BMDDeckLinkAPIVersion, &version) == S_OK) {
  81. if (version < BLACKMAGIC_DECKLINK_API_VERSION)
  82. av_log(avctx, AV_LOG_WARNING, "Installed DeckLink drivers are too old and may be incompatible with the SDK this module was built against. "
  83. "Make sure you have DeckLink drivers " BLACKMAGIC_DECKLINK_API_VERSION_STRING " or newer installed.\n");
  84. } else {
  85. av_log(avctx, AV_LOG_ERROR, "Failed to check installed DeckLink API version.\n");
  86. }
  87. if (api)
  88. api->Release();
  89. }
  90. return iter;
  91. }
  92. static int decklink_get_attr_string(IDeckLink *dl, BMDDeckLinkAttributeID cfg_id, const char **s)
  93. {
  94. DECKLINK_STR tmp;
  95. HRESULT hr;
  96. IDeckLinkProfileAttributes *attr;
  97. *s = NULL;
  98. if (dl->QueryInterface(IID_IDeckLinkProfileAttributes, (void **)&attr) != S_OK)
  99. return AVERROR_EXTERNAL;
  100. hr = attr->GetString(cfg_id, &tmp);
  101. attr->Release();
  102. if (hr == S_OK) {
  103. *s = DECKLINK_STRDUP(tmp);
  104. DECKLINK_FREE(tmp);
  105. if (!*s)
  106. return AVERROR(ENOMEM);
  107. } else if (hr == E_FAIL) {
  108. return AVERROR_EXTERNAL;
  109. }
  110. return 0;
  111. }
  112. static int decklink_select_input(AVFormatContext *avctx, BMDDeckLinkConfigurationID cfg_id)
  113. {
  114. struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  115. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  116. BMDDeckLinkAttributeID attr_id = (cfg_id == bmdDeckLinkConfigAudioInputConnection) ? BMDDeckLinkAudioInputConnections : BMDDeckLinkVideoInputConnections;
  117. int64_t bmd_input = (cfg_id == bmdDeckLinkConfigAudioInputConnection) ? (int64_t)ctx->audio_input : (int64_t)ctx->video_input;
  118. const char *type_name = (cfg_id == bmdDeckLinkConfigAudioInputConnection) ? "audio" : "video";
  119. int64_t supported_connections = 0;
  120. HRESULT res;
  121. if (bmd_input) {
  122. res = ctx->attr->GetInt(attr_id, &supported_connections);
  123. if (res != S_OK) {
  124. av_log(avctx, AV_LOG_ERROR, "Failed to query supported %s inputs.\n", type_name);
  125. return AVERROR_EXTERNAL;
  126. }
  127. if ((supported_connections & bmd_input) != bmd_input) {
  128. av_log(avctx, AV_LOG_ERROR, "Device does not support selected %s input.\n", type_name);
  129. return AVERROR(ENOSYS);
  130. }
  131. res = ctx->cfg->SetInt(cfg_id, bmd_input);
  132. if (res != S_OK) {
  133. av_log(avctx, AV_LOG_ERROR, "Failed to select %s input.\n", type_name);
  134. return AVERROR_EXTERNAL;
  135. }
  136. }
  137. return 0;
  138. }
  139. static DECKLINK_BOOL field_order_eq(enum AVFieldOrder field_order, BMDFieldDominance bmd_field_order)
  140. {
  141. if (field_order == AV_FIELD_UNKNOWN)
  142. return true;
  143. if ((field_order == AV_FIELD_TT || field_order == AV_FIELD_TB) && bmd_field_order == bmdUpperFieldFirst)
  144. return true;
  145. if ((field_order == AV_FIELD_BB || field_order == AV_FIELD_BT) && bmd_field_order == bmdLowerFieldFirst)
  146. return true;
  147. if (field_order == AV_FIELD_PROGRESSIVE && (bmd_field_order == bmdProgressiveFrame || bmd_field_order == bmdProgressiveSegmentedFrame))
  148. return true;
  149. return false;
  150. }
  151. int ff_decklink_set_configs(AVFormatContext *avctx,
  152. decklink_direction_t direction) {
  153. struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  154. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  155. HRESULT res;
  156. if (ctx->duplex_mode) {
  157. DECKLINK_BOOL duplex_supported = false;
  158. #if BLACKMAGIC_DECKLINK_API_VERSION >= 0x0b000000
  159. IDeckLinkProfileManager *manager = NULL;
  160. if (ctx->dl->QueryInterface(IID_IDeckLinkProfileManager, (void **)&manager) == S_OK)
  161. duplex_supported = true;
  162. #else
  163. if (ctx->attr->GetFlag(BMDDeckLinkSupportsDuplexModeConfiguration, &duplex_supported) != S_OK)
  164. duplex_supported = false;
  165. #endif
  166. if (duplex_supported) {
  167. #if BLACKMAGIC_DECKLINK_API_VERSION >= 0x0b000000
  168. IDeckLinkProfile *profile = NULL;
  169. BMDProfileID bmd_profile_id;
  170. if (ctx->duplex_mode < 0 || ctx->duplex_mode >= FF_ARRAY_ELEMS(decklink_profile_id_map))
  171. return EINVAL;
  172. bmd_profile_id = decklink_profile_id_map[ctx->duplex_mode];
  173. res = manager->GetProfile(bmd_profile_id, &profile);
  174. if (res == S_OK) {
  175. res = profile->SetActive();
  176. profile->Release();
  177. }
  178. manager->Release();
  179. #else
  180. res = ctx->cfg->SetInt(bmdDeckLinkConfigDuplexMode, ctx->duplex_mode == 2 ? bmdDuplexModeFull : bmdDuplexModeHalf);
  181. #endif
  182. if (res != S_OK)
  183. av_log(avctx, AV_LOG_WARNING, "Setting duplex mode failed.\n");
  184. else
  185. av_log(avctx, AV_LOG_VERBOSE, "Successfully set duplex mode to %s duplex.\n", ctx->duplex_mode == 2 || ctx->duplex_mode == 4 ? "full" : "half");
  186. } else {
  187. av_log(avctx, AV_LOG_WARNING, "Unable to set duplex mode, because it is not supported.\n");
  188. }
  189. }
  190. if (direction == DIRECTION_IN) {
  191. int ret;
  192. ret = decklink_select_input(avctx, bmdDeckLinkConfigAudioInputConnection);
  193. if (ret < 0)
  194. return ret;
  195. ret = decklink_select_input(avctx, bmdDeckLinkConfigVideoInputConnection);
  196. if (ret < 0)
  197. return ret;
  198. }
  199. if (direction == DIRECTION_OUT && cctx->timing_offset != INT_MIN) {
  200. res = ctx->cfg->SetInt(bmdDeckLinkConfigReferenceInputTimingOffset, cctx->timing_offset);
  201. if (res != S_OK)
  202. av_log(avctx, AV_LOG_WARNING, "Setting timing offset failed.\n");
  203. }
  204. if (direction == DIRECTION_OUT && ctx->link > 0) {
  205. res = ctx->cfg->SetInt(bmdDeckLinkConfigSDIOutputLinkConfiguration, ctx->link);
  206. if (res != S_OK)
  207. av_log(avctx, AV_LOG_WARNING, "Setting link configuration failed.\n");
  208. else
  209. av_log(avctx, AV_LOG_VERBOSE, "Successfully set link configuration: 0x%x.\n", ctx->link);
  210. if (ctx->link == bmdLinkConfigurationQuadLink && cctx->sqd >= 0) {
  211. res = ctx->cfg->SetFlag(bmdDeckLinkConfigQuadLinkSDIVideoOutputSquareDivisionSplit, cctx->sqd);
  212. if (res != S_OK)
  213. av_log(avctx, AV_LOG_WARNING, "Setting SquareDivisionSplit failed.\n");
  214. else
  215. av_log(avctx, AV_LOG_VERBOSE, "Successfully set SquareDivisionSplit.\n");
  216. }
  217. }
  218. if (direction == DIRECTION_OUT && cctx->level_a >= 0) {
  219. DECKLINK_BOOL level_a_supported = false;
  220. if (ctx->attr->GetFlag(BMDDeckLinkSupportsSMPTELevelAOutput, &level_a_supported) != S_OK)
  221. level_a_supported = false;
  222. if (level_a_supported) {
  223. res = ctx->cfg->SetFlag(bmdDeckLinkConfigSMPTELevelAOutput, cctx->level_a);
  224. if (res != S_OK)
  225. av_log(avctx, AV_LOG_WARNING, "Setting SMPTE levelA failed.\n");
  226. else
  227. av_log(avctx, AV_LOG_VERBOSE, "Successfully set SMPTE levelA.\n");
  228. } else {
  229. av_log(avctx, AV_LOG_WARNING, "Unable to set SMPTE levelA mode, because it is not supported.\n");
  230. }
  231. }
  232. return 0;
  233. }
  234. int ff_decklink_set_format(AVFormatContext *avctx,
  235. int width, int height,
  236. int tb_num, int tb_den,
  237. enum AVFieldOrder field_order,
  238. decklink_direction_t direction)
  239. {
  240. struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  241. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  242. #if BLACKMAGIC_DECKLINK_API_VERSION >= 0x0b000000
  243. DECKLINK_BOOL support;
  244. #else
  245. BMDDisplayModeSupport support;
  246. #endif
  247. IDeckLinkDisplayModeIterator *itermode;
  248. IDeckLinkDisplayMode *mode;
  249. int i = 1;
  250. HRESULT res;
  251. av_log(avctx, AV_LOG_DEBUG, "Trying to find mode for frame size %dx%d, frame timing %d/%d, field order %d, direction %d, format code %s\n",
  252. width, height, tb_num, tb_den, field_order, direction, cctx->format_code ? cctx->format_code : "(unset)");
  253. if (direction == DIRECTION_IN) {
  254. res = ctx->dli->GetDisplayModeIterator (&itermode);
  255. } else {
  256. res = ctx->dlo->GetDisplayModeIterator (&itermode);
  257. }
  258. if (res!= S_OK) {
  259. av_log(avctx, AV_LOG_ERROR, "Could not get Display Mode Iterator\n");
  260. return AVERROR(EIO);
  261. }
  262. char format_buf[] = " ";
  263. if (cctx->format_code)
  264. memcpy(format_buf, cctx->format_code, FFMIN(strlen(cctx->format_code), sizeof(format_buf)));
  265. BMDDisplayMode target_mode = (BMDDisplayMode)AV_RB32(format_buf);
  266. AVRational target_tb = av_make_q(tb_num, tb_den);
  267. ctx->bmd_mode = bmdModeUnknown;
  268. while ((ctx->bmd_mode == bmdModeUnknown) && itermode->Next(&mode) == S_OK) {
  269. BMDTimeValue bmd_tb_num, bmd_tb_den;
  270. int bmd_width = mode->GetWidth();
  271. int bmd_height = mode->GetHeight();
  272. BMDDisplayMode bmd_mode = mode->GetDisplayMode();
  273. BMDFieldDominance bmd_field_dominance = mode->GetFieldDominance();
  274. mode->GetFrameRate(&bmd_tb_num, &bmd_tb_den);
  275. AVRational mode_tb = av_make_q(bmd_tb_num, bmd_tb_den);
  276. if ((bmd_width == width &&
  277. bmd_height == height &&
  278. !av_cmp_q(mode_tb, target_tb) &&
  279. field_order_eq(field_order, bmd_field_dominance))
  280. || target_mode == bmd_mode) {
  281. ctx->bmd_mode = bmd_mode;
  282. ctx->bmd_width = bmd_width;
  283. ctx->bmd_height = bmd_height;
  284. ctx->bmd_tb_den = bmd_tb_den;
  285. ctx->bmd_tb_num = bmd_tb_num;
  286. ctx->bmd_field_dominance = bmd_field_dominance;
  287. av_log(avctx, AV_LOG_INFO, "Found Decklink mode %d x %d with rate %.2f%s\n",
  288. bmd_width, bmd_height, 1/av_q2d(mode_tb),
  289. (ctx->bmd_field_dominance==bmdLowerFieldFirst || ctx->bmd_field_dominance==bmdUpperFieldFirst)?"(i)":"");
  290. }
  291. mode->Release();
  292. i++;
  293. }
  294. itermode->Release();
  295. if (ctx->bmd_mode == bmdModeUnknown)
  296. return -1;
  297. #if BLACKMAGIC_DECKLINK_API_VERSION >= 0x0b050000
  298. if (direction == DIRECTION_IN) {
  299. BMDDisplayMode actualMode = ctx->bmd_mode;
  300. if (ctx->dli->DoesSupportVideoMode(ctx->video_input, ctx->bmd_mode, ctx->raw_format,
  301. bmdNoVideoInputConversion, bmdSupportedVideoModeDefault,
  302. &actualMode, &support) != S_OK || !support || ctx->bmd_mode != actualMode)
  303. return -1;
  304. } else {
  305. BMDDisplayMode actualMode = ctx->bmd_mode;
  306. if (ctx->dlo->DoesSupportVideoMode(bmdVideoConnectionUnspecified, ctx->bmd_mode, ctx->raw_format,
  307. bmdNoVideoOutputConversion, bmdSupportedVideoModeDefault,
  308. &actualMode, &support) != S_OK || !support || ctx->bmd_mode != actualMode)
  309. return -1;
  310. }
  311. return 0;
  312. #elif BLACKMAGIC_DECKLINK_API_VERSION >= 0x0b000000
  313. if (direction == DIRECTION_IN) {
  314. if (ctx->dli->DoesSupportVideoMode(ctx->video_input, ctx->bmd_mode, ctx->raw_format,
  315. bmdSupportedVideoModeDefault,
  316. &support) != S_OK)
  317. return -1;
  318. } else {
  319. BMDDisplayMode actualMode = ctx->bmd_mode;
  320. if (ctx->dlo->DoesSupportVideoMode(bmdVideoConnectionUnspecified, ctx->bmd_mode, ctx->raw_format,
  321. bmdSupportedVideoModeDefault,
  322. &actualMode, &support) != S_OK || !support || ctx->bmd_mode != actualMode) {
  323. return -1;
  324. }
  325. }
  326. if (support)
  327. return 0;
  328. #else
  329. if (direction == DIRECTION_IN) {
  330. if (ctx->dli->DoesSupportVideoMode(ctx->bmd_mode, ctx->raw_format,
  331. bmdVideoOutputFlagDefault,
  332. &support, NULL) != S_OK)
  333. return -1;
  334. } else {
  335. if (!ctx->supports_vanc || ctx->dlo->DoesSupportVideoMode(ctx->bmd_mode, ctx->raw_format,
  336. bmdVideoOutputVANC,
  337. &support, NULL) != S_OK || support != bmdDisplayModeSupported) {
  338. /* Try without VANC enabled */
  339. if (ctx->dlo->DoesSupportVideoMode(ctx->bmd_mode, ctx->raw_format,
  340. bmdVideoOutputFlagDefault,
  341. &support, NULL) != S_OK) {
  342. return -1;
  343. }
  344. ctx->supports_vanc = 0;
  345. }
  346. }
  347. if (support == bmdDisplayModeSupported)
  348. return 0;
  349. #endif
  350. return -1;
  351. }
  352. int ff_decklink_set_format(AVFormatContext *avctx, decklink_direction_t direction) {
  353. return ff_decklink_set_format(avctx, 0, 0, 0, 0, AV_FIELD_UNKNOWN, direction);
  354. }
  355. int ff_decklink_list_devices(AVFormatContext *avctx,
  356. struct AVDeviceInfoList *device_list,
  357. int show_inputs, int show_outputs)
  358. {
  359. IDeckLink *dl = NULL;
  360. IDeckLinkIterator *iter = decklink_create_iterator(avctx);
  361. int ret = 0;
  362. if (!iter)
  363. return AVERROR(EIO);
  364. while (ret == 0 && iter->Next(&dl) == S_OK) {
  365. IDeckLinkOutput *output_config;
  366. IDeckLinkInput *input_config;
  367. const char *display_name = NULL;
  368. const char *unique_name = NULL;
  369. AVDeviceInfo *new_device = NULL;
  370. int add = 0;
  371. ret = decklink_get_attr_string(dl, BMDDeckLinkDisplayName, &display_name);
  372. if (ret < 0)
  373. goto next;
  374. ret = decklink_get_attr_string(dl, BMDDeckLinkDeviceHandle, &unique_name);
  375. if (ret < 0)
  376. goto next;
  377. if (show_outputs) {
  378. if (dl->QueryInterface(IID_IDeckLinkOutput, (void **)&output_config) == S_OK) {
  379. output_config->Release();
  380. add = 1;
  381. }
  382. }
  383. if (show_inputs) {
  384. if (dl->QueryInterface(IID_IDeckLinkInput, (void **)&input_config) == S_OK) {
  385. input_config->Release();
  386. add = 1;
  387. }
  388. }
  389. if (add == 1) {
  390. new_device = (AVDeviceInfo *) av_mallocz(sizeof(AVDeviceInfo));
  391. if (!new_device) {
  392. ret = AVERROR(ENOMEM);
  393. goto next;
  394. }
  395. new_device->device_name = av_strdup(unique_name ? unique_name : display_name);
  396. new_device->device_description = av_strdup(display_name);
  397. if (!new_device->device_name ||
  398. !new_device->device_description ||
  399. av_dynarray_add_nofree(&device_list->devices, &device_list->nb_devices, new_device) < 0) {
  400. ret = AVERROR(ENOMEM);
  401. av_freep(&new_device->device_name);
  402. av_freep(&new_device->device_description);
  403. av_freep(&new_device);
  404. goto next;
  405. }
  406. }
  407. next:
  408. av_freep(&display_name);
  409. av_freep(&unique_name);
  410. dl->Release();
  411. }
  412. iter->Release();
  413. return ret;
  414. }
  415. /* This is a wrapper around the ff_decklink_list_devices() which dumps the
  416. output to av_log() and exits (for backward compatibility with the
  417. "-list_devices" argument). */
  418. void ff_decklink_list_devices_legacy(AVFormatContext *avctx,
  419. int show_inputs, int show_outputs)
  420. {
  421. struct AVDeviceInfoList *device_list = NULL;
  422. int ret;
  423. device_list = (struct AVDeviceInfoList *) av_mallocz(sizeof(AVDeviceInfoList));
  424. if (!device_list)
  425. return;
  426. ret = ff_decklink_list_devices(avctx, device_list, show_inputs, show_outputs);
  427. if (ret == 0) {
  428. av_log(avctx, AV_LOG_INFO, "Blackmagic DeckLink %s devices:\n",
  429. show_inputs ? "input" : "output");
  430. for (int i = 0; i < device_list->nb_devices; i++) {
  431. av_log(avctx, AV_LOG_INFO, "\t'%s'\n", device_list->devices[i]->device_description);
  432. }
  433. }
  434. avdevice_free_list_devices(&device_list);
  435. }
  436. int ff_decklink_list_formats(AVFormatContext *avctx, decklink_direction_t direction)
  437. {
  438. struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  439. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  440. IDeckLinkDisplayModeIterator *itermode;
  441. IDeckLinkDisplayMode *mode;
  442. uint32_t format_code;
  443. HRESULT res;
  444. if (direction == DIRECTION_IN) {
  445. int ret;
  446. ret = decklink_select_input(avctx, bmdDeckLinkConfigAudioInputConnection);
  447. if (ret < 0)
  448. return ret;
  449. ret = decklink_select_input(avctx, bmdDeckLinkConfigVideoInputConnection);
  450. if (ret < 0)
  451. return ret;
  452. res = ctx->dli->GetDisplayModeIterator (&itermode);
  453. } else {
  454. res = ctx->dlo->GetDisplayModeIterator (&itermode);
  455. }
  456. if (res!= S_OK) {
  457. av_log(avctx, AV_LOG_ERROR, "Could not get Display Mode Iterator\n");
  458. return AVERROR(EIO);
  459. }
  460. av_log(avctx, AV_LOG_INFO, "Supported formats for '%s':\n\tformat_code\tdescription",
  461. avctx->url);
  462. while (itermode->Next(&mode) == S_OK) {
  463. BMDTimeValue tb_num, tb_den;
  464. mode->GetFrameRate(&tb_num, &tb_den);
  465. format_code = av_bswap32(mode->GetDisplayMode());
  466. av_log(avctx, AV_LOG_INFO, "\n\t%.4s\t\t%ldx%ld at %d/%d fps",
  467. (char*) &format_code, mode->GetWidth(), mode->GetHeight(),
  468. (int) tb_den, (int) tb_num);
  469. switch (mode->GetFieldDominance()) {
  470. case bmdLowerFieldFirst:
  471. av_log(avctx, AV_LOG_INFO, " (interlaced, lower field first)"); break;
  472. case bmdUpperFieldFirst:
  473. av_log(avctx, AV_LOG_INFO, " (interlaced, upper field first)"); break;
  474. }
  475. mode->Release();
  476. }
  477. av_log(avctx, AV_LOG_INFO, "\n");
  478. itermode->Release();
  479. return 0;
  480. }
  481. void ff_decklink_cleanup(AVFormatContext *avctx)
  482. {
  483. struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  484. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  485. if (ctx->dli)
  486. ctx->dli->Release();
  487. if (ctx->dlo)
  488. ctx->dlo->Release();
  489. if (ctx->attr)
  490. ctx->attr->Release();
  491. if (ctx->cfg)
  492. ctx->cfg->Release();
  493. if (ctx->dl)
  494. ctx->dl->Release();
  495. }
  496. int ff_decklink_init_device(AVFormatContext *avctx, const char* name)
  497. {
  498. struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  499. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  500. IDeckLink *dl = NULL;
  501. IDeckLinkIterator *iter = decklink_create_iterator(avctx);
  502. if (!iter)
  503. return AVERROR_EXTERNAL;
  504. while (iter->Next(&dl) == S_OK) {
  505. const char *display_name = NULL;
  506. const char *unique_name = NULL;
  507. decklink_get_attr_string(dl, BMDDeckLinkDisplayName, &display_name);
  508. decklink_get_attr_string(dl, BMDDeckLinkDeviceHandle, &unique_name);
  509. if (display_name && !strcmp(name, display_name) || unique_name && !strcmp(name, unique_name)) {
  510. av_free((void *)unique_name);
  511. av_free((void *)display_name);
  512. ctx->dl = dl;
  513. break;
  514. }
  515. av_free((void *)display_name);
  516. av_free((void *)unique_name);
  517. dl->Release();
  518. }
  519. iter->Release();
  520. if (!ctx->dl)
  521. return AVERROR(ENXIO);
  522. if (ctx->dl->QueryInterface(IID_IDeckLinkConfiguration, (void **)&ctx->cfg) != S_OK) {
  523. av_log(avctx, AV_LOG_ERROR, "Could not get configuration interface for '%s'\n", name);
  524. ff_decklink_cleanup(avctx);
  525. return AVERROR_EXTERNAL;
  526. }
  527. if (ctx->dl->QueryInterface(IID_IDeckLinkProfileAttributes, (void **)&ctx->attr) != S_OK) {
  528. av_log(avctx, AV_LOG_ERROR, "Could not get attributes interface for '%s'\n", name);
  529. ff_decklink_cleanup(avctx);
  530. return AVERROR_EXTERNAL;
  531. }
  532. return 0;
  533. }