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.

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