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.

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