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.

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