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.

458 lines
16KB

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