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.

383 lines
13KB

  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 <DeckLinkAPI.h>
  22. #ifdef _WIN32
  23. #include <DeckLinkAPI_i.c>
  24. #else
  25. #include <DeckLinkAPIDispatch.cpp>
  26. #endif
  27. #include <pthread.h>
  28. #include <semaphore.h>
  29. extern "C" {
  30. #include "libavformat/avformat.h"
  31. #include "libavformat/internal.h"
  32. #include "libavutil/imgutils.h"
  33. #include "libavutil/bswap.h"
  34. }
  35. #include "decklink_common.h"
  36. #ifdef _WIN32
  37. IDeckLinkIterator *CreateDeckLinkIteratorInstance(void)
  38. {
  39. IDeckLinkIterator *iter;
  40. if (CoInitialize(NULL) < 0) {
  41. av_log(NULL, AV_LOG_ERROR, "COM initialization failed.\n");
  42. return NULL;
  43. }
  44. if (CoCreateInstance(CLSID_CDeckLinkIterator, NULL, CLSCTX_ALL,
  45. IID_IDeckLinkIterator, (void**) &iter) != S_OK) {
  46. av_log(NULL, AV_LOG_ERROR, "DeckLink drivers not installed.\n");
  47. return NULL;
  48. }
  49. return iter;
  50. }
  51. #endif
  52. #ifdef _WIN32
  53. static char *dup_wchar_to_utf8(wchar_t *w)
  54. {
  55. char *s = NULL;
  56. int l = WideCharToMultiByte(CP_UTF8, 0, w, -1, 0, 0, 0, 0);
  57. s = (char *) av_malloc(l);
  58. if (s)
  59. WideCharToMultiByte(CP_UTF8, 0, w, -1, s, l, 0, 0);
  60. return s;
  61. }
  62. #define DECKLINK_STR OLECHAR *
  63. #define DECKLINK_STRDUP dup_wchar_to_utf8
  64. #define DECKLINK_FREE(s) SysFreeString(s)
  65. #define DECKLINK_BOOL BOOL
  66. #elif defined(__APPLE__)
  67. static char *dup_cfstring_to_utf8(CFStringRef w)
  68. {
  69. char s[256];
  70. CFStringGetCString(w, s, 255, kCFStringEncodingUTF8);
  71. return av_strdup(s);
  72. }
  73. #define DECKLINK_STR const __CFString *
  74. #define DECKLINK_STRDUP dup_cfstring_to_utf8
  75. #define DECKLINK_FREE(s) free((void *) s)
  76. #define DECKLINK_BOOL bool
  77. #else
  78. #define DECKLINK_STR const char *
  79. #define DECKLINK_STRDUP av_strdup
  80. /* free() is needed for a string returned by the DeckLink SDL. */
  81. #define DECKLINK_FREE(s) free((void *) s)
  82. #define DECKLINK_BOOL bool
  83. #endif
  84. HRESULT ff_decklink_get_display_name(IDeckLink *This, const char **displayName)
  85. {
  86. DECKLINK_STR tmpDisplayName;
  87. HRESULT hr = This->GetDisplayName(&tmpDisplayName);
  88. if (hr != S_OK)
  89. return hr;
  90. *displayName = DECKLINK_STRDUP(tmpDisplayName);
  91. DECKLINK_FREE(tmpDisplayName);
  92. return hr;
  93. }
  94. static int decklink_select_input(AVFormatContext *avctx, BMDDeckLinkConfigurationID cfg_id)
  95. {
  96. struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  97. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  98. BMDDeckLinkAttributeID attr_id = (cfg_id == bmdDeckLinkConfigAudioInputConnection) ? BMDDeckLinkAudioInputConnections : BMDDeckLinkVideoInputConnections;
  99. int64_t bmd_input = (cfg_id == bmdDeckLinkConfigAudioInputConnection) ? (int64_t)ctx->audio_input : (int64_t)ctx->video_input;
  100. const char *type_name = (cfg_id == bmdDeckLinkConfigAudioInputConnection) ? "audio" : "video";
  101. int64_t supported_connections = 0;
  102. HRESULT res;
  103. if (bmd_input) {
  104. res = ctx->attr->GetInt(attr_id, &supported_connections);
  105. if (res != S_OK) {
  106. av_log(avctx, AV_LOG_ERROR, "Failed to query supported %s inputs.\n", type_name);
  107. return AVERROR_EXTERNAL;
  108. }
  109. if ((supported_connections & bmd_input) != bmd_input) {
  110. av_log(avctx, AV_LOG_ERROR, "Device does not support selected %s input.\n", type_name);
  111. return AVERROR(ENOSYS);
  112. }
  113. res = ctx->cfg->SetInt(cfg_id, bmd_input);
  114. if (res != S_OK) {
  115. av_log(avctx, AV_LOG_ERROR, "Failed to select %s input.\n", type_name);
  116. return AVERROR_EXTERNAL;
  117. }
  118. }
  119. return 0;
  120. }
  121. static DECKLINK_BOOL field_order_eq(enum AVFieldOrder field_order, BMDFieldDominance bmd_field_order)
  122. {
  123. if (field_order == AV_FIELD_UNKNOWN)
  124. return true;
  125. if ((field_order == AV_FIELD_TT || field_order == AV_FIELD_TB) && bmd_field_order == bmdUpperFieldFirst)
  126. return true;
  127. if ((field_order == AV_FIELD_BB || field_order == AV_FIELD_BT) && bmd_field_order == bmdLowerFieldFirst)
  128. return true;
  129. if (field_order == AV_FIELD_PROGRESSIVE && (bmd_field_order == bmdProgressiveFrame || bmd_field_order == bmdProgressiveSegmentedFrame))
  130. return true;
  131. return false;
  132. }
  133. int ff_decklink_set_format(AVFormatContext *avctx,
  134. int width, int height,
  135. int tb_num, int tb_den,
  136. enum AVFieldOrder field_order,
  137. decklink_direction_t direction, int num)
  138. {
  139. struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  140. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  141. BMDDisplayModeSupport support;
  142. IDeckLinkDisplayModeIterator *itermode;
  143. IDeckLinkDisplayMode *mode;
  144. int i = 1;
  145. HRESULT res;
  146. 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\n",
  147. width, height, tb_num, tb_den, field_order, direction, num);
  148. if (ctx->duplex_mode) {
  149. DECKLINK_BOOL duplex_supported = false;
  150. if (ctx->attr->GetFlag(BMDDeckLinkSupportsDuplexModeConfiguration, &duplex_supported) != S_OK)
  151. duplex_supported = false;
  152. if (duplex_supported) {
  153. res = ctx->cfg->SetInt(bmdDeckLinkConfigDuplexMode, ctx->duplex_mode == 2 ? bmdDuplexModeFull : bmdDuplexModeHalf);
  154. if (res != S_OK)
  155. av_log(avctx, AV_LOG_WARNING, "Setting duplex mode failed.\n");
  156. else
  157. av_log(avctx, AV_LOG_VERBOSE, "Successfully set duplex mode to %s duplex.\n", ctx->duplex_mode == 2 ? "full" : "half");
  158. } else {
  159. av_log(avctx, AV_LOG_WARNING, "Unable to set duplex mode, because it is not supported.\n");
  160. }
  161. }
  162. if (direction == DIRECTION_IN) {
  163. int ret;
  164. ret = decklink_select_input(avctx, bmdDeckLinkConfigAudioInputConnection);
  165. if (ret < 0)
  166. return ret;
  167. ret = decklink_select_input(avctx, bmdDeckLinkConfigVideoInputConnection);
  168. if (ret < 0)
  169. return ret;
  170. res = ctx->dli->GetDisplayModeIterator (&itermode);
  171. } else {
  172. res = ctx->dlo->GetDisplayModeIterator (&itermode);
  173. }
  174. if (res!= S_OK) {
  175. av_log(avctx, AV_LOG_ERROR, "Could not get Display Mode Iterator\n");
  176. return AVERROR(EIO);
  177. }
  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. BMDFieldDominance bmd_field_dominance = mode->GetFieldDominance();
  185. mode->GetFrameRate(&bmd_tb_num, &bmd_tb_den);
  186. AVRational mode_tb = av_make_q(bmd_tb_num, bmd_tb_den);
  187. if ((bmd_width == width &&
  188. bmd_height == height &&
  189. !av_cmp_q(mode_tb, target_tb) &&
  190. field_order_eq(field_order, bmd_field_dominance)) || i == num) {
  191. ctx->bmd_mode = mode->GetDisplayMode();
  192. ctx->bmd_width = bmd_width;
  193. ctx->bmd_height = bmd_height;
  194. ctx->bmd_tb_den = bmd_tb_den;
  195. ctx->bmd_tb_num = bmd_tb_num;
  196. ctx->bmd_field_dominance = bmd_field_dominance;
  197. av_log(avctx, AV_LOG_INFO, "Found Decklink mode %d x %d with rate %.2f%s\n",
  198. bmd_width, bmd_height, 1/av_q2d(mode_tb),
  199. (ctx->bmd_field_dominance==bmdLowerFieldFirst || ctx->bmd_field_dominance==bmdUpperFieldFirst)?"(i)":"");
  200. }
  201. mode->Release();
  202. i++;
  203. }
  204. itermode->Release();
  205. if (ctx->bmd_mode == bmdModeUnknown)
  206. return -1;
  207. if (direction == DIRECTION_IN) {
  208. if (ctx->dli->DoesSupportVideoMode(ctx->bmd_mode, bmdFormat8BitYUV,
  209. bmdVideoOutputFlagDefault,
  210. &support, NULL) != S_OK)
  211. return -1;
  212. } else {
  213. if (ctx->dlo->DoesSupportVideoMode(ctx->bmd_mode, bmdFormat8BitYUV,
  214. bmdVideoOutputFlagDefault,
  215. &support, NULL) != S_OK)
  216. return -1;
  217. }
  218. if (support == bmdDisplayModeSupported)
  219. return 0;
  220. return -1;
  221. }
  222. int ff_decklink_set_format(AVFormatContext *avctx, decklink_direction_t direction, int num) {
  223. return ff_decklink_set_format(avctx, 0, 0, 0, 0, AV_FIELD_UNKNOWN, direction, num);
  224. }
  225. int ff_decklink_list_devices(AVFormatContext *avctx)
  226. {
  227. IDeckLink *dl = NULL;
  228. IDeckLinkIterator *iter = CreateDeckLinkIteratorInstance();
  229. if (!iter) {
  230. av_log(avctx, AV_LOG_ERROR, "Could not create DeckLink iterator\n");
  231. return AVERROR(EIO);
  232. }
  233. av_log(avctx, AV_LOG_INFO, "Blackmagic DeckLink devices:\n");
  234. while (iter->Next(&dl) == S_OK) {
  235. const char *displayName;
  236. ff_decklink_get_display_name(dl, &displayName);
  237. av_log(avctx, AV_LOG_INFO, "\t'%s'\n", displayName);
  238. av_free((void *) displayName);
  239. dl->Release();
  240. }
  241. iter->Release();
  242. return 0;
  243. }
  244. int ff_decklink_list_formats(AVFormatContext *avctx, decklink_direction_t direction)
  245. {
  246. struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  247. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  248. IDeckLinkDisplayModeIterator *itermode;
  249. IDeckLinkDisplayMode *mode;
  250. uint32_t format_code;
  251. int i=0;
  252. HRESULT res;
  253. if (direction == DIRECTION_IN) {
  254. int ret;
  255. ret = decklink_select_input(avctx, bmdDeckLinkConfigAudioInputConnection);
  256. if (ret < 0)
  257. return ret;
  258. ret = decklink_select_input(avctx, bmdDeckLinkConfigVideoInputConnection);
  259. if (ret < 0)
  260. return ret;
  261. res = ctx->dli->GetDisplayModeIterator (&itermode);
  262. } else {
  263. res = ctx->dlo->GetDisplayModeIterator (&itermode);
  264. }
  265. if (res!= S_OK) {
  266. av_log(avctx, AV_LOG_ERROR, "Could not get Display Mode Iterator\n");
  267. return AVERROR(EIO);
  268. }
  269. av_log(avctx, AV_LOG_INFO, "Supported formats for '%s':\n\tmode\tformat_code\tdescription",
  270. avctx->filename);
  271. while (itermode->Next(&mode) == S_OK) {
  272. BMDTimeValue tb_num, tb_den;
  273. mode->GetFrameRate(&tb_num, &tb_den);
  274. format_code = av_bswap32(mode->GetDisplayMode());
  275. av_log(avctx, AV_LOG_INFO, "\n\t%d\t%.4s\t\t%ldx%ld at %d/%d fps",
  276. ++i, (char*) &format_code, mode->GetWidth(), mode->GetHeight(),
  277. (int) tb_den, (int) tb_num);
  278. switch (mode->GetFieldDominance()) {
  279. case bmdLowerFieldFirst:
  280. av_log(avctx, AV_LOG_INFO, " (interlaced, lower field first)"); break;
  281. case bmdUpperFieldFirst:
  282. av_log(avctx, AV_LOG_INFO, " (interlaced, upper field first)"); break;
  283. }
  284. mode->Release();
  285. }
  286. av_log(avctx, AV_LOG_INFO, "\n");
  287. itermode->Release();
  288. return 0;
  289. }
  290. void ff_decklink_cleanup(AVFormatContext *avctx)
  291. {
  292. struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  293. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  294. if (ctx->dli)
  295. ctx->dli->Release();
  296. if (ctx->dlo)
  297. ctx->dlo->Release();
  298. if (ctx->attr)
  299. ctx->attr->Release();
  300. if (ctx->cfg)
  301. ctx->cfg->Release();
  302. if (ctx->dl)
  303. ctx->dl->Release();
  304. }
  305. int ff_decklink_init_device(AVFormatContext *avctx, const char* name)
  306. {
  307. struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data;
  308. struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx;
  309. IDeckLink *dl = NULL;
  310. IDeckLinkIterator *iter = CreateDeckLinkIteratorInstance();
  311. if (!iter) {
  312. av_log(avctx, AV_LOG_ERROR, "Could not create DeckLink iterator\n");
  313. return AVERROR_EXTERNAL;
  314. }
  315. while (iter->Next(&dl) == S_OK) {
  316. const char *displayName;
  317. ff_decklink_get_display_name(dl, &displayName);
  318. if (!strcmp(name, displayName)) {
  319. av_free((void *)displayName);
  320. ctx->dl = dl;
  321. break;
  322. }
  323. av_free((void *)displayName);
  324. dl->Release();
  325. }
  326. iter->Release();
  327. if (!ctx->dl)
  328. return AVERROR(ENXIO);
  329. if (ctx->dl->QueryInterface(IID_IDeckLinkConfiguration, (void **)&ctx->cfg) != S_OK) {
  330. av_log(avctx, AV_LOG_ERROR, "Could not get configuration interface for '%s'\n", name);
  331. ff_decklink_cleanup(avctx);
  332. return AVERROR_EXTERNAL;
  333. }
  334. if (ctx->dl->QueryInterface(IID_IDeckLinkAttributes, (void **)&ctx->attr) != S_OK) {
  335. av_log(avctx, AV_LOG_ERROR, "Could not get attributes interface for '%s'\n", name);
  336. ff_decklink_cleanup(avctx);
  337. return AVERROR_EXTERNAL;
  338. }
  339. return 0;
  340. }