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