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.

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