You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

525 lines
19KB

  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. return iter;
  70. }
  71. static int decklink_get_attr_string(IDeckLink *dl, BMDDeckLinkAttributeID cfg_id, const char **s)
  72. {
  73. DECKLINK_STR tmp;
  74. HRESULT hr;
  75. IDeckLinkProfileAttributes *attr;
  76. *s = NULL;
  77. if (dl->QueryInterface(IID_IDeckLinkProfileAttributes, (void **)&attr) != S_OK)
  78. return AVERROR_EXTERNAL;
  79. hr = attr->GetString(cfg_id, &tmp);
  80. attr->Release();
  81. if (hr == S_OK) {
  82. *s = DECKLINK_STRDUP(tmp);
  83. DECKLINK_FREE(tmp);
  84. if (!*s)
  85. return AVERROR(ENOMEM);
  86. } else if (hr == E_FAIL) {
  87. return AVERROR_EXTERNAL;
  88. }
  89. return 0;
  90. }
  91. static int decklink_select_input(AVFormatContext *avctx, BMDDeckLinkConfigurationID cfg_id)
  92. {
  93. struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  94. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  95. BMDDeckLinkAttributeID attr_id = (cfg_id == bmdDeckLinkConfigAudioInputConnection) ? BMDDeckLinkAudioInputConnections : BMDDeckLinkVideoInputConnections;
  96. int64_t bmd_input = (cfg_id == bmdDeckLinkConfigAudioInputConnection) ? (int64_t)ctx->audio_input : (int64_t)ctx->video_input;
  97. const char *type_name = (cfg_id == bmdDeckLinkConfigAudioInputConnection) ? "audio" : "video";
  98. int64_t supported_connections = 0;
  99. HRESULT res;
  100. if (bmd_input) {
  101. res = ctx->attr->GetInt(attr_id, &supported_connections);
  102. if (res != S_OK) {
  103. av_log(avctx, AV_LOG_ERROR, "Failed to query supported %s inputs.\n", type_name);
  104. return AVERROR_EXTERNAL;
  105. }
  106. if ((supported_connections & bmd_input) != bmd_input) {
  107. av_log(avctx, AV_LOG_ERROR, "Device does not support selected %s input.\n", type_name);
  108. return AVERROR(ENOSYS);
  109. }
  110. res = ctx->cfg->SetInt(cfg_id, bmd_input);
  111. if (res != S_OK) {
  112. av_log(avctx, AV_LOG_ERROR, "Failed to select %s input.\n", type_name);
  113. return AVERROR_EXTERNAL;
  114. }
  115. }
  116. return 0;
  117. }
  118. static DECKLINK_BOOL field_order_eq(enum AVFieldOrder field_order, BMDFieldDominance bmd_field_order)
  119. {
  120. if (field_order == AV_FIELD_UNKNOWN)
  121. return true;
  122. if ((field_order == AV_FIELD_TT || field_order == AV_FIELD_TB) && bmd_field_order == bmdUpperFieldFirst)
  123. return true;
  124. if ((field_order == AV_FIELD_BB || field_order == AV_FIELD_BT) && bmd_field_order == bmdLowerFieldFirst)
  125. return true;
  126. if (field_order == AV_FIELD_PROGRESSIVE && (bmd_field_order == bmdProgressiveFrame || bmd_field_order == bmdProgressiveSegmentedFrame))
  127. return true;
  128. return false;
  129. }
  130. int ff_decklink_set_configs(AVFormatContext *avctx,
  131. decklink_direction_t direction) {
  132. struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  133. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  134. HRESULT res;
  135. if (ctx->duplex_mode) {
  136. DECKLINK_BOOL duplex_supported = false;
  137. #if BLACKMAGIC_DECKLINK_API_VERSION >= 0x0b000000
  138. IDeckLinkProfileManager *manager = NULL;
  139. if (ctx->dl->QueryInterface(IID_IDeckLinkProfileManager, (void **)&manager) == S_OK)
  140. duplex_supported = true;
  141. #else
  142. if (ctx->attr->GetFlag(BMDDeckLinkSupportsDuplexModeConfiguration, &duplex_supported) != S_OK)
  143. duplex_supported = false;
  144. #endif
  145. if (duplex_supported) {
  146. #if BLACKMAGIC_DECKLINK_API_VERSION >= 0x0b000000
  147. IDeckLinkProfile *profile = NULL;
  148. BMDProfileID bmd_profile_id = ctx->duplex_mode == 2 ? bmdProfileOneSubDeviceFullDuplex : bmdProfileTwoSubDevicesHalfDuplex;
  149. res = manager->GetProfile(bmd_profile_id, &profile);
  150. if (res == S_OK) {
  151. res = profile->SetActive();
  152. profile->Release();
  153. }
  154. manager->Release();
  155. #else
  156. res = ctx->cfg->SetInt(bmdDeckLinkConfigDuplexMode, ctx->duplex_mode == 2 ? bmdDuplexModeFull : bmdDuplexModeHalf);
  157. #endif
  158. if (res != S_OK)
  159. av_log(avctx, AV_LOG_WARNING, "Setting duplex mode failed.\n");
  160. else
  161. av_log(avctx, AV_LOG_VERBOSE, "Successfully set duplex mode to %s duplex.\n", ctx->duplex_mode == 2 ? "full" : "half");
  162. } else {
  163. av_log(avctx, AV_LOG_WARNING, "Unable to set duplex mode, because it is not supported.\n");
  164. }
  165. }
  166. if (direction == DIRECTION_IN) {
  167. int ret;
  168. ret = decklink_select_input(avctx, bmdDeckLinkConfigAudioInputConnection);
  169. if (ret < 0)
  170. return ret;
  171. ret = decklink_select_input(avctx, bmdDeckLinkConfigVideoInputConnection);
  172. if (ret < 0)
  173. return ret;
  174. }
  175. if (direction == DIRECTION_OUT && cctx->timing_offset != INT_MIN) {
  176. res = ctx->cfg->SetInt(bmdDeckLinkConfigReferenceInputTimingOffset, cctx->timing_offset);
  177. if (res != S_OK)
  178. av_log(avctx, AV_LOG_WARNING, "Setting timing offset failed.\n");
  179. }
  180. return 0;
  181. }
  182. int ff_decklink_set_format(AVFormatContext *avctx,
  183. int width, int height,
  184. int tb_num, int tb_den,
  185. enum AVFieldOrder field_order,
  186. decklink_direction_t direction, int num)
  187. {
  188. struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  189. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  190. #if BLACKMAGIC_DECKLINK_API_VERSION >= 0x0b000000
  191. DECKLINK_BOOL support;
  192. #else
  193. BMDDisplayModeSupport support;
  194. #endif
  195. IDeckLinkDisplayModeIterator *itermode;
  196. IDeckLinkDisplayMode *mode;
  197. int i = 1;
  198. HRESULT res;
  199. 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",
  200. width, height, tb_num, tb_den, field_order, direction, num, (cctx->format_code) ? cctx->format_code : "(unset)");
  201. if (direction == DIRECTION_IN) {
  202. res = ctx->dli->GetDisplayModeIterator (&itermode);
  203. } else {
  204. res = ctx->dlo->GetDisplayModeIterator (&itermode);
  205. }
  206. if (res!= S_OK) {
  207. av_log(avctx, AV_LOG_ERROR, "Could not get Display Mode Iterator\n");
  208. return AVERROR(EIO);
  209. }
  210. char format_buf[] = " ";
  211. if (cctx->format_code)
  212. memcpy(format_buf, cctx->format_code, FFMIN(strlen(cctx->format_code), sizeof(format_buf)));
  213. BMDDisplayMode target_mode = (BMDDisplayMode)AV_RB32(format_buf);
  214. AVRational target_tb = av_make_q(tb_num, tb_den);
  215. ctx->bmd_mode = bmdModeUnknown;
  216. while ((ctx->bmd_mode == bmdModeUnknown) && itermode->Next(&mode) == S_OK) {
  217. BMDTimeValue bmd_tb_num, bmd_tb_den;
  218. int bmd_width = mode->GetWidth();
  219. int bmd_height = mode->GetHeight();
  220. BMDDisplayMode bmd_mode = mode->GetDisplayMode();
  221. BMDFieldDominance bmd_field_dominance = mode->GetFieldDominance();
  222. mode->GetFrameRate(&bmd_tb_num, &bmd_tb_den);
  223. AVRational mode_tb = av_make_q(bmd_tb_num, bmd_tb_den);
  224. if ((bmd_width == width &&
  225. bmd_height == height &&
  226. !av_cmp_q(mode_tb, target_tb) &&
  227. field_order_eq(field_order, bmd_field_dominance))
  228. || i == num
  229. || target_mode == bmd_mode) {
  230. ctx->bmd_mode = bmd_mode;
  231. ctx->bmd_width = bmd_width;
  232. ctx->bmd_height = bmd_height;
  233. ctx->bmd_tb_den = bmd_tb_den;
  234. ctx->bmd_tb_num = bmd_tb_num;
  235. ctx->bmd_field_dominance = bmd_field_dominance;
  236. av_log(avctx, AV_LOG_INFO, "Found Decklink mode %d x %d with rate %.2f%s\n",
  237. bmd_width, bmd_height, 1/av_q2d(mode_tb),
  238. (ctx->bmd_field_dominance==bmdLowerFieldFirst || ctx->bmd_field_dominance==bmdUpperFieldFirst)?"(i)":"");
  239. }
  240. mode->Release();
  241. i++;
  242. }
  243. itermode->Release();
  244. if (ctx->bmd_mode == bmdModeUnknown)
  245. return -1;
  246. #if BLACKMAGIC_DECKLINK_API_VERSION >= 0x0b000000
  247. if (direction == DIRECTION_IN) {
  248. if (ctx->dli->DoesSupportVideoMode(ctx->video_input, ctx->bmd_mode, (BMDPixelFormat) cctx->raw_format,
  249. bmdSupportedVideoModeDefault,
  250. &support) != S_OK)
  251. return -1;
  252. } else {
  253. BMDDisplayMode actualMode = ctx->bmd_mode;
  254. if (ctx->dlo->DoesSupportVideoMode(bmdVideoConnectionUnspecified, ctx->bmd_mode, ctx->raw_format,
  255. bmdSupportedVideoModeDefault,
  256. &actualMode, &support) != S_OK || !support || ctx->bmd_mode != actualMode) {
  257. return -1;
  258. }
  259. }
  260. if (support)
  261. return 0;
  262. #else
  263. if (direction == DIRECTION_IN) {
  264. if (ctx->dli->DoesSupportVideoMode(ctx->bmd_mode, (BMDPixelFormat) cctx->raw_format,
  265. bmdVideoOutputFlagDefault,
  266. &support, NULL) != S_OK)
  267. return -1;
  268. } else {
  269. if (!ctx->supports_vanc || ctx->dlo->DoesSupportVideoMode(ctx->bmd_mode, ctx->raw_format,
  270. bmdVideoOutputVANC,
  271. &support, NULL) != S_OK || support != bmdDisplayModeSupported) {
  272. /* Try without VANC enabled */
  273. if (ctx->dlo->DoesSupportVideoMode(ctx->bmd_mode, ctx->raw_format,
  274. bmdVideoOutputFlagDefault,
  275. &support, NULL) != S_OK) {
  276. return -1;
  277. }
  278. ctx->supports_vanc = 0;
  279. }
  280. }
  281. if (support == bmdDisplayModeSupported)
  282. return 0;
  283. #endif
  284. return -1;
  285. }
  286. int ff_decklink_set_format(AVFormatContext *avctx, decklink_direction_t direction, int num) {
  287. return ff_decklink_set_format(avctx, 0, 0, 0, 0, AV_FIELD_UNKNOWN, direction, num);
  288. }
  289. int ff_decklink_list_devices(AVFormatContext *avctx,
  290. struct AVDeviceInfoList *device_list,
  291. int show_inputs, int show_outputs)
  292. {
  293. IDeckLink *dl = NULL;
  294. IDeckLinkIterator *iter = decklink_create_iterator(avctx);
  295. int ret = 0;
  296. if (!iter)
  297. return AVERROR(EIO);
  298. while (ret == 0 && iter->Next(&dl) == S_OK) {
  299. IDeckLinkOutput *output_config;
  300. IDeckLinkInput *input_config;
  301. const char *display_name = NULL;
  302. const char *unique_name = NULL;
  303. AVDeviceInfo *new_device = NULL;
  304. int add = 0;
  305. ret = decklink_get_attr_string(dl, BMDDeckLinkDisplayName, &display_name);
  306. if (ret < 0)
  307. goto next;
  308. ret = decklink_get_attr_string(dl, BMDDeckLinkDeviceHandle, &unique_name);
  309. if (ret < 0)
  310. goto next;
  311. if (show_outputs) {
  312. if (dl->QueryInterface(IID_IDeckLinkOutput, (void **)&output_config) == S_OK) {
  313. output_config->Release();
  314. add = 1;
  315. }
  316. }
  317. if (show_inputs) {
  318. if (dl->QueryInterface(IID_IDeckLinkInput, (void **)&input_config) == S_OK) {
  319. input_config->Release();
  320. add = 1;
  321. }
  322. }
  323. if (add == 1) {
  324. new_device = (AVDeviceInfo *) av_mallocz(sizeof(AVDeviceInfo));
  325. if (!new_device) {
  326. ret = AVERROR(ENOMEM);
  327. goto next;
  328. }
  329. new_device->device_name = av_strdup(unique_name ? unique_name : display_name);
  330. new_device->device_description = av_strdup(display_name);
  331. if (!new_device->device_name ||
  332. !new_device->device_description ||
  333. av_dynarray_add_nofree(&device_list->devices, &device_list->nb_devices, new_device) < 0) {
  334. ret = AVERROR(ENOMEM);
  335. av_freep(&new_device->device_name);
  336. av_freep(&new_device->device_description);
  337. av_freep(&new_device);
  338. goto next;
  339. }
  340. }
  341. next:
  342. av_freep(&display_name);
  343. av_freep(&unique_name);
  344. dl->Release();
  345. }
  346. iter->Release();
  347. return ret;
  348. }
  349. /* This is a wrapper around the ff_decklink_list_devices() which dumps the
  350. output to av_log() and exits (for backward compatibility with the
  351. "-list_devices" argument). */
  352. void ff_decklink_list_devices_legacy(AVFormatContext *avctx,
  353. int show_inputs, int show_outputs)
  354. {
  355. struct AVDeviceInfoList *device_list = NULL;
  356. int ret;
  357. device_list = (struct AVDeviceInfoList *) av_mallocz(sizeof(AVDeviceInfoList));
  358. if (!device_list)
  359. return;
  360. ret = ff_decklink_list_devices(avctx, device_list, show_inputs, show_outputs);
  361. if (ret == 0) {
  362. av_log(avctx, AV_LOG_INFO, "Blackmagic DeckLink %s devices:\n",
  363. show_inputs ? "input" : "output");
  364. for (int i = 0; i < device_list->nb_devices; i++) {
  365. av_log(avctx, AV_LOG_INFO, "\t'%s'\n", device_list->devices[i]->device_description);
  366. }
  367. }
  368. avdevice_free_list_devices(&device_list);
  369. }
  370. int ff_decklink_list_formats(AVFormatContext *avctx, decklink_direction_t direction)
  371. {
  372. struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  373. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  374. IDeckLinkDisplayModeIterator *itermode;
  375. IDeckLinkDisplayMode *mode;
  376. uint32_t format_code;
  377. HRESULT res;
  378. if (direction == DIRECTION_IN) {
  379. int ret;
  380. ret = decklink_select_input(avctx, bmdDeckLinkConfigAudioInputConnection);
  381. if (ret < 0)
  382. return ret;
  383. ret = decklink_select_input(avctx, bmdDeckLinkConfigVideoInputConnection);
  384. if (ret < 0)
  385. return ret;
  386. res = ctx->dli->GetDisplayModeIterator (&itermode);
  387. } else {
  388. res = ctx->dlo->GetDisplayModeIterator (&itermode);
  389. }
  390. if (res!= S_OK) {
  391. av_log(avctx, AV_LOG_ERROR, "Could not get Display Mode Iterator\n");
  392. return AVERROR(EIO);
  393. }
  394. av_log(avctx, AV_LOG_INFO, "Supported formats for '%s':\n\tformat_code\tdescription",
  395. avctx->url);
  396. while (itermode->Next(&mode) == S_OK) {
  397. BMDTimeValue tb_num, tb_den;
  398. mode->GetFrameRate(&tb_num, &tb_den);
  399. format_code = av_bswap32(mode->GetDisplayMode());
  400. av_log(avctx, AV_LOG_INFO, "\n\t%.4s\t\t%ldx%ld at %d/%d fps",
  401. (char*) &format_code, mode->GetWidth(), mode->GetHeight(),
  402. (int) tb_den, (int) tb_num);
  403. switch (mode->GetFieldDominance()) {
  404. case bmdLowerFieldFirst:
  405. av_log(avctx, AV_LOG_INFO, " (interlaced, lower field first)"); break;
  406. case bmdUpperFieldFirst:
  407. av_log(avctx, AV_LOG_INFO, " (interlaced, upper field first)"); break;
  408. }
  409. mode->Release();
  410. }
  411. av_log(avctx, AV_LOG_INFO, "\n");
  412. itermode->Release();
  413. return 0;
  414. }
  415. void ff_decklink_cleanup(AVFormatContext *avctx)
  416. {
  417. struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  418. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  419. if (ctx->dli)
  420. ctx->dli->Release();
  421. if (ctx->dlo)
  422. ctx->dlo->Release();
  423. if (ctx->attr)
  424. ctx->attr->Release();
  425. if (ctx->cfg)
  426. ctx->cfg->Release();
  427. if (ctx->dl)
  428. ctx->dl->Release();
  429. }
  430. int ff_decklink_init_device(AVFormatContext *avctx, const char* name)
  431. {
  432. struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  433. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  434. IDeckLink *dl = NULL;
  435. IDeckLinkIterator *iter = decklink_create_iterator(avctx);
  436. if (!iter)
  437. return AVERROR_EXTERNAL;
  438. while (iter->Next(&dl) == S_OK) {
  439. const char *display_name = NULL;
  440. const char *unique_name = NULL;
  441. decklink_get_attr_string(dl, BMDDeckLinkDisplayName, &display_name);
  442. decklink_get_attr_string(dl, BMDDeckLinkDeviceHandle, &unique_name);
  443. if (display_name && !strcmp(name, display_name) || unique_name && !strcmp(name, unique_name)) {
  444. av_free((void *)unique_name);
  445. av_free((void *)display_name);
  446. ctx->dl = dl;
  447. break;
  448. }
  449. av_free((void *)display_name);
  450. av_free((void *)unique_name);
  451. dl->Release();
  452. }
  453. iter->Release();
  454. if (!ctx->dl)
  455. return AVERROR(ENXIO);
  456. if (ctx->dl->QueryInterface(IID_IDeckLinkConfiguration, (void **)&ctx->cfg) != S_OK) {
  457. av_log(avctx, AV_LOG_ERROR, "Could not get configuration interface for '%s'\n", name);
  458. ff_decklink_cleanup(avctx);
  459. return AVERROR_EXTERNAL;
  460. }
  461. if (ctx->dl->QueryInterface(IID_IDeckLinkProfileAttributes, (void **)&ctx->attr) != S_OK) {
  462. av_log(avctx, AV_LOG_ERROR, "Could not get attributes interface for '%s'\n", name);
  463. ff_decklink_cleanup(avctx);
  464. return AVERROR_EXTERNAL;
  465. }
  466. return 0;
  467. }