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.

450 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->dlo->DoesSupportVideoMode(ctx->bmd_mode, bmdFormat8BitYUV,
  217. bmdVideoOutputFlagDefault,
  218. &support, NULL) != S_OK)
  219. return -1;
  220. }
  221. if (support == bmdDisplayModeSupported)
  222. return 0;
  223. return -1;
  224. }
  225. int ff_decklink_set_format(AVFormatContext *avctx, decklink_direction_t direction, int num) {
  226. return ff_decklink_set_format(avctx, 0, 0, 0, 0, AV_FIELD_UNKNOWN, direction, num);
  227. }
  228. int ff_decklink_list_devices(AVFormatContext *avctx,
  229. struct AVDeviceInfoList *device_list,
  230. int show_inputs, int show_outputs)
  231. {
  232. IDeckLink *dl = NULL;
  233. IDeckLinkIterator *iter = decklink_create_iterator(avctx);
  234. int ret = 0;
  235. if (!iter)
  236. return AVERROR(EIO);
  237. while (ret == 0 && iter->Next(&dl) == S_OK) {
  238. IDeckLinkOutput *output_config;
  239. IDeckLinkInput *input_config;
  240. const char *displayName;
  241. AVDeviceInfo *new_device = NULL;
  242. int add = 0;
  243. ff_decklink_get_display_name(dl, &displayName);
  244. if (show_outputs) {
  245. if (dl->QueryInterface(IID_IDeckLinkOutput, (void **)&output_config) == S_OK) {
  246. output_config->Release();
  247. add = 1;
  248. }
  249. }
  250. if (show_inputs) {
  251. if (dl->QueryInterface(IID_IDeckLinkInput, (void **)&input_config) == S_OK) {
  252. input_config->Release();
  253. add = 1;
  254. }
  255. }
  256. if (add == 1) {
  257. new_device = (AVDeviceInfo *) av_mallocz(sizeof(AVDeviceInfo));
  258. if (!new_device) {
  259. ret = AVERROR(ENOMEM);
  260. goto next;
  261. }
  262. new_device->device_name = av_strdup(displayName);
  263. new_device->device_description = av_strdup(displayName);
  264. if (!new_device->device_name ||
  265. !new_device->device_description ||
  266. av_dynarray_add_nofree(&device_list->devices, &device_list->nb_devices, new_device) < 0) {
  267. ret = AVERROR(ENOMEM);
  268. av_freep(&new_device->device_name);
  269. av_freep(&new_device->device_description);
  270. av_freep(&new_device);
  271. goto next;
  272. }
  273. }
  274. next:
  275. av_freep(&displayName);
  276. dl->Release();
  277. }
  278. iter->Release();
  279. return ret;
  280. }
  281. /* This is a wrapper around the ff_decklink_list_devices() which dumps the
  282. output to av_log() and exits (for backward compatibility with the
  283. "-list_devices" argument). */
  284. void ff_decklink_list_devices_legacy(AVFormatContext *avctx,
  285. int show_inputs, int show_outputs)
  286. {
  287. struct AVDeviceInfoList *device_list = NULL;
  288. int ret;
  289. device_list = (struct AVDeviceInfoList *) av_mallocz(sizeof(AVDeviceInfoList));
  290. if (!device_list)
  291. return;
  292. ret = ff_decklink_list_devices(avctx, device_list, show_inputs, show_outputs);
  293. if (ret == 0) {
  294. av_log(avctx, AV_LOG_INFO, "Blackmagic DeckLink %s devices:\n",
  295. show_inputs ? "input" : "output");
  296. for (int i = 0; i < device_list->nb_devices; i++) {
  297. av_log(avctx, AV_LOG_INFO, "\t'%s'\n", device_list->devices[i]->device_name);
  298. }
  299. }
  300. avdevice_free_list_devices(&device_list);
  301. }
  302. int ff_decklink_list_formats(AVFormatContext *avctx, decklink_direction_t direction)
  303. {
  304. struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  305. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  306. IDeckLinkDisplayModeIterator *itermode;
  307. IDeckLinkDisplayMode *mode;
  308. uint32_t format_code;
  309. HRESULT res;
  310. if (direction == DIRECTION_IN) {
  311. int ret;
  312. ret = decklink_select_input(avctx, bmdDeckLinkConfigAudioInputConnection);
  313. if (ret < 0)
  314. return ret;
  315. ret = decklink_select_input(avctx, bmdDeckLinkConfigVideoInputConnection);
  316. if (ret < 0)
  317. return ret;
  318. res = ctx->dli->GetDisplayModeIterator (&itermode);
  319. } else {
  320. res = ctx->dlo->GetDisplayModeIterator (&itermode);
  321. }
  322. if (res!= S_OK) {
  323. av_log(avctx, AV_LOG_ERROR, "Could not get Display Mode Iterator\n");
  324. return AVERROR(EIO);
  325. }
  326. av_log(avctx, AV_LOG_INFO, "Supported formats for '%s':\n\tformat_code\tdescription",
  327. avctx->url);
  328. while (itermode->Next(&mode) == S_OK) {
  329. BMDTimeValue tb_num, tb_den;
  330. mode->GetFrameRate(&tb_num, &tb_den);
  331. format_code = av_bswap32(mode->GetDisplayMode());
  332. av_log(avctx, AV_LOG_INFO, "\n\t%.4s\t\t%ldx%ld at %d/%d fps",
  333. (char*) &format_code, mode->GetWidth(), mode->GetHeight(),
  334. (int) tb_den, (int) tb_num);
  335. switch (mode->GetFieldDominance()) {
  336. case bmdLowerFieldFirst:
  337. av_log(avctx, AV_LOG_INFO, " (interlaced, lower field first)"); break;
  338. case bmdUpperFieldFirst:
  339. av_log(avctx, AV_LOG_INFO, " (interlaced, upper field first)"); break;
  340. }
  341. mode->Release();
  342. }
  343. av_log(avctx, AV_LOG_INFO, "\n");
  344. itermode->Release();
  345. return 0;
  346. }
  347. void ff_decklink_cleanup(AVFormatContext *avctx)
  348. {
  349. struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  350. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  351. if (ctx->dli)
  352. ctx->dli->Release();
  353. if (ctx->dlo)
  354. ctx->dlo->Release();
  355. if (ctx->attr)
  356. ctx->attr->Release();
  357. if (ctx->cfg)
  358. ctx->cfg->Release();
  359. if (ctx->dl)
  360. ctx->dl->Release();
  361. }
  362. int ff_decklink_init_device(AVFormatContext *avctx, const char* name)
  363. {
  364. struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  365. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  366. IDeckLink *dl = NULL;
  367. IDeckLinkIterator *iter = decklink_create_iterator(avctx);
  368. if (!iter)
  369. return AVERROR_EXTERNAL;
  370. while (iter->Next(&dl) == S_OK) {
  371. const char *displayName;
  372. ff_decklink_get_display_name(dl, &displayName);
  373. if (!strcmp(name, displayName)) {
  374. av_free((void *)displayName);
  375. ctx->dl = dl;
  376. break;
  377. }
  378. av_free((void *)displayName);
  379. dl->Release();
  380. }
  381. iter->Release();
  382. if (!ctx->dl)
  383. return AVERROR(ENXIO);
  384. if (ctx->dl->QueryInterface(IID_IDeckLinkConfiguration, (void **)&ctx->cfg) != S_OK) {
  385. av_log(avctx, AV_LOG_ERROR, "Could not get configuration interface for '%s'\n", name);
  386. ff_decklink_cleanup(avctx);
  387. return AVERROR_EXTERNAL;
  388. }
  389. if (ctx->dl->QueryInterface(IID_IDeckLinkAttributes, (void **)&ctx->attr) != S_OK) {
  390. av_log(avctx, AV_LOG_ERROR, "Could not get attributes interface for '%s'\n", name);
  391. ff_decklink_cleanup(avctx);
  392. return AVERROR_EXTERNAL;
  393. }
  394. return 0;
  395. }