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.

479 lines
17KB

  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. int decklink_get_attr_string(IDeckLink *dl, BMDDeckLinkAttributeID cfg_id, const char **s)
  72. {
  73. DECKLINK_STR tmp;
  74. HRESULT hr;
  75. IDeckLinkAttributes *attr;
  76. *s = NULL;
  77. if (dl->QueryInterface(IID_IDeckLinkAttributes, (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 (ctx->attr->GetFlag(BMDDeckLinkSupportsDuplexModeConfiguration, &duplex_supported) != S_OK)
  138. duplex_supported = false;
  139. if (duplex_supported) {
  140. res = ctx->cfg->SetInt(bmdDeckLinkConfigDuplexMode, ctx->duplex_mode == 2 ? bmdDuplexModeFull : bmdDuplexModeHalf);
  141. if (res != S_OK)
  142. av_log(avctx, AV_LOG_WARNING, "Setting duplex mode failed.\n");
  143. else
  144. av_log(avctx, AV_LOG_VERBOSE, "Successfully set duplex mode to %s duplex.\n", ctx->duplex_mode == 2 ? "full" : "half");
  145. } else {
  146. av_log(avctx, AV_LOG_WARNING, "Unable to set duplex mode, because it is not supported.\n");
  147. }
  148. }
  149. if (direction == DIRECTION_IN) {
  150. int ret;
  151. ret = decklink_select_input(avctx, bmdDeckLinkConfigAudioInputConnection);
  152. if (ret < 0)
  153. return ret;
  154. ret = decklink_select_input(avctx, bmdDeckLinkConfigVideoInputConnection);
  155. if (ret < 0)
  156. return ret;
  157. }
  158. return 0;
  159. }
  160. int ff_decklink_set_format(AVFormatContext *avctx,
  161. int width, int height,
  162. int tb_num, int tb_den,
  163. enum AVFieldOrder field_order,
  164. decklink_direction_t direction, int num)
  165. {
  166. struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  167. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  168. BMDDisplayModeSupport support;
  169. IDeckLinkDisplayModeIterator *itermode;
  170. IDeckLinkDisplayMode *mode;
  171. int i = 1;
  172. HRESULT res;
  173. 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",
  174. width, height, tb_num, tb_den, field_order, direction, num, (cctx->format_code) ? cctx->format_code : "(unset)");
  175. if (direction == DIRECTION_IN) {
  176. res = ctx->dli->GetDisplayModeIterator (&itermode);
  177. } else {
  178. res = ctx->dlo->GetDisplayModeIterator (&itermode);
  179. }
  180. if (res!= S_OK) {
  181. av_log(avctx, AV_LOG_ERROR, "Could not get Display Mode Iterator\n");
  182. return AVERROR(EIO);
  183. }
  184. char format_buf[] = " ";
  185. if (cctx->format_code)
  186. memcpy(format_buf, cctx->format_code, FFMIN(strlen(cctx->format_code), sizeof(format_buf)));
  187. BMDDisplayMode target_mode = (BMDDisplayMode)AV_RB32(format_buf);
  188. AVRational target_tb = av_make_q(tb_num, tb_den);
  189. ctx->bmd_mode = bmdModeUnknown;
  190. while ((ctx->bmd_mode == bmdModeUnknown) && itermode->Next(&mode) == S_OK) {
  191. BMDTimeValue bmd_tb_num, bmd_tb_den;
  192. int bmd_width = mode->GetWidth();
  193. int bmd_height = mode->GetHeight();
  194. BMDDisplayMode bmd_mode = mode->GetDisplayMode();
  195. BMDFieldDominance bmd_field_dominance = mode->GetFieldDominance();
  196. mode->GetFrameRate(&bmd_tb_num, &bmd_tb_den);
  197. AVRational mode_tb = av_make_q(bmd_tb_num, bmd_tb_den);
  198. if ((bmd_width == width &&
  199. bmd_height == height &&
  200. !av_cmp_q(mode_tb, target_tb) &&
  201. field_order_eq(field_order, bmd_field_dominance))
  202. || i == num
  203. || target_mode == bmd_mode) {
  204. ctx->bmd_mode = bmd_mode;
  205. ctx->bmd_width = bmd_width;
  206. ctx->bmd_height = bmd_height;
  207. ctx->bmd_tb_den = bmd_tb_den;
  208. ctx->bmd_tb_num = bmd_tb_num;
  209. ctx->bmd_field_dominance = bmd_field_dominance;
  210. av_log(avctx, AV_LOG_INFO, "Found Decklink mode %d x %d with rate %.2f%s\n",
  211. bmd_width, bmd_height, 1/av_q2d(mode_tb),
  212. (ctx->bmd_field_dominance==bmdLowerFieldFirst || ctx->bmd_field_dominance==bmdUpperFieldFirst)?"(i)":"");
  213. }
  214. mode->Release();
  215. i++;
  216. }
  217. itermode->Release();
  218. if (ctx->bmd_mode == bmdModeUnknown)
  219. return -1;
  220. if (direction == DIRECTION_IN) {
  221. if (ctx->dli->DoesSupportVideoMode(ctx->bmd_mode, (BMDPixelFormat) cctx->raw_format,
  222. bmdVideoOutputFlagDefault,
  223. &support, NULL) != S_OK)
  224. return -1;
  225. } else {
  226. if (!ctx->supports_vanc || ctx->dlo->DoesSupportVideoMode(ctx->bmd_mode, ctx->raw_format,
  227. bmdVideoOutputVANC,
  228. &support, NULL) != S_OK) {
  229. /* Try without VANC enabled */
  230. if (ctx->dlo->DoesSupportVideoMode(ctx->bmd_mode, ctx->raw_format,
  231. bmdVideoOutputFlagDefault,
  232. &support, NULL) != S_OK) {
  233. return -1;
  234. }
  235. ctx->supports_vanc = 0;
  236. }
  237. }
  238. if (support == bmdDisplayModeSupported)
  239. return 0;
  240. return -1;
  241. }
  242. int ff_decklink_set_format(AVFormatContext *avctx, decklink_direction_t direction, int num) {
  243. return ff_decklink_set_format(avctx, 0, 0, 0, 0, AV_FIELD_UNKNOWN, direction, num);
  244. }
  245. int ff_decklink_list_devices(AVFormatContext *avctx,
  246. struct AVDeviceInfoList *device_list,
  247. int show_inputs, int show_outputs)
  248. {
  249. IDeckLink *dl = NULL;
  250. IDeckLinkIterator *iter = decklink_create_iterator(avctx);
  251. int ret = 0;
  252. if (!iter)
  253. return AVERROR(EIO);
  254. while (ret == 0 && iter->Next(&dl) == S_OK) {
  255. IDeckLinkOutput *output_config;
  256. IDeckLinkInput *input_config;
  257. const char *display_name = NULL;
  258. const char *unique_name = NULL;
  259. AVDeviceInfo *new_device = NULL;
  260. int add = 0;
  261. ret = decklink_get_attr_string(dl, BMDDeckLinkDisplayName, &display_name);
  262. if (ret < 0)
  263. goto next;
  264. ret = decklink_get_attr_string(dl, BMDDeckLinkDeviceHandle, &unique_name);
  265. if (ret < 0)
  266. goto next;
  267. if (show_outputs) {
  268. if (dl->QueryInterface(IID_IDeckLinkOutput, (void **)&output_config) == S_OK) {
  269. output_config->Release();
  270. add = 1;
  271. }
  272. }
  273. if (show_inputs) {
  274. if (dl->QueryInterface(IID_IDeckLinkInput, (void **)&input_config) == S_OK) {
  275. input_config->Release();
  276. add = 1;
  277. }
  278. }
  279. if (add == 1) {
  280. new_device = (AVDeviceInfo *) av_mallocz(sizeof(AVDeviceInfo));
  281. if (!new_device) {
  282. ret = AVERROR(ENOMEM);
  283. goto next;
  284. }
  285. new_device->device_name = av_strdup(unique_name ? unique_name : display_name);
  286. new_device->device_description = av_strdup(display_name);
  287. if (!new_device->device_name ||
  288. !new_device->device_description ||
  289. av_dynarray_add_nofree(&device_list->devices, &device_list->nb_devices, new_device) < 0) {
  290. ret = AVERROR(ENOMEM);
  291. av_freep(&new_device->device_name);
  292. av_freep(&new_device->device_description);
  293. av_freep(&new_device);
  294. goto next;
  295. }
  296. }
  297. next:
  298. av_freep(&display_name);
  299. av_freep(&unique_name);
  300. dl->Release();
  301. }
  302. iter->Release();
  303. return ret;
  304. }
  305. /* This is a wrapper around the ff_decklink_list_devices() which dumps the
  306. output to av_log() and exits (for backward compatibility with the
  307. "-list_devices" argument). */
  308. void ff_decklink_list_devices_legacy(AVFormatContext *avctx,
  309. int show_inputs, int show_outputs)
  310. {
  311. struct AVDeviceInfoList *device_list = NULL;
  312. int ret;
  313. device_list = (struct AVDeviceInfoList *) av_mallocz(sizeof(AVDeviceInfoList));
  314. if (!device_list)
  315. return;
  316. ret = ff_decklink_list_devices(avctx, device_list, show_inputs, show_outputs);
  317. if (ret == 0) {
  318. av_log(avctx, AV_LOG_INFO, "Blackmagic DeckLink %s devices:\n",
  319. show_inputs ? "input" : "output");
  320. for (int i = 0; i < device_list->nb_devices; i++) {
  321. av_log(avctx, AV_LOG_INFO, "\t'%s'\n", device_list->devices[i]->device_description);
  322. }
  323. }
  324. avdevice_free_list_devices(&device_list);
  325. }
  326. int ff_decklink_list_formats(AVFormatContext *avctx, decklink_direction_t direction)
  327. {
  328. struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  329. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  330. IDeckLinkDisplayModeIterator *itermode;
  331. IDeckLinkDisplayMode *mode;
  332. uint32_t format_code;
  333. HRESULT res;
  334. if (direction == DIRECTION_IN) {
  335. int ret;
  336. ret = decklink_select_input(avctx, bmdDeckLinkConfigAudioInputConnection);
  337. if (ret < 0)
  338. return ret;
  339. ret = decklink_select_input(avctx, bmdDeckLinkConfigVideoInputConnection);
  340. if (ret < 0)
  341. return ret;
  342. res = ctx->dli->GetDisplayModeIterator (&itermode);
  343. } else {
  344. res = ctx->dlo->GetDisplayModeIterator (&itermode);
  345. }
  346. if (res!= S_OK) {
  347. av_log(avctx, AV_LOG_ERROR, "Could not get Display Mode Iterator\n");
  348. return AVERROR(EIO);
  349. }
  350. av_log(avctx, AV_LOG_INFO, "Supported formats for '%s':\n\tformat_code\tdescription",
  351. avctx->url);
  352. while (itermode->Next(&mode) == S_OK) {
  353. BMDTimeValue tb_num, tb_den;
  354. mode->GetFrameRate(&tb_num, &tb_den);
  355. format_code = av_bswap32(mode->GetDisplayMode());
  356. av_log(avctx, AV_LOG_INFO, "\n\t%.4s\t\t%ldx%ld at %d/%d fps",
  357. (char*) &format_code, mode->GetWidth(), mode->GetHeight(),
  358. (int) tb_den, (int) tb_num);
  359. switch (mode->GetFieldDominance()) {
  360. case bmdLowerFieldFirst:
  361. av_log(avctx, AV_LOG_INFO, " (interlaced, lower field first)"); break;
  362. case bmdUpperFieldFirst:
  363. av_log(avctx, AV_LOG_INFO, " (interlaced, upper field first)"); break;
  364. }
  365. mode->Release();
  366. }
  367. av_log(avctx, AV_LOG_INFO, "\n");
  368. itermode->Release();
  369. return 0;
  370. }
  371. void ff_decklink_cleanup(AVFormatContext *avctx)
  372. {
  373. struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  374. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  375. if (ctx->dli)
  376. ctx->dli->Release();
  377. if (ctx->dlo)
  378. ctx->dlo->Release();
  379. if (ctx->attr)
  380. ctx->attr->Release();
  381. if (ctx->cfg)
  382. ctx->cfg->Release();
  383. if (ctx->dl)
  384. ctx->dl->Release();
  385. }
  386. int ff_decklink_init_device(AVFormatContext *avctx, const char* name)
  387. {
  388. struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  389. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  390. IDeckLink *dl = NULL;
  391. IDeckLinkIterator *iter = decklink_create_iterator(avctx);
  392. if (!iter)
  393. return AVERROR_EXTERNAL;
  394. while (iter->Next(&dl) == S_OK) {
  395. const char *display_name = NULL;
  396. const char *unique_name = NULL;
  397. decklink_get_attr_string(dl, BMDDeckLinkDisplayName, &display_name);
  398. decklink_get_attr_string(dl, BMDDeckLinkDeviceHandle, &unique_name);
  399. if (display_name && !strcmp(name, display_name) || unique_name && !strcmp(name, unique_name)) {
  400. av_free((void *)unique_name);
  401. av_free((void *)display_name);
  402. ctx->dl = dl;
  403. break;
  404. }
  405. av_free((void *)display_name);
  406. av_free((void *)unique_name);
  407. dl->Release();
  408. }
  409. iter->Release();
  410. if (!ctx->dl)
  411. return AVERROR(ENXIO);
  412. if (ctx->dl->QueryInterface(IID_IDeckLinkConfiguration, (void **)&ctx->cfg) != S_OK) {
  413. av_log(avctx, AV_LOG_ERROR, "Could not get configuration interface for '%s'\n", name);
  414. ff_decklink_cleanup(avctx);
  415. return AVERROR_EXTERNAL;
  416. }
  417. if (ctx->dl->QueryInterface(IID_IDeckLinkAttributes, (void **)&ctx->attr) != S_OK) {
  418. av_log(avctx, AV_LOG_ERROR, "Could not get attributes interface for '%s'\n", name);
  419. ff_decklink_cleanup(avctx);
  420. return AVERROR_EXTERNAL;
  421. }
  422. return 0;
  423. }